简体   繁体   English

为什么在struts2中不保留session

[英]Why session is not preserved in struts2

I would like to use values across multiple actions in strtus2, concretelly, I developed following sample.我想在 strtus2 中的多个操作中使用值,具体来说,我开发了以下示例。

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.当我在输入标签中输入sample input时,这个值被传递给updateAction.java然后nextAction.jsp将被加载然后ServletHelperAction将被执行。

After that, sample input is shown in my console.之后, sample input显示在我的控制台中。 My expectation is to show changed in Action because this variable is changed in updateAction.java .我的期望是changed in Action因为此变量在updateAction.java中更改。

Why this variables are not preserved in session?为什么这个变量没有保存在会话中? Is this related to ValueStack ?这与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"
}

The main point that you might be missing while working with Struts2 application.在使用 Struts2 应用程序时您可能会遗漏的要点。 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.这些对象由默认为struts2的对象工厂创建,并使它们可从 Struts2 容器或通过实用程序类静态使用。 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.您永远不会手动创建这些对象,而是由 Struts 管理以正确构建实例并将实例注入容器。

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.如果你曾经使用过容器,你可以参考这个问题来更好地理解 Struts2 中的 DI 和 Struts 容器管理的对象的可用范围。

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. SessionMap对象在执行之前为每个操作创建,用于将 bean 存储到由此类成功包装的 http 会话。 The SesdionMap provides better access to http session attributes rather than itself. SesdionMap提供了对 http 会话属性而不是其自身的更好访问。 Unless you invalidate a session via the SesdionMap or renew a http session Struts will keep your beans in the map between actions.除非您通过SesdionMap使会话无效或更新 http 会话,否则 Struts 将使您的 bean 保持在操作之间的映射中。 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.如果您想在保留相同集合的同时重置SessionMap ,则必须以超出该问题主题的某种方式重新填充它。

From the code you used the form html that is not mapped to any action configuration.从您使用未映射到任何操作配置的表单 html 的代码中。 The code should be updated to use struts tags or at least a valid html.应更新代码以使用 struts 标记或至少使用有效的 html。 Because the code you use doesn't go to the struts action execute method.因为你使用的代码并没有去到struts action execute方法。

<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 SesdionMap and it doesn't contain a value stack. ValueStack有一个action范围,不会进入会话,会话是一个单独的对象SesdionMap ,它不包含值堆栈。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM