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>
posted @ 2015-03-20 01:30  shenming  阅读(170)  评论(0编辑  收藏  举报