Passing variable from action class (Struts 2) to JSP

10,590

Solution 1

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.

Solution 2

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">
Share:
10,590
user752590
Author by

user752590

Updated on June 04, 2022

Comments

  • user752590
    user752590 almost 2 years

    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")); %>
    
  • user752590
    user752590 about 9 years
    ...Kindly please see edited secion, as i was not able to clarify u my problem.
  • Roman C
    Roman C about 9 years
    The key value you can pas from the action with getter, but you'd better use a map for the sessionKey and use a key to access it instead of random session variable.
  • srinivas gowda
    srinivas gowda over 7 years
    <s:property value="#session['resultParameter']" /> <s:if test="resultParameter == success"> <div class="errormessage"> <h1>Hi</h1> </div> </s:if>
  • srinivas gowda
    srinivas gowda over 7 years
    s:property prints value is success why am not getting hi message