我正在开发一个简单的 UI,用户可以通过它向我们的公告系统添加消息。
他们在 addMessage.jsp 中输入详细信息,在 reviewMessage.jsp 上查看结果,然后从 reviewMessage.jsp 提交,信息将通过存储过程发送到数据库。
一切正常,直到用户从评论页面提交。当执行离开 JSP 并重新进入 Action 类时,消息 bean 已变为空。如何在同一个操作中通过多个方法和页面持久化 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;
}