Struts2 JSON 插件:添加 ActionMessages、ActionErrors 和 FieldErrors 到响应

发布于 2024-10-26 14:29:52 字数 2642 浏览 2 评论 0原文

我正在制作 JQuery Ajax 帖子,并希望将任何 actionmessagesactionerrorsfielderrors 添加到响应中的操作中(采用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

〆凄凉。 2024-11-02 14:29:52

我弄清楚了如何在 JSON 结果中返回 ActionErrors、ActionMessages 和 FieldErrors!

下面是一个示例包 - 我设置了一个全局错误处理程序,以 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:

/**
 * 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;
}  

或者循环遍历它们如下:

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

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:

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

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);  
    });  
}
听,心雨的声音 2024-11-02 14:29:52

includeProperties 参数可以使用以下方法稍微缩短:

<result name="error" type="json">
    <param name="ignoreHierarchy">false</param>
    <param name="includeProperties">
        actionErrors.*,
        actionMessages.*,
        fieldErrors.*
    </param>
</result>

The includeProperties param can be shortened slightly using:

<result name="error" type="json">
    <param name="ignoreHierarchy">false</param>
    <param name="includeProperties">
        actionErrors.*,
        actionMessages.*,
        fieldErrors.*
    </param>
</result>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文