简体   繁体   English

在 Struts2 中执行另一个操作后,值堆栈的值发生变化

[英]Value of Value stack is changed after execute another action in Struts2

I have like following jsp .I send some data via form and execute Action ,我喜欢关注jsp 。我通过表单发送一些数据并执行Action

case.jsp

before action:${CasePostFormBean.subject}
<s:action var="ServletHelperAction" name="login-user-to-bean" namespace="/servlet-helper"></s:action>
after action:${CasePostFormBean.subject}

<input name="CasePostFormBean.subject" value="${CasePostFormBean.getSubject()}" form="formCasePost">

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

servlethelper.jsp

<html></html>

CasePostAction.java

public class CasePostAction extends ActionSupport implements SessionAware {

    @Getter @Setter
    private CasePostFormBean casePostFormBean = new CasePostFormBean();

    @Action(value = "case-create", results = {
        @Result(location = "case.jsp")
    })
    public String register() {
        this.casePostFormBean.setSubject("success to send form");
        return SUCCESS;
    }
}
}
public class ServletHelperAction extends ActionSupport implements SessionAware  {

    // Beans
    @Getter @Setter
    private UserParamsBean userParamsBean = new UserParamsBean();

    @Action(value = "login-user-to-bean", results = {
        @Result(location = "servlethelper.jsp")
    })
    public String loginUserToBean() {
        final MstUserModel mstUserModel = new MstUserModel(this.dbManager.getSqlSession());

        if (this.session.get("userParamsBean.userCode") != null) {
            this.userParamsBean.setUserCode(this.session.get("userParamsBean.userCode").toString());

            Map<String, Object> loginUser = mstUserModel.selectUserOne(1, this.userParamsBean.getUserCode()).get(0);
            this.userParamsBean.setUserName(loginUser.get("name").toString());
        }
        return SUCCESS;
    }
}

After input input value and after button submitted, like following was shown.输入input value并提交按钮后,如下所示。

before action: success to send form
after action: input value

Out put is like follows, When I execute another action via <s:action> the value stack seems to be changed.输出如下,当我通过<s:action>执行另一个操作时,值堆栈似乎发生了变化。 Seems that after <s:action> these sent data was recaptured by value stack.似乎在<s:action>之后,这些发送的数据被值堆栈重新捕获。

My desired result is like我想要的结果就像

before action: success to send form
after action: success to send form

What is the root cause of this?根本原因是什么?

If you set some bean to the action object, then it belongs to it until you reference it someare else, ie a session map. Since you have to implement a SessionAware interface on CasePostAction .如果您将某个 bean 设置为操作 object,那么它属于它,直到您在其他地方引用它,即 session map。因为您必须在CasePostAction上实现SessionAware接口。 Then you put CasePostFormBean into the session map. In this way the scope of the object will be a session scope until you remove it from the session or invalidate a session map.然后,您将CasePostFormBean放入session882477763218788。以这种方式,8828299540288的scope将是session88888888888888888888888888888888888888.

In the JSP you use EL expression to search for the CasePostFormBean .在 JSP 中,您使用 EL 表达式来搜索CasePostFormBean Currently you keep it in the action bean and a scope belongs to the action scope.目前,您将其保存在操作 bean 中,而 scope 属于action scope。

The action scope is similar to the request if you don't instantiate other actions in the request like <s:action> .如果您不在请求中实例化其他操作,例如<s:action> ,则action scope 类似于request Each action has its own action scope and therefore a ValueStack .每个动作都有自己的action scope ,因此有一个ValueStack

Change the code更改代码

public class CasePostAction extends ActionSupport implements SessionAware {

    @Getter @Setter
    private CasePostFormBean casePostFormBean = new CasePostFormBean();

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

    @Action(value = "case-create", results = {
        @Result(location = "case.jsp")
    })
    public String register() {
        
this.casePostFormBean.setSubject("success to send form");
        session.put("casePostFormBean", casePostFormBean);
        return SUCCESS;
    }
}
}

When a result is executed you should reference this object directly from the session.执行结果时,您应该直接从 session 引用此 object。

before action:<s:property value="%{#session.casePostFormBean.subject}"/>
<s:action var="ServletHelperAction" name="login-user-to-bean" namespace="/servlet-helper"></s:action>
after action:<s:property value="%{#session.casePostFormBean.subject}"/>

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

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