java 我可以在不同的动作类之间传播 struts2 ActionErrors 吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1070111/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can I propagate struts2 ActionErrors between different action classes?
提问by sk.
If I have an action where the result is a redirectAction to another action in a different class, is it possible to get validation errors to display in the resulting action? E.g. in the following example, if a user executes actionA (which has no views associated with it), and there are errors, is there any way to display those errors in the actionB result (foo.jsp)? Or am I going about this in completely the wrong way?
如果我有一个操作的结果是重定向操作到不同类中的另一个操作,是否有可能在结果操作中显示验证错误?例如,在下面的示例中,如果用户执行 actionA(没有与之关联的视图),并且出现错误,有没有办法在 actionB 结果 (foo.jsp) 中显示这些错误?还是我以完全错误的方式处理这个问题?
<package name="a" extends="struts-default" namespace="/a">
<action name="actionA" class="actionAClass">
<result name="input" type="redirectAction">
<param name="actionName">actionB</param>
<param name="namespace">/b</param>
</result>
<result type="redirectAction">
<param name="actionName">actionB</param>
<param name="namespace">/b</param>
</result>
</action>
</package>
<package name="b" extends="struts-default" namespace="/b">
<action name="actionB" class="actionBClass">
<result>/foo.jsp</result>
</action>
</package>
采纳答案by Brian Yarger
There may be a way to do that, but I don't think it's a very good way to use struts. If actionA is failing validation, you most likely would want to either have a non-redirect input result for it that shows the errors, or perhaps a global error page that can show it.
可能有办法做到这一点,但我认为这不是使用 struts 的好方法。如果 actionA 验证失败,您很可能希望为其提供一个显示错误的非重定向输入结果,或者一个可以显示它的全局错误页面。
I suppose you could store the action errors somewhere like the session in between the redirect, but you wouldn't really be using the framework how it was designed.
我想你可以将动作错误存储在重定向之间的会话之类的地方,但你不会真正使用框架的设计方式。
回答by Sarvjeet Ahuja
Struts2 by default has a store interceptor. It stores the actionMessages, actionErrors and fieldErrors in session in STORE mode and you can retrieve the same in the next redirect by using the same interceptor by using it in RETRIEVE mode. More details can be found here
Struts2 默认有一个存储拦截器。它在 STORE 模式下的 session 中存储 actionMessages、actionErrors 和 fieldErrors,您可以通过在 RETRIEVE 模式下使用相同的拦截器在下一次重定向中检索相同的内容。可以在此处找到更多详细信息
回答by samsina
Basically you have to use predefined interceptors called store which takes operationMode: store and retrieve:
基本上,您必须使用名为 store 的预定义拦截器,它采用 operationMode: store 和retrieve:
<package name="a" extends="struts-default" namespace="/a">
<action name="actionA" class="actionAClass">
<!-- Here you are storing the Error messages -->
<interceptor-ref name="store">
<param name="operationMode">STORE</param>
</interceptor-ref>
<!-- include your default stack in case you need to load other interceptors -->
<interceptor-ref name="defaultStack" />
<result name="input" type="redirectAction">
<param name="actionName">actionB</param>
<param name="namespace">/b</param>
</result>
<result type="redirectAction">
<param name="actionName">actionB</param>
<param name="namespace">/b</param>
</result>
</action>
</package>
<package name="b" extends="struts-default" namespace="/b">
<action name="actionB" class="actionBClass">
<interceptor-ref name="store">
<param name="operationMode">RETRIEVE</param>
</interceptor-ref>
<!-- include your default stack in case you need to load other interceptors -->
<interceptor-ref name="defaultStack" />
<result>/foo.jsp</result>
</action>
</package>
回答by Jibran
I find a better solution to pass action errors and messages on actionRedirect result type. It is working for me.
我找到了一个更好的解决方案来在 actionRedirect 结果类型上传递操作错误和消息。它对我有用。
<action name="action1" class="action.Action1" >
<result>/abc.jsp</result>
<result name="input" type="redirectAction">
<param name="actionName">action2</param>
<param name="param1">${param1}</param>
<param name="param2">${param2}</param>
<param name="actionErrors">${actionErrors}</param>
</result>
</action>
<action name="action2" class="action.Action2" >
<result>/def.jsp</result>
<result name="input">/def.jsp</result>
</action/>
This is it ..... Happy coding
这就是......快乐编码
回答by tiwari.vikash
Result type chain will copy messages/errors to resulting action if you do following in struts.xml or struts.properties file -
如果您在 struts.xml 或 struts.properties 文件中执行以下操作,结果类型链会将消息/错误复制到结果操作中 -
struts.xwork.chaining.copyErrors - set to true to copy Action Errors
struts.xwork.chaining.copyFieldErrors - set to true to copy Field Errors
struts.xwork.chaining.copyMessages - set to true to copy Action Messages
Example (struts.xml)-
示例(struts.xml)-
<constant name="struts.xwork.chaining.copyErrors" value="true"/>
回答by Meena
Use ActionContext.getContext().getSession().put(key, value)in the first action and retrieve it using ActionContext.getContext().getSession().get(key)in the redirectedActionand addActionErrorsto the main Action
使用ActionContext.getContext().getSession().put(key, value)在第一时间行动,并检索它使用ActionContext.getContext().getSession().get(key)的redirectedAction和addActionErrors在主要的行动
回答by Alireza Fattahi
The store interceptor (MessageStoreInterceptor) can be used to store and retrieve actionErrors, actionMessagesand fieldErrors.
存储拦截器( MessageStoreInterceptor) 可用于存储和检索actionErrors,actionMessages和fieldErrors。
You can change the operation of store interceptor on the fly by passing the operationModeparameter to action
您可以通过将operationMode参数传递给 action来动态更改存储拦截器的操作
http://localhost/sample.action?operationMode=STORE
http://localhost/sample.action?operationMode=STORE
You can set the store interceptor in STOREmode in your default stack which enables all action message to be stored in the session.
您可以STORE在默认堆栈中将存储拦截器设置为模式,这使所有操作消息都可以存储在会话中。
<interceptor-ref name="store">
<param name="operationMode">STORE</param>
</interceptor-ref>
To get the messages you need to add storeinterceptor in RETRIEVEmode to the specific action which needs these messages.
要获取消息,您需要store在RETRIEVE模式中将拦截器添加到需要这些消息的特定操作中。
This is a sample global error page which is redirected to, this action can read actionErrors, fieldErrorsand actionMessageswhen we add the storeinterceptor to it and set the operationModeto RETRIEVE:
这是一个重定向到的示例全局错误页面,此操作可以读取actionErrors, fieldErrors并且actionMessages当我们向其添加store拦截器并将其设置operationMode为RETRIEVE:
@Action(value = "error-page" ,
interceptorRefs =
{@InterceptorRef(value = "store", params = {"operationMode", "RETRIEVE"})}
)
public String execute() throws Exception {
LOG.error("An error accrued during action ActionErrors '{}' , FieldErrors '{}' " , getActionErrors() , getFieldErrors());
//Rest of the code
}
The MessageStoreInterceptorremove previous errors before adding new ones.
在MessageStoreInterceptor添加新的之前删除以前的错误。
You can set the store in AUTOMATICin you default stack. In this way all messages are stored always and will automatically retried when the action result is type of ServletRedirectResult(For example if the action 'redirectAction', 'redirect') So you don't need to define an explicit storeinterceptor in RETRIEVEmode.
您可以AUTOMATIC在默认堆栈中设置商店。通过这种方式,所有消息始终存储,并且在操作结果类型为 时将自动重试ServletRedirectResult(例如,如果操作 'redirectAction', 'redirect') 所以您不需要store在RETRIEVEmode 中定义显式拦截器。
Although it is not a good approach but you can access the store messages in the session with these keys.
虽然这不是一个好方法,但您可以使用这些键访问会话中的存储消息。
session.get(MessageStoreInterceptor.fieldErrorsSessionKey)
session.get(MessageStoreInterceptor.actionErrorsSessionKey)
session.get(MessageStoreInterceptor.actionMessagesSessionKey)
回答by Mozquito
This work in me
这在我身上工作
add this line in struts.xml :
在 struts.xml 中添加这一行:
<constant name="struts.xwork.chaining.copyErrors" value="true"/>
<constant name="struts.xwork.chaining.copyMessages" value="true"/>
use result type "chain" and add result with name "input" :
使用结果类型“chain”并添加名称为“input”的结果:
<package name="a" extends="struts-default" namespace="/a">
<action name="actionA" class="actionAClass">
<result name="input" type="chain">
<param name="actionName">actionB</param>
<param name="namespace">/b</param>
</result>
<result type="chain">
<param name="actionName">actionB</param>
<param name="namespace">/b</param>
</result>
</action>
</package>
<package name="b" extends="struts-default" namespace="/b">
<action name="actionB" class="actionBClass">
<result>/foo.jsp</result>
<result name="input">/foo.jsp</result>
</action>
</package>
回答by Szymon Lisiecki
You can use result type "chain".
您可以使用结果类型“链”。
<action name="delete" class="com.example.Delete">
<result name="error" type="chain">
<param name="actionName">show</param>
</result>
</action>
<action name="show" class="com.example.Show">
<result name="success" type="dispatcher">/jsp/show.jsp</result>
</action>
in show.jsp you can display action errors or action messages that you set in delete action
在 show.jsp 中,您可以显示在删除操作中设置的操作错误或操作消息
回答by rdk
This functionality is not supported by Struts2 by default. Solution exists though (it is done by simple struts interceptor that stores messages in session).
默认情况下,Struts2 不支持此功能。虽然存在解决方案(它是由在会话中存储消息的简单 struts 拦截器完成的)。

