转发到相同的struts2操作时丢失bean的值

时间:2012-07-06 19:24:48

标签: java struts2 javabeans

我正在开发一个简单的用户界面,用户可以通过它向我们的公告系统添加消息。

他们在addMessage.jsp上输入详细信息,在reviewMessage.jsp上查看结果,然后从reviewMessage.jsp提交,其中信息将通过存储过程发送到数据库。

一切正常,直到用户从评论页面进入提交。当执行离开JSP并重新进入Action类时,消息bean变为null。如何在同一个动作中通过多个方法和页面持久化bean?

这是我的struts.xml文件的片段:

<!--Render message entry page-->
<action name="activateMessageInitial"
    class="org.mycompany.struts.action.ActivateMessageAction" method="execute">
    <result name="success">/addMessage.jsp</result>
</action>

<!--Receive message bean fields, adjust their results for storage, render review page-->        
<action name="segmentMessage"
    class="org.mycompany.struts.action.ActivateMessageAction" method="parseAndSegment">
    <result name="failed">/addMessage.jsp</result>
    <result name="success">/reviewMessage.jsp</result>
</action>

<!--Review complete, break message bean into fields to submit to database, then return to main menu-->      
<action name="submitMessage"
    class="org.mycompany.struts.action.ActivateMessageAction" method="activate">            
    <result name="success" type="redirectAction">main</result>
</action>   

一个模糊的ActivateMessageAction类:

private TargetedMessage messageBean;

public String execute() throws Exception 
{           
    //empty method  
}

//Take the provider message bean, adjust any values as necessary, and then add in the segmentation detail.
public String parseAndSegment() throws Exception    
{       
    //adjust bean fields
}

//Take a completed provider message bean and send it to the database for processing.
public String activate()
{

    //send bean to database; null, here, hence the error.
    ActivateMessageModel.createMessage(messageBean);    
}

public void setMessageBean(TargetedMessage messageBean)
{
    this.messageBean = messageBean;
}

public TargetedMessage getMessageBean()
{
    return messageBean;
}

1 个答案:

答案 0 :(得分:2)

操作的生命周期仅限于单个HTTP请求。您可以将数据作为隐藏表单字段传递,也可以将对象存储在会话中。