原文地址:http://my.oschina.net/lsw90/blog/63362
今天处理了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>
|
引用来自“tianhandigeng1498”的评论
对于没有的方法这样处理确实可以,但是如果有这个action,但是没有要访问的方法那应该这样处理呢,比如这样 :
public class ProductAction extends ActionSupport {
public String add() {
return INPUT;
}
}
现在我只有这个方法,但是我这样访问:
product!edit.action
这样你这样配置,会报no such method的错误,而且页面上也会把相应的错误信息显示出来,楼主对于这个问题,怎么解决,想一下吧,也可以加我q 874904507 一起讨论,我也没想出一个万全的方法
这一句就是处理你说的异常啊。。。