zoukankan      html  css  js  c++  java
  • SSH答疑解惑系列(三)——Struts2的异常处理

    Struts2的异常采用声明式异常捕捉,具体通过拦截器来实现。

    在项目中,我们可以在Action中直接抛出异常,剩下的就交给Struts2的拦截器来处理了。当然,我们需要进行相关配置。

    Struts2配置了默认的拦截器,来处理异常。在struts-default.xml中,可以找到配置。有了这个配置以后,我们在项目中就方便多了。

     <interceptor name="exception" class="com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor"/>

    在项目中,只需要使用exception-mapping标签来配置:

    1)全局异常

    适用对象:所有Action

    <global-results >
            <!-- 跳转到全局异常的页面 -->
            <result name="global-error">/global-error.jsp</result>
        </global-results>
    
        <global-exception-mappings>
            <exception-mapping result="global-error" exception="com.struts2.ApplicationException"></exception-mapping>
        </global-exception-mappings>

    2)局部异常

    使用对象:异常所在的Action

    <!-- 局部异常,LoginAction -->
            <action name="login" class="com.struts2.LoginAction">
    
                <exception-mapping result="error" exception="com.struts2.ApplicationException"></exception-mapping>
                <result>/success.jsp</result>
                <result name="error">/error.jsp</result>
            </action>

    ApplicationException

    这里写图片描述

    Throwable是所有异常的根
    Error是错误
    Exception是异常,RuntimeException是运行中的异常。

    除了Struts2自带的拦截器,我们还可以自定义拦截器,覆盖底层的拦截器,使得异常的捕捉更加灵活。

    自定义拦截器的配置也十分方便。

    <interceptors>
                       <!-- 声明拦截器 -->
                       <interceptor name="errorInterceptor" class="cn.itcast.util.ErrorInterceptor" />
                       <!-- 配置拦截器栈 -->
                       <interceptor-stack name="myErrorInterceptor">
                                   <interceptor-ref name="defaultStack" />
                                   <interceptor-ref name="errorInterceptor" />
                       </interceptor-stack>
    </interceptors>
    <!-- 覆盖底层的拦截器栈 对包中的所有action都有效 -->
    <default-interceptor-ref name="myErrorInterceptor"/>
    
                <global-results>
                        <result name="errorMsg">/errorMsg.jsp</result>
                </global-results>
                <global-exception-mappings>
                        <exception-mapping result="errorMsg" exception="java.lang.Exception"></exception-mapping>
            </global-exception-mappings>
    

    Struts2的“错误页”
    经常使用ssh框架开发,会发现Struts2中各种报错页面的布局和显示信息很类似。这是因为Struts2提供了一个它自己的“错误页”,在org.apache.struts2.dispatcher包下。

    这里写图片描述

    <html>
    <head>
        <title>Struts Problem Report</title>
        <style>
            pre {
                margin: 0;
                padding: 0;
            }    
        </style>
    </head>
    <body>
        <h2>Struts Problem Report</h2>
        <p>
        Struts has detected an unhandled exception:
        </p>
    
    <#assign msgs = [] />
    <#list chain as ex>
        <#if ex.message??>
            <#assign msgs = [ex.message] + msgs/>
        </#if>    
    </#list>
    <#assign rootex = exception/>
    <#list chain as ex>
        <#if (ex.location?? && (ex.location != unknown))>
            <#assign rootloc = ex.location/>
            <#assign rootex = ex/>
        <#else>
                <#assign tmploc = locator.getLocation(ex) />
                <#if (tmploc != unknown)>
                <#assign rootloc = tmploc/>
                    <#assign rootex = ex/>
                </#if>  
        </#if>    
    </#list>
    
    <div id="exception-info">
    <table>
        <tr>
            <td><strong>Messages</strong>:</td>
            <td>
                <#if (msgs?size > 1)>
                <ol>
                    <#list msgs as msg>
                        <#if (msg?is_method)>
                            <li>${msg[0]}</li>
                        <#else>
                            <li>${msg}</li>
                        </#if>
                    </#list>
                </ol>
                <#elseif (msgs?size == 1)>
                    <#if (msgs[0]?is_method)>
                        <li>${msgs[0][0]}</li>
                    <#else>
                        <li>${msgs[0]}</li>
                    </#if>
                </#if>
            </td>
        </tr>
        <#if rootloc??>
        <tr>
            <td><strong>File</strong>:</td>
            <td>${rootloc.URI}</td>
        </tr>
        <tr>
            <td><strong>Line number</strong>:</td>
            <td>${rootloc.lineNumber}</td>
        </tr>
        <#if (rootloc.columnNumber >= 0)>
        <tr>
            <td><strong>Column number</strong>:</td>
            <td>${rootloc.columnNumber}</td>
        </tr>
        </#if>
        </#if>
    
    </table>
    </div>
    
    <#if rootloc??>
        <#assign snippet = rootloc.getSnippet(2) />
        <#if (snippet?size > 0)>
            <div id="snippet">
            <hr />
    
                <#list snippet as line>
                    <#if (line_index == 2)>
                        <#if (rootloc.columnNumber >= 3)>
                            <pre style="background:yellow">${(line[0..(rootloc.columnNumber-3)]?html)}<span style="background:red">${(line[(rootloc.columnNumber-2)]?html)}</span><#if ((rootloc.columnNumber)<line.length())>${(line[(rootloc.columnNumber-1)..]?html)}</#if></pre>
                        <#else>
                            <pre style="background:yellow">${line?html}</pre>
                        </#if>    
                    <#else>
                        <pre>${line?html}</pre>
                    </#if>    
                </#list>
            </div>
        </#if>    
    </#if>
    
    <div id="stacktraces">
    <hr />
    <h3>Stacktraces</h3>
    <#list chain as ex>
    <div class="stacktrace" style="padding-left: ${ex_index * 2}em">
        <strong>${ex}</strong>
        <div>
        <pre>
        <#list ex.stackTrace as frame>
        ${frame}
        </#list>
        </pre>
        </div>
    </div>
    </#list>
    </div>
    
    <div class="footer">
    <hr />
    <p>
    You are seeing this page because development mode is enabled.  Development mode, or devMode, enables extra
    debugging behaviors and reports to assist developers.  To disable this mode, set:
    <pre>
      struts.devMode=false
    </pre>
    in your <code>WEB-INF/classes/struts.properties</code> file.
    </p>
    </div>
    </body>
    </html>
    
  • 相关阅读:
    Hadoop概述与配置
    组件间通讯及钩子函数
    react组件
    JSX
    Node.js二分查找
    链表
    微信支付V3
    在有序数组中查找某一特定元素的索引
    给定一个只包括 ‘(‘,‘)‘,‘{‘,‘}‘,‘[‘,‘]‘ 的字符串,判断字符串是否有效。
    我的前半生
  • 原文地址:https://www.cnblogs.com/saixing/p/6730243.html
Copyright © 2011-2022 走看看