zoukankan      html  css  js  c++  java
  • struts2异常处理

    今天处理了struts2 的异常,跟大家分享下:

    1.处理不存在的Action:

    只需在struts.xml中加

    1
    2
    <default-action-ref name="defaultAction" />
    <action name="defaultAction" class="com.lsw.permission.action.DefaultAction" />

    2.处理其他异常(如空指针,不存在的方法...),我们一般会定义全局异常及全局Result:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    <global-results><!-- 定义全局Result -->
        <result name="redirect" type="redirect">${returnPageURL}</result>
        <result name="dispatcher" type="dispatcher">${returnPageURL}</result>
        <result name="login" type="dispatcher">/login.jsp</result>
        <result name="exceptionError" type="dispatcher">/WEB-INF/jsp/error/exception.jsp</result>
    </global-results>
     
    <global-exception-mappings><!-- 全局异常处理 -->
        <exception-mapping result="exceptionError" exception="java.lang.NullPointerException" />
        <exception-mapping result="exceptionError" exception="java.lang.NoSuchMethodException" />
        <exception-mapping result="exceptionError" exception="java.lang.Exception" />
    </global-exception-mappings>

     3.处理不存在的页面,如不存在的JSP,html,htm等页面(404异常),struts是不能处理这个异常的,还有其他异常(如500,401等等)都交给tomcat来处理,只需在web.xml中加如下配置即可:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    <!-- 处理不存在的页面 -->
    <error-page>
        <error-code>404</error-code>
        <location>/WEB-INF/jsp/error/404.jsp</location>
    </error-page>
    <!-- 处理500异常 -->
    <error-page>
        <error-code>500</error-code>
        <location>/WEB-INF/jsp/error/500.jsp</location>
    </error-page>
  • 相关阅读:
    ZOJ 2859 Matrix Searching
    URAL 1102. Strange Dialog
    ZOJ 1986 Bridging Signals
    POJ 3233 Matrix Power Series
    POJ 1836 Alignment
    POJ 3267 The Cow Lexicon
    ZOJ 3471 Most Powerful
    IIS:HTTP 错误 403.9 禁止访问:连接的用户过多
    使用Command对象执行数据库操作
    C#类型转换
  • 原文地址:https://www.cnblogs.com/shenming/p/4352549.html
Copyright © 2011-2022 走看看