2

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")); %>
Roman C
  • 49,761
  • 33
  • 66
  • 176
user752590
  • 111
  • 5
  • 12
  • 29
  • What is the point in appending random values to the session key? – Aleksandr M Feb 04 '15 at 09:43
  • @Aleksandr M -- basically this random number is specific to my tab(it will be unique for that tab for all operation done by user), if uerr use 'open link in new tab' option than than new random variable shall be created for new tab. Since session is shared by all tabs of browser so i am differenciating by key. – user752590 Feb 04 '15 at 10:05
  • @Aleksandr M--I have not decided till yet, may be by some sharing object of JVM/ by some business logic on static hashmap. – user752590 Feb 04 '15 at 10:49
  • 1
    Take a look at this question: http://stackoverflow.com/q/4479995/1700321. – Aleksandr M Feb 04 '15 at 10:58
  • @Aleksandr M Kindly see edited section as session key passing dynamically syntax is not working. – user752590 Feb 05 '15 at 11:09
  • 1
    You shouldn't accept answer until you've verified that it works. – Aleksandr M Feb 05 '15 at 11:11
  • 1
    Put `` and `` in your jsp and see what it prints. – Aleksandr M Feb 05 '15 at 11:12
  • @Aleksandr M... both are printing nothing on gui. – user752590 Feb 05 '15 at 11:49
  • 1
    It means that you haven't set `rndmVar` in your action. – Aleksandr M Feb 05 '15 at 11:52
  • @Aleksandr M.... I can see it as request parameter in scriplet tag, it is printing correct value <% System.out.println(" Random var value passed from action class 2 JSP "+request.getParameter("rndmVar")); %> – user752590 Feb 05 '15 at 11:56
  • 1
    Request parameter isn't the same thing as an S2 value stack. – Aleksandr M Feb 05 '15 at 12:02
  • 1
    You can use `#parameters['rndmVar']` to get value from parameters, but it is rather ugly, better set it properly into value stack. – Aleksandr M Feb 05 '15 at 12:35
  • @Aleksandr M...As u have already said,For all the values where rndmVar is added in value stack,syntax is working perfectly fine,so for value stack variables there is no problem, but somewhere in code i have also used reuest parameter which values will be also appended in 'sessionKey',basicall i want to do something like ...but this syntax is not working – user752590 Feb 09 '15 at 06:49

2 Answers2

2

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.

Community
  • 1
  • 1
Aleksandr M
  • 24,264
  • 12
  • 69
  • 143
1

You can't check the value is set like this, unless the value is of type Boolean. Instead if the value is not set it must contain a null. Then you can check it like this

<s:if test="#session.sessionKey != null">

A random value can be used as an index for the collection defined by the sessionKey.

For example if sessionKey is the Map<String, Object> and it's put into the #session then you can check the object using a random value

<s:if test="#session.sessionKey[rndmVar] != null">
Roman C
  • 49,761
  • 33
  • 66
  • 176