You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Struts2会话未保留问题咨询:跨Action值传递异常

问题:Struts2中Session变量修改后未保留值的原因

在Struts2中尝试跨Action共享值,流程如下:

  1. 输入框输入sample input,提交到updateAction.java
  2. updateAction将值修改为changed in Action并存入Session,随后跳转到nextAction.jsp
  3. nextAction.jsp通过<s:action>标签调用ServletHelperAction
  4. 控制台输出的Session值是sample input,而非预期的changed in Action

疑问:为什么Session中没有保留修改后的值?是否和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>

ServletHelperAction.java

public class ServletHelperAction extends ActionSupport implements SessionAware{

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

    @Action(value = "login-user-to-bean", results = {
            @Result(location = "servlethelper.jsp")
        })
    public String loginUserToBean() {
        System.out.println(this.session.get("changedInputValue"));
        // 预期输出"changed in Action",实际输出"sample input"
    }
}

问题原因及解决方案

核心原因

  1. 代码语法错误导致Session赋值未执行
    原updateAction的update()方法中,session.put语句末尾缺少分号,这会引发编译错误,导致修改后的值根本没存入Session。此时控制台输出的sample input是请求参数直接传入或其他逻辑意外存入的旧值。

  2. ServletHelperAction的Session注入问题
    若ServletHelperAction未给session属性添加@Getter@Setter,Struts2无法完成Session注入,调用session.get()会抛出空指针异常,你看到的输出大概率是其他逻辑的结果。

  3. 请求参数的意外覆盖(潜在风险)
    <s:action>标签默认会把当前请求的参数注入到目标Action中,如果ServletHelperAction定义了inputValue属性,原始请求的sample input可能会覆盖Session中的值。

  4. 与ValueStack无关
    此问题和ValueStack没有关系,ValueStack负责Action属性的OGNL解析与访问,Session是独立的全局存储容器,二者逻辑互不影响。

修复步骤

  1. 修正updateAction的语法错误
    在session.put语句末尾添加分号,确保代码正常编译执行:

    this.session.put("changedInputValue", inputValue);
    
  2. 完善ServletHelperAction的SessionAware实现
    确保session属性有@Getter@Setter,让Struts2能正确注入Session:

    @Getter @Setter
    private Map<String, Object> session;
    
  3. 阻止请求参数注入目标Action
    在<s:action>标签中添加ignoreParams="true",避免原始请求参数覆盖Session值:

    <s:action var="ServletHelperAction" name="login-user-to-bean" namespace="/servlet-helper" ignoreParams="true"></s:action>
    
  4. 添加日志验证赋值逻辑
    在updateAction中加入日志输出,确认修改后的值已存入Session:

    System.out.println("存入Session的值:" + inputValue);
    

内容的提问来源于stack exchange,提问作者Heisenberg

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.07 12:35:18