In order to clarify the problem, we must start from the beginning.
First configure a parameter from the background, put in a field, the field called keywords, the value of this parameter is called Efmis://|efmfj|username|2200|0| | 2014|http://10.20.1.54:7001/cssserverportal222012/| | | | | |02, let's not take care of what this value means, I believe many people have encountered more complex strings than this. Background configuration, the foreground can be so to show: ${tag_bean.keywords}, can be sure, regardless of the background configuration into what, the front will certainly be the same as the original display, the first problem appears: in username this position, embedded is the user name of the current logged-on user, Must be dynamic code. To write efmis://|efmfj|${username}|2200|0| | 2014|http://10.20.1.54:7001/cssserverportal222012? This writing and expectations can be different, it will be the same as the same display, and will not translate the EL expression to dynamic code, temporarily do not consider the use of El Expression nesting, it is obviously not directly use, must handle such a string.
This string is to be used as a parameter of a JS method, for example:
Copy Code code as follows:
<li [#if c.keywords?exists] keywords= "${c.keywords}"
[/#if] path= "${c.path}" onclick= "Clickclient (this.path,this.keywords);" >
<a href= "javascript:void (0)" style= "Cursor:pointer;" ><span>${c.name}</span>
</a>
</li>
Clickclient This method is not really a method to invoke, it is only a transitional method.
Copy Code code as follows:
Clickclient = function (path,keywords) {
Parsing decomposition keywords start
Keywords = keywords.replace ("username", "${user.username}");
var suffindex=keywords.indexof ("http");
var prefix = keywords.substr (0,suffindex-1);
var suffix = keywords.substr (suffIndex-1);
var preindex=prefix.lastindexof ("|") +1;
var year = Prefix.substr (Preindex);
prefix = prefix.substr (0,preindex);
Parse Decomposition keywords end
Merge URLs
Keywords = prefix+$ ("#year"). Val () +suffix;
Clientinvoke (path,keywords);
}
In this method, the purpose of the final implementation is to call the Clientinvoke method, and the parameters keywords is changed, to undergo a certain processing, the first ${user.username} to replace the user, in the JS code, Even if an El expression is dynamically parsed, this achieves the dynamic target of invoking the current username. When the 2014 year is also set to dynamic and can be switched, the string is decomposed into three parts:
Prefix: efmis://|efmfj|username|2200|0| |
Year: 2014
Suffix: |http://10.20.1.54:7001/cssserverportal222012/| | | | |02
Put the year in a select Drop-down menu, and when it touches the Clickclient method, immediately remove the year from the current option and then concatenate it with the prefix suffix, which is the flexibility to achieve the annual variability.
Copy Code code as follows:
Annual switching
<select id= "Year" onclick= "Switchyear (this.value);" >
<option value= ">2012</option>"
<option value= "2013" >2013</option>
<option value= "2014" selected= "selected" >2014</option>
<option value= "2015" >2015</option>
</select>
This time there will be a problem, when the annual switch, such as the default of 2014, after switching to 2013, if the page refresh, it will be changed back to the default 2014, this How to do? All the variables are reloaded after the refresh, so setting the global variable is not going to work, so we have to ask, what does not change with the page refresh and we are easy to operate? See the title of this article I think everyone will know: cookie!
Cookies are stored locally and can be stored with them, which plays a great role in remembering the functions of passwords. This is where we use cookies to put the year in a cookie. Each time the page is loaded, determine if the cookie exists, and if it exists, take it out and put it in a select, and if it doesn't exist, remove it from the Select and save it to a cookie.
Copy Code code as follows:
$ (document). Ready (function () {
if (GetCookie ("year") ==null) {//No such cookie exists, put it in
Setcookie ("Year", $ ("#year"). Val ());
}else{
The cookie already exists, it is removed from the
$ ("#year"). Val (GetCookie ("Year"));
}
});
Set cookies
function Setcookie (name,value)
{
var days = 30;
var exp = new Date ();
Exp.settime (Exp.gettime () + 365*24*60*60*1000);
Document.cookie = name + "=" + escape (value);
+ "; expires=" + exp.togmtstring ();
}
Read cookies
function GetCookie (name)
{
var arr,reg=new RegExp ("(^|)" +name+ "= ([^;] *)(;|$)");
if (Arr=document.cookie.match (reg)) return unescape (arr[2]);
else return null;
}
When switching the year, the value of the cookie is also changed:
Copy Code code as follows:
Switchyear=function (year) {
Setcookie ("Year", year);
}
According to the user's request, must let 2014 as the default. After each switch completes the cookie, close the browser, re-open the login to enter the home page, the year is still 2014, not the last switch of that value. So we don't need to set the expiration time of the cookie, just make it clear automatically after the browser is closed.
Of course, if you expect your browser to remember cookies for a long time, set an expiration date, and the annotation code in Setcookie is exactly the time to set an expiration date. Interested can be studied under.