Home > Mobile >  Why session is not preserved in struts2
Why session is not preserved in struts2

Time:12-28

I would like to use values across multiple actions in strtus2 , concretelly, I developed following sample.

When I input like sample input in input tag, this value is passed to updateAction.java then nextAction.jsp will be loaded then ServletHelperAction will executed.

After that,sample input is shown in my console. My expectation is to show changed in Action because this variable is changed in updateAction.java.

Why this variables are not preserved in session ? Is this related to ValueStack ?

jsp

<input name="inputValue" value="${getInputValue()}" form="formCasePost">

<form method="post" id="formCasePost" submit="case-update"></form>

updateAction.java

public class updateAction extends ActionSupport implements SessionAware {

 @Getter @Setter
 private String inputValue;

 @Getter @Setter
 private Map<String, Object> session;

 @Action(value = "case-update", results = {
        @Result(location = "nextAction.jsp")
    })
    public String update() {
       this.setInputValue("changed in Action");
       this.session.put("changedInputValue",inputValue)
        return SUCCESS;
    }
}

nextAction.jsp

<s:action var="ServletHelperAction" name="login-user-to-bean" namespace="/servlet-helper"></s:action>

AnotherFileAction.java

public class ServletHelperAction extends ActionSupport implements SessionAware{

        @Action(value = "login-user-to-bean", results = {
        @Result(location = "servlethelper.jsp")
    })
    public String loginUserToBean() {
         System.out.println(this.session.get("changedInputValue"));
        // my expectation is "changed in Action", but actual value is "sample input"
}

CodePudding user response:

The main point that you might be missing while working with Struts2 application. Every action is using there own action context and therefore a value stack. These objects are created by the object factory which is struts2 by default and makes them available from the Struts2 conteiner or statically via utility classes. The last option is very usable if you need them from the custom interceptor. You never create these object manually but managed by Struts to properly build and inject instances to the container.

If you ever worked with the container you can refer this question to better understand DI in Struts2 and available scopes of objects managed by Struts container.

The SessionMap object is created for each action before it executes to be used for storing your beans to http session which is successfully wrapped by this class. The SesdionMap provides better access to http session attributes rather than itself. Unless you invalidate a session via the SesdionMap or renew a http session Struts will keep your beans in the map between actions. If you want to reset the SessionMap while keeping the same collection you have to repopulate it in some way that out of topic of this question.

From the code you used the form html that is not mapped to any action configuration. The code should be updated to use struts tags or at least a valid html. Because the code you use doesn't go to the struts action execute method.

<form method="post" id="formCasePost" action="case-update">
  <input type="submit" value="case-update">
</form>

In the view you map the action which is in different namespace instead of default. If the action is not mapped correctly then you might not get any errors by the action tag.

The ValueStack has a scope to action and doesn't go to the session which is a separate object SessionMap and it doesn't contain a value stack.

  • Related