今天在整理之前的项目的时候想着有的action层没有做异常处理,于是想着自定义拦截器处理一下未拦截的异常。
代码:
packagecn.xm.exam.action.safeHat;importjava.util.HashMap;importjava.util.Map;importorg.slf4j.Logger;importorg.slf4j.LoggerFactory;importorg.springframework.context.annotation.Scope;importorg.springframework.stereotype.Controller;importcom.opensymphony.xwork2.ActionSupport;
@Controller
@Scope("prototype")public class SafeHatAction extendsActionSupport {private static final Logger log = LoggerFactory.getLogger(SafeHatAction.class);private Mapresponse;publicSafeHatAction() {
log.info("SafeHatAction init.........");
}publicString test1() {
response= newHashMap();
response.put("ttt", "tttttt");returnSUCCESS;
}publicString test2() {
response= newHashMap();
response.put("ttt", "tttttt");int i = 1/0;returnSUCCESS;
}public MapgetResponse() {returnresponse;
}public void setResponse(Mapresponse) {this.response =response;
}
}
1.异常在Action层未做处理的情况:
由于在web.xml中定义了500错误的页面,因此跳转到500页面,如下:
500
/500.jsp
可是上面终究不够友好。
2.自定义拦截器处理全局异常
(