Technology used Struts2 and JSP In Jsp I am checking my session value as whether it is set or not.
<s:if test="%{#session.sessionKey}">
Now I have another random variable rndmVar
in my Action class(I can not set this variable in session scope.), now my session key will be sessionKey
variable appended by rndmVar
value, so how will check this new session key in JSP.
i want something like
<s:if test="%{#session.sessionKey+rndmVar}"> //session key appended by rndmVar value.
First problem how will pass this variable from action class to JSP, and than how will I check it in JSP.
i tried something like
public String rndmVar;
public String getRndmVar() {
return rndmVar;
}
public void setRndmVar(String rndmVar) {
this.lang = lang;
}
than in method of action class(this method of action class will redirect to JSP.), I setted this random variable value.
setRndmVar("rndmfdfkad");
I thought var rndmVar
was added in value stack and can be accessed using OGNL. but this didn't work.
Now I am putting values in session object in action class as.
String randomVar="fadfjk";
String newSessionKey="sessionKey"+randomVar;
session.put(newSessionKey,"value_part");
Now in Jsp I want to check whether newSessionKey
variable is set or not. Now how can I pass dynamically key value to JSP.
<s:if test="#session['sessionKey' + rndmVar] != null">
above Syntax is not working, in fact it is always giving null
and going in else loop.
In my action class
session.put("sessionKey"+rndmVar,"businessList"); //where businessList is ArrayList<BusinessLogicDTO> businessList
I am able to see rndmVar
value in scriplet tags in JSP.
<% System.out.println(" Random var value passed from action class 2 JSP "+request.getParameter("rndmVar")); %>
If you have rndmVar
field with getter/setter in your action and this page is a result of this particular action then you can access this variable like this in JSP.
<s:property value="rndmVar"/>
To get the desired value from the session use this:
<s:if test="#session['sessionKey' + rndmVar] != null">
To get the rndmVar
value from request parameters use this:
<s:if test="#session['sessionKey'+ #parameters['rndmVar'][0]]">
Note the #parameters['some_key']
will return array of Strings, because it can be more than one parameter with some_key
in the request. So you if you want the first parameter in this array you need to use [0]
to get it.
Also check this question to get hints about managing session data for multiple tabs.