Value of Value stack is changed after execute another action in Struts2

124 Views Asked by At

I have like following jsp.I send some data via form and execute 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.

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. Seems that after <s:action> these sent data was recaptured by value stack.

My desired result is like

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

What is the root cause of this?

1

There are 1 best solutions below

0
On BEST ANSWER

If you set some bean to the action object, then it belongs to it until you reference it someare else, i.e. a session map. Since you have to implement a SessionAware interface on CasePostAction. 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.

In the JSP you use EL expression to search for the CasePostFormBean. Currently you keep it in the action bean and a scope belongs to the action scope.

The action scope is similar to the request if you don't instantiate other actions in the request like <s:action>. Each action has its own action scope and therefore a 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.

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}"/>