Struts2 JSON 插件:添加 ActionMessages、ActionErrors 和 FieldErrors 到响应
我正在制作 JQuery Ajax 帖子,并希望将任何 actionmessages
、actionerrors
和 fielderrors
添加到响应中的操作中(采用 JSON 格式) )。
我将此结果添加
<result name="input" type="json">
<param name="ignoreHierarchy">false</param>
<param name="includeProperties">fieldErrors,actionErrors</param>
</result>
到 struts.xml
中的操作配置中。
尽管值堆栈上存在字段错误,但我收到: {"actionErrors":[],"fieldErrors":{}}
作为响应返回。
如果我将结果配置更改为:
<result name="input" type="json">
<param name="ignoreHierarchy">false</param>
<param name="root">fieldErrors</param>
</result>
JSON 响应是我期望的::{"thePropertyWithValidationError":["您必须提供正确的信息。"]}
如果可能的话,我真的希望响应中包含操作错误和字段错误。
有什么想法吗?提前非常感谢!
编辑:
我想我可能需要使用某种正则表达式...我尝试过:
<result name="input" type="json">
<param name="ignoreHierarchy">false</param>
<param name="includeProperties">fieldErrors\[\d+\],actionErrors\[\d+\]</param>
</result>
结果相同:
{"actionErrors":[],"fieldErrors":{}}
我还发现了这个 错误报告,这可能会导致我的问题,因为我使用的是 Struts v2.2.1。 (v2.2.2 尚未发布)
编辑#2:
也许JSONValidationInterceptor 是我需要的...我似乎不知道如何将它与我的自定义 JQuery Ajax 帖子一起使用...
我正在使用 json拦截器来填充我的属性 - 下面是我的操作配置:
<action name="MyAction" method="add" class="com.test.actions.MyAction">
<interceptor-ref name="json" />
<interceptor-ref name="jsonValidationWorkflowStack"/>
<interceptor-ref name="MyCustomInterceptor" />
<result name="success" type="json" />
</action>
我正在发布: {"struts.enableJSONValidation":"true", "testProperty":"true"}
响应只是转发到我的全局结果映射 error.jsp (显示的字段错误与我的情况相同)设置为在 error.jsp 中显示):
<global-results>
<result name="error">/WEB-INF/jsp/error.jsp</result>
<result name="Exception">/WEB-INF/jsp/error.jsp</result>
</global-results>
<global-exception-mappings>
<exception-mapping exception="java.lang.Throwable" result="Exception" />
</global-exception-mappings>
我想我期望如果堆栈上有 fielderrors/actionerrors,它们会以 JSON 形式返回?
I am making JQuery Ajax posts and would like any actionmessages
, actionerrors
, and fielderrors
added to in the action back in the response (in JSON format).
I added this result:
<result name="input" type="json">
<param name="ignoreHierarchy">false</param>
<param name="includeProperties">fieldErrors,actionErrors</param>
</result>
to my action configuration in the struts.xml
.
I am getting: {"actionErrors":[],"fieldErrors":{}}
back as a response, despite there being field errors on the value stack.
If I change my result configuration to:
<result name="input" type="json">
<param name="ignoreHierarchy">false</param>
<param name="root">fieldErrors</param>
</result>
the JSON response is I expected::{"thePropertyWithValidationError":["You must supply correct information."]}
I would really like both action errors and field errors included in the response, if possible.
Any ideas? Thank you so much in advance!!
Edit:
I think I may need to utilize some sort of regular expression...I tried:
<result name="input" type="json">
<param name="ignoreHierarchy">false</param>
<param name="includeProperties">fieldErrors\[\d+\],actionErrors\[\d+\]</param>
</result>
with the same result:
{"actionErrors":[],"fieldErrors":{}}
I also found this bug report, which may be contributing to my issues as I am using Struts v2.2.1. (v2.2.2 is not yet out)
Edit #2:
Perhaps the JSONValidationInterceptor is what I need...I can't seem to figure out how to use it with my custom JQuery Ajax posts...
I am using the json interceptor to populate my properties-below is my action configuration:
<action name="MyAction" method="add" class="com.test.actions.MyAction">
<interceptor-ref name="json" />
<interceptor-ref name="jsonValidationWorkflowStack"/>
<interceptor-ref name="MyCustomInterceptor" />
<result name="success" type="json" />
</action>
I am posting:{"struts.enableJSONValidation":"true", "testProperty":"true"}
The response is just forwarding to my global results mapping, error.jsp (with the field errors displayed as I have them set to display in the error.jsp):
<global-results>
<result name="error">/WEB-INF/jsp/error.jsp</result>
<result name="Exception">/WEB-INF/jsp/error.jsp</result>
</global-results>
<global-exception-mappings>
<exception-mapping exception="java.lang.Throwable" result="Exception" />
</global-exception-mappings>
I guess I was expecting that if there were fielderrors/actionerrors on the stack, they would be returned as JSON?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我弄清楚了如何在 JSON 结果中返回 ActionErrors、ActionMessages 和 FieldErrors!
下面是一个示例包 - 我设置了一个全局错误处理程序,以 JSON 形式输出操作错误、字段错误和操作消息:
如果存在操作错误或字段错误,我可以检查 JQuery Javascript:
或者循环遍历它们如下:
I figured out how to return ActionErrors, ActionMessages, and FieldErrors in the JSON result!
Below is a sample package- I set a up a global error handler that spits out the action errors, field errors, and action messages as JSON:
I can check in my JQuery Javascript if action errors or field errors exist:
or loop through them as follows:
includeProperties 参数可以使用以下方法稍微缩短:
The includeProperties param can be shortened slightly using: