struts2 MessageStoreInterceptor 拦截器的使用

MessageStoreInterceptor 拦截器可以把和该 Action 相关的 messages, errors 和 field errors(下称 "消息") 保存到 session 中(默认放在 Action 中, 而 Action 在 request 中). 以使可以跨请求访问 messages, errors 和 field errors.

从文档上知道 operationMode 的合法取值有: NONE, STORE, RETRIEVE 和 AUTOMATIC. 其中 NONE 为默认值.

         ①. STORE: MessageStoreInterceptor 拦截器会把 "消息" 放入 session 域中.

         ②. RETRIEVE: MessageStoreInterceptor 拦截器会把 "消息" 从 session 中取出来, 放入 Action 中.       

         ③. AUTOMATIC: MessageStoreInterceptor 拦截器会把 "消息" 从 session 中取出来, 放入 Action 中.  而且若响应结果的类型的 redirect, Action 中的 "消息" 会被放入 session 中.               

         ④. NONE: 什么都不做.

MessageStoreInterceptor 拦截器能保证 actionMessage 在 redirect 后不会丢失.  struts.xml 文件的部分配置如下:

<interceptors>
<interceptor-stack name="storeMessage">
<interceptor-ref name="defaultStack" />
<interceptor-ref name="store">
<param name="operationMode">AUTOMATIC</param>
</interceptor-ref>
</interceptor-stack>
</interceptors>

<default-interceptor-ref name="storeMessage" />

使用的时候需要把type参数设置为redirect

<action name="action" class="Action">
<result name="success" type="redirect">NextAction</result>
</action>

原文地址:https://www.cnblogs.com/akatuki/p/4039826.html