接上一篇:FastJson
Controller中的每个Handler自己处理异常
public User2 test3(){
User2 user2 = null;
try {
user2 = new User2(1,null,null,new Date(),22222.38);
System.out.println(user2);
//调用Servlet
} catch (Exception1 e) {
e.printStackTrace();
return "redirect:/xx/error1";
} catch (Exception2 e){
e.printStackTrace();
return "redirect:/xx/error2";
}
return user2;
}
public User2 test4(){
User2 user2 = null;
try {
user2 = new User2(2,null,null,new Date(),2333.18);
System.out.println(user2);
//调用Servlet
} catch (Exception1 e) {
e.printStackTrace();
return "redirect:/xx/error1";
} catch (Exception2 e){
e.printStackTrace();
return "redirect:/xx/error2";
}
return user2;
}
此种处理方案,异常处理逻辑,分散在各个handler中,不利于集中管理
Controller中的每一个Handler不再自己处理异常,而是直接throws所有异常,定义在一个”异常解析器“集中捕获处理所有异常
2. resolver包下定义异常解析器:MyExceptionResolver
3. 其他新建如下:
//异常解析器
//任何一个Handler中抛出异常时,都会被该异常处理器捕获处理,Handler就是controller里面的方法
public class MyExceptionResolver implements HandlerExceptionResolver {
@Override
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
ModelAndView modelAndView = new ModelAndView();
if (ex instanceof MyException1) {
modelAndView.setViewName("redirect:/error1.jsp");
} else if (ex instanceof MyException2) {
modelAndView.setViewName("redirect:/error2.jsp");
} else if (ex instanceof MyException3) {
modelAndView.setViewName("redirect:/error3.jsp");
}
return modelAndView;
}
}
<bean class="com.lyx.resolver.MyExceptionResolver">bean>
@RequestMapping("/ex")
@Controller
public class ExController {
@RequestMapping("/test")
public String test(Integer id) throws Exception {
if(id==1){
throw new MyException1("testex1");
}
return "success";
}
}
@Controller
@RequestMapping("/login")
@SessionAttributes("state")
public class LoginController {
@RequestMapping("/save")
public String save(Model model){
//登录后留取状态标记
model.addAttribute("state","ok");
return "index";
}
}
public class MyInterceptor1 implements HandlerInterceptor{
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
return false;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
}
}
package com.lyx.interceptor;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class MyInterceptor1 implements HandlerInterceptor{
//在handler之前执行
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//判断登录状态
HttpSession session = request.getSession();
if(session.getAttribute("state")!=null){
return true;
}
//中断之后,响应请求
response.sendRedirect("/login.jsp");
return false;
}
//在handler之后执行,响应之前执行
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
}
//在视图渲染之后执行
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
}
}
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/login/test2"/>
<mvc:mapping path="/login/test3"/>
<bean class="com.lyx.interceptor.MyInterceptor1">
bean>
mvc:interceptor>
mvc:interceptors>
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/login/*"/>
<mvc:mapping path="/login/**"/>
<mvc:mapping path="/login/test*"/>
<mvc:exclude-mapping path="/login/save"/>
<bean class="com.lyx.interceptor.MyInterceptor1">
bean>
mvc:interceptor>
mvc:interceptors>