在 PRG 模式中如何在刷新成功页面上删除 ActionMessage

2022-01-16 00:00:00 html post-redirect-get java jsp struts2

为了避免表单重新提交,我使用了 POST-Redirect-GET 模式,它工作正常.现在在我的注册页面(成功页面)上显示 ActionMessage 评论插入成功!".当用户刷新页面时,我想删除此消息.

To avoid the form resubmission I used POST-Redirect-GET pattern and it is working fine. Now on my registration page(success page) it is showing the ActionMessage "Review Inserted successfully!". When user refreshes the page I want to remove this message.

我在 JSP 页面上的代码:

My code on JSP page:

<s:if test="hasActionMessages()">
    <div class="success-mesg" id="success-mesg">
        <s:iterator value="actionMessages">
            <s:property value="top" />
        </s:iterator>
    </div>
</s:if>

关于struts.xml:

On struts.xml:

<action name="insertReview" class = "com.tenkinfo.streamlinedmapnsav.ui.action.WriteReviewAction" method="insertReview" >
    <result name="insertReviewDone" type="redirect" >insertReviewDone</result>
    <result name="input" type="tiles">display.writeReview.page</result>
</action>

<action name="insertReviewDone" class = "com.tenkinfo.streamlinedmapnsav.ui.action.WriteReviewAction" method="insertReviewDone" >
    <result name="success" type="tiles" >display.writeReview.page</result>
</action>

在动作类中:

public String insertReviewDone(){
    addActionMessage("Write review is successful");
    return "success";
}

在执行 Aleksandr M 建议的答案后,我遇到了以下问题:

After implementing the answer suggested by Aleksandr M, I am having below problem:

<s:form name="insertReview" action="insertReview" id="insertReview"
            theme="simple" enctype="multipart/form-data">
        <div class="content-area">
            <h1>
            Review
            <s:property value="businessName" />
            </h1>
            <s:if test="hasActionMessages()">
            <div class="success-mesg" id="success-mesg">
            <s:iterator value="actionMessages">
        <s:property value="top" />
    </s:iterator>
   </div>
</s:if>

刷新以上代码中的页面后,businessName 被删除,不显示.任何帮助将不胜感激.

After refreshing the page in above code businessName is removed and not displaying. Any help will be appreciated.

推荐答案

使用 Message Store Interceptor 将操作消息存储在您的 insertReview 中并在您的 insertReviewDone 操作中检索它.当您返回 insertReviewDone 结果时,在 insertReview 方法中添加操作消息.

Use Message Store Interceptor to store action message in your insertReview and retrieve it in your insertReviewDone action. Add action message in insertReview method when you are returning insertReviewDone result.

也可以使用 redirectAction 结果重定向到 action 而不是 redirect.

Also use redirectAction result to redirect to action instead of redirect.

<action name="insertReview" class = "com.tenkinfo.streamlinedmapnsav.ui.action.WriteReviewAction" method="insertReview">
  <interceptor-ref name="store">
    <param name="operationMode">STORE</param>
  </interceptor-ref>
  <interceptor-ref name="defaultStack" />

  <result name="insertReviewDone" type="redirectAction">insertReviewDone</result>
  <result name="input" type="tiles">display.writeReview.page</result>
</action>

<action name="insertReviewDone" class = "com.tenkinfo.streamlinedmapnsav.ui.action.WriteReviewAction" method="insertReviewDone" >
  <interceptor-ref name="store">
    <param name="operationMode">RETRIEVE</param>
  </interceptor-ref>
  <interceptor-ref name="defaultStack" />

  <result name="success" type="tiles">display.writeReview.page</result>
</action>

相关文章