我已经在这里和互联网上进行搜索,但是找不到解决我问题的方法。

我想使用我的自定义异常处理程序来拦截所有Java异常并以特定的方式处理它们(我将特定状态设置为响应,然后在通用Ajax错误中进行处理:调用它们将被处理,而JS对话框将被处理使用特定消息创建)。

因此,我创建了动作:

public class DefaultExceptionHandlerAction extends ExceptionMappingInterceptor  {

/** The class logger. */
private static final Logger LOGGER = Logger.getLogger(DefaultExceptionHandlerAction.class);

@Override
protected void publishException(ActionInvocation invocation, ExceptionHolder exceptionHolder) {

    LOGGER.error("Global Exception msg: "+ exceptionHolder.getException().getMessage(), exceptionHolder.getException());

    String message = "error msg for client";

    HttpServletResponse response = ServletActionContext.getResponse();
    response.reset();
    response.setStatus(HttpServletResponse.SC_BAD_REQUEST);

    PrintWriter out = null;
    try {
        response.setCharacterEncoding("UTF-8");
        out = response.getWriter();
        out.print(message);
    } catch (IOException ioe) {
        LOGGER.error("IOException in printMessage : " + ioe.getMessage(), ioe);
    } finally {
        if (out != null) {
            out.flush();
            out.close();
        }
    }
}
}


除此之外,在我的struts.xml中,我有:

 <interceptor name="exception" class="com.travelsoft.orchestra.b2b.configuration.DefaultExceptionHandlerAction" />

 <interceptor-stack name="securedStack">
   <interceptor-ref name="exception" />
   <interceptor-ref name="defaultStack" />
   <interceptor-ref name="i18n" />
   <interceptor-ref name="login" />
 </interceptor-stack>

<global-results>
  <result name="exception">/defaultExceptionHandler.action</result>
</global-results>

<global-exception-mappings>
  <exception-mapping exception="java.lang.Exception" result="exception" />
</global-exception-mappings>


在一项特定的操作中,我直接在代码中强制使用了空指针异常。
当然,此操作使用的是securedStack拦截器。

执行此操作时,我不确定的循环。

javascript:

  error : function(jqXHR, textStatus, errorThrown) {
     // all other errors
     var contentDialog = $("<div/>",{ "id":"contentDialog"});
     contentDialog.html(jqXHR.responseText);
     contentDialog.dialog({
           title : 'Erreur',
           modal : true,
           zIndex: 8888,
           resizable: false,
           close: function(event, ui) {
             $(this).dialog("destroy").remove();
           },
           buttons:{"OK": function() {
           $(this).dialog("close");}}
     });

 },...

最佳答案

defaultStack already contains exception Interceptor

<interceptor-stack name="defaultStack">
    <interceptor-ref name="exception"/>
    <interceptor-ref name="alias"/>
    <interceptor-ref name="servletConfig"/>
    <interceptor-ref name="i18n"/>
    <interceptor-ref name="prepare"/>
    <interceptor-ref name="chain"/>
    <interceptor-ref name="scopedModelDriven"/>
    <interceptor-ref name="modelDriven"/>
    <interceptor-ref name="fileUpload"/>
    <interceptor-ref name="checkbox"/>
    <interceptor-ref name="multiselect"/>
    <interceptor-ref name="staticParams"/>
    <interceptor-ref name="actionMappingParams"/>
    <interceptor-ref name="params">
        <param name="excludeParams">dojo\..*,^struts\..*,^session\..*,^request\..*,^application\..*,^servlet(Request|Response)\..*,parameters\...*</param>
    </interceptor-ref>
    <interceptor-ref name="conversionError"/>
    <interceptor-ref name="validation">
        <param name="excludeMethods">input,back,cancel,browse</param>
    </interceptor-ref>
    <interceptor-ref name="workflow">
        <param name="excludeMethods">input,back,cancel,browse</param>
    </interceptor-ref>
    <interceptor-ref name="debugging"/>
</interceptor-stack>


,您基本上包括了两个异常拦截器,即您编写的一个和原始的;他们可能互相弄乱了;

尝试通过手动插入默认堆栈的拦截器(除了配置中的第一个异常拦截器)来组成堆栈:

 <interceptor-stack name="securedStack">
   <interceptor-ref name="exception" />
        <interceptor-ref name="alias"/>
        <interceptor-ref name="servletConfig"/>
        <interceptor-ref name="i18n"/>
        <interceptor-ref name="prepare"/>
        <interceptor-ref name="chain"/>
        <interceptor-ref name="scopedModelDriven"/>
        <interceptor-ref name="modelDriven"/>
        <interceptor-ref name="fileUpload"/>
        <interceptor-ref name="checkbox"/>
        <interceptor-ref name="multiselect"/>
        <interceptor-ref name="staticParams"/>
        <interceptor-ref name="actionMappingParams"/>
        <interceptor-ref name="params">
            <param name="excludeParams">dojo\..*,^struts\..*,^session\..*,^request\..*,^application\..*,^servlet(Request|Response)\..*,parameters\...*</param>
        </interceptor-ref>
        <interceptor-ref name="conversionError"/>
        <interceptor-ref name="validation">
            <param name="excludeMethods">input,back,cancel,browse</param>
        </interceptor-ref>
        <interceptor-ref name="workflow">
            <param name="excludeMethods">input,back,cancel,browse</param>
        </interceptor-ref>
        <interceptor-ref name="debugging"/>
   <interceptor-ref name="login" />
 </interceptor-stack>


注意:i18n Interceptor也已经存在...

09-25 22:22