<small id='iyrWk'></small><noframes id='iyrWk'>

      <tfoot id='iyrWk'></tfoot>

    1. <legend id='iyrWk'><style id='iyrWk'><dir id='iyrWk'><q id='iyrWk'></q></dir></style></legend>
        <bdo id='iyrWk'></bdo><ul id='iyrWk'></ul>
      <i id='iyrWk'><tr id='iyrWk'><dt id='iyrWk'><q id='iyrWk'><span id='iyrWk'><b id='iyrWk'><form id='iyrWk'><ins id='iyrWk'></ins><ul id='iyrWk'></ul><sub id='iyrWk'></sub></form><legend id='iyrWk'></legend><bdo id='iyrWk'><pre id='iyrWk'><center id='iyrWk'></center></pre></bdo></b><th id='iyrWk'></th></span></q></dt></tr></i><div id='iyrWk'><tfoot id='iyrWk'></tfoot><dl id='iyrWk'><fieldset id='iyrWk'></fieldset></dl></div>

        如何验证使用 Struts2 JSON 插件发送的 JSON 而不抛出异常

        How to validate JSON being sent using Struts2 JSON Plugin without throwing exception(如何验证使用 Struts2 JSON 插件发送的 JSON 而不抛出异常)
            <tfoot id='GWuce'></tfoot>

              <tbody id='GWuce'></tbody>

              <bdo id='GWuce'></bdo><ul id='GWuce'></ul>

              1. <i id='GWuce'><tr id='GWuce'><dt id='GWuce'><q id='GWuce'><span id='GWuce'><b id='GWuce'><form id='GWuce'><ins id='GWuce'></ins><ul id='GWuce'></ul><sub id='GWuce'></sub></form><legend id='GWuce'></legend><bdo id='GWuce'><pre id='GWuce'><center id='GWuce'></center></pre></bdo></b><th id='GWuce'></th></span></q></dt></tr></i><div id='GWuce'><tfoot id='GWuce'></tfoot><dl id='GWuce'><fieldset id='GWuce'></fieldset></dl></div>

                  <small id='GWuce'></small><noframes id='GWuce'>

                  <legend id='GWuce'><style id='GWuce'><dir id='GWuce'><q id='GWuce'></q></dir></style></legend>
                  本文介绍了如何验证使用 Struts2 JSON 插件发送的 JSON 而不抛出异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  假设动作中有一个 Double 变量,如果请求正文中发送的值类似于

                  Suppose there's a Double variable in an action, and if the value sent in the request body is something like

                  {"dblField":""}
                  

                  interceptorStack 看起来像:

                  <action name="save" class="actions.MyAction" method="save">
                      <interceptor-ref name="jsonValidationWorkflowStack">
                      </interceptor-ref>
                      <!--<interceptor-ref name="loginStack"/>-->
                     <!-- I've tried using each of the above two separately, but both failed -->
                      <interceptor-ref name="json">
                          <param name="enableSMD">true</param>
                      </interceptor-ref>
                      
                      <result type="json" name="*">
                          <param name="excludeProperties">
                              idIo
                          </param>
                      </result>
                  </action>
                  

                  然后动作抛出NumberFormatException,根据插件源代码这里.

                  Then the action throws a NumberFormatException, which is fine according to the plugin source code here.

                  但是这个异常并没有在插件中处理,因此,从动作抛出异常返回,这导致 global-exception-handler 的触发.

                  But this exception is not handled in the plugin and hence, returns from the action throwing exception, which results in the firing of global-exception-handler.

                  如果使用查询字符串发送相同的请求,?dblField= 则操作返回 INPUT.

                  If the same request was sent using a query-string, ?dblField= then the action returns INPUT.

                  那么,我怎样才能使 json-plugin 以相同的方式返回 INPUT 并设置适当的 fieldErrors 而不是抛出 NumberFormatException并触发 globalExceptionHandler?

                  So, how can I make the json-plugin behave in the same way to return INPUT and set appropriate fieldErrors instead of throwing NumberFormatException and firing the globalExceptionHandler?

                  推荐答案

                  你可以把 exception 拦截器 在你自己的拦截器之前而不是 json 拦截器 通过扩展 json 拦截器并覆盖 intercept 方法,您可以在其中捕获错误.然后,您可以添加操作错误或重新抛出自定义异常,您可以在操作配置中或全局映射.

                  You could place exception interceptor before your own interceptor instead of the json interceptor by extending json interceptor and override intercept method where you can catch errors. Then you can either add action errors or rethrow a custom exception which you can map in the action config or globally.

                  <exception-mapping exception="org.exceptionmapping.CustomException"
                                               result="errorresult"/>
                  

                  这样您可以将所有仅 json 的拦截器错误映射到您的自定义异常.

                  This way you can map all json only interceptor errors with your custom exception.

                  这篇关于如何验证使用 Struts2 JSON 插件发送的 JSON 而不抛出异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Java Bytecode Manipulation Library Suggestions(Java 字节码操作库建议)
                  Java CLI UI-design: frameworks or libraries?(Java CLI UI 设计:框架还是库?)
                  About the use of Beans.xml configuration file in Spring Framework application(关于Spring Framework应用中Beans.xml配置文件的使用)
                  What is the difference between Spring, Struts, Hibernate, JavaServer Faces, Tapestry?(Spring、Struts、Hibernate、JavaServer Faces、Tapestry 有什么区别?)
                  Are there any android application framework like spring?(有没有像spring这样的android应用程序框架?)
                  Java Swing based game framework. Any advice?(基于 Java Swing 的游戏框架.有什么建议吗?)

                1. <legend id='usyOf'><style id='usyOf'><dir id='usyOf'><q id='usyOf'></q></dir></style></legend>

                  • <bdo id='usyOf'></bdo><ul id='usyOf'></ul>
                      <tbody id='usyOf'></tbody>
                    <tfoot id='usyOf'></tfoot>

                        <small id='usyOf'></small><noframes id='usyOf'>

                          <i id='usyOf'><tr id='usyOf'><dt id='usyOf'><q id='usyOf'><span id='usyOf'><b id='usyOf'><form id='usyOf'><ins id='usyOf'></ins><ul id='usyOf'></ul><sub id='usyOf'></sub></form><legend id='usyOf'></legend><bdo id='usyOf'><pre id='usyOf'><center id='usyOf'></center></pre></bdo></b><th id='usyOf'></th></span></q></dt></tr></i><div id='usyOf'><tfoot id='usyOf'></tfoot><dl id='usyOf'><fieldset id='usyOf'></fieldset></dl></div>