Struts2的JSON插件:添加ActionMessages,的ActionErrors,并FieldErrors以响应 [英] Struts2 JSON Plugin: Adding ActionMessages, ActionErrors, and FieldErrors to response

查看:399
本文介绍了Struts2的JSON插件:添加ActionMessages,的ActionErrors,并FieldErrors以响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提出的JQuery Ajax的职位,并希望任何 actionmessages 的ActionErrors fielderrors 加入到行动早在响应(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).

我加了这样的结果:

<result name="input" type="json">    
    <param name="ignoreHierarchy">false</param>  
    <param name="includeProperties">fieldErrors,actionErrors</param>  
</result>

在我的操作配置的的struts.xml

我得到: {的ActionErrors:[],fieldErrors:{}} 返回作为响应,尽管值堆在那里是场错误。

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>

JSON的反应是我的预期::标签      {thePropertyWithValidationError:你必须提供正确的信息]}

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!!

编辑:
我想我可能需要利用某种常规EX pression ......我想:


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":{}}

我也发现了这个错误报告,这可能因为我使用Struts是导致我的问题V2.2.1。 (V2.2.2还没有出来)

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)

编辑#2:

也许<一href="http://struts.apache.org/2.1.2/struts2-core/apidocs/org/apache/struts2/interceptor/validaton/JSONValidationInterceptor.html">JSONValidationInterceptor是什么,我需要......我似乎无法弄清楚如何使用它与我的自定义JQuery的Ajax的帖子...

Perhaps the JSONValidationInterceptor is what I need...I can't seem to figure out how to use it with my custom JQuery Ajax posts...

我使用的JSON拦截器来填充我的属性,下面是我的操作配置:

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>  

我张贴: {struts.enableJSONValidation:真正的,testProperty:真正的}

I am posting: {"struts.enableJSONValidation":"true", "testProperty":"true"}

响应只是转发到我的全球结果的映射,error.jsp文件(含显示,因为我有他们设置在error.jsp页面显示的字段错误):

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>  

我想我期待,如果有fielderrors /堆栈上的ActionErrors,他们将返回JSON?

I guess I was expecting that if there were fielderrors/actionerrors on the stack, they would be returned as JSON?

推荐答案

我想出如何返回JSON结果的ActionErrors,ActionMessages和FieldErrors!

I figured out how to return ActionErrors, ActionMessages, and FieldErrors in the JSON result!

下面是一个简单的包 - 我设置了该吐出的动作失误,现场的错误,并采取行动的消息为JSON一个全球性的错误处理程序:

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:

<!--myBase has interceptor stack defined-->
<package name="JsonResults" namespace="/json" extends="myBase">

<global-results>
    <result name="input" type="json">
        <param name="ignoreHierarchy">false</param>
        <param name="includeProperties">actionErrors\[\d+\], fieldErrors\..+$, actionMessages\[\d+\]</param>
    </result>
    <result name="error" type="json">
        <param name="ignoreHierarchy">false</param>
        <param name="includeProperties">actionErrors\[\d+\], fieldErrors\..+$, actionMessages\[\d+\]</param>
    </result>
    <result name="Exception" type="json">
        <param name="ignoreHierarchy">false</param>
        <param name="includeProperties">actionErrors\[\d+\], fieldErrors\..+$, actionMessages\[\d+\]</param>
    </result>
</global-results>
<global-exception-mappings>
    <exception-mapping exception="java.lang.Throwable" result="Exception" />
</global-exception-mappings>

<!-- sample action -->  
<action name="SampleAction" method="loadSamples" class="com.sample">
    <result name="success" type="json">
        <param name="ignoreHierarchy">false</param>
        <param name="includeProperties">sampleComplexBean\[\d+\]\..+$, actionMessages\[\d+\]</param>
    </result>   
</action>
</package>

我可以检查我的JQuery的Javascript如果操作错误或现场错误存在:

I can check in my JQuery Javascript if action errors or field errors exist:

/**
 * Checks the passed in json and searches for the 
 * existence of an "actionErrors" or "fieldErrors" objects
 * 
 * @param json          The JSON to check for errors
 * @returns {Boolean}   true if errors exist; false otherwise
 */
function areActionOrFieldErrorInJson(json) {

    var errorsExist = false;

    if ( json != null && ( json.actionErrors != null || json.fieldErrors != null ) ) {
        errorsExist = true;
    }

    return errorsExist;
}  

或通过它们循环如下:

or loop through them as follows:

if ( json.actionErrors != null && json.actionErrors.length !== 0 ) {  
    $.each(json.actionErrors, function(e) {  
        doStuff(this);  
    });  
}

这篇关于Struts2的JSON插件:添加ActionMessages,的ActionErrors,并FieldErrors以响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆