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>
  • 相关阅读:
    前端插件集合
    建立controller
    W3C对DOM2.0定义的标准事件
    事件代理和委托学习
    css3属性flex弹性布局设置三列(四列)分布样式
    css+html 关于文本的总结(整理中)
    jquery阻止事件冒泡的3种方式
    web前端打印总结
    前端打印插件
    object实现小老鼠交互
  • 原文地址:https://www.cnblogs.com/shenming/p/4352549.html
Copyright © 2011-2022 走看看