[英]Spring MVC: manage Exceptions
我在Spring Web模型 - 视图 - 控制器(MVC)框架中有这个类。 Spring Web模型 - 视图 - 控制器(MVC)框架的版本是3.2.8
我有这个控制器
public class WelcomeController extends ViewController {
@Autowired
private UserService userService;
@Autowired
private SessionHelper sessionHelper;
public ModelAndView handleRequest(final HttpServletRequest request, HttpServletResponse response) throws Exception {
sessionHelper.invalidate(request, new String[] { SubmitController.CONFIRMATION_INFO,
SubmitController.CONFIRMATION_INFO2,
SubmitController.CONFIRMATION_INFO3,
"changePrdStatus" });
HttpSession session = request.getSession();
DetailedUser ecasUser = (DetailedUser)request.getUserPrincipal();
User usr = userService.getUserFromLDAP(ecasUser);
..
}
抛出此异常
com.tdk.iop.domain.security.ApplicationException: more than 1 user with the same email
at com.tdk.iop.dao.impl.UserDaoImpl.findByEmail(UserDaoImpl.java:195)
at com.tdk.iop.services.impl.ServiceSupport.getEcasUser(ServiceSupport.java:46)
at com.tdk.iop.services.impl.UserServiceImpl.getUserFromEcas(UserServiceImpl.java:37)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:183)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150)
at org.springframework.aop.framework.adapter.MethodBeforeAdviceInterceptor.invoke(MethodBeforeAdviceInterceptor.java:51)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:96)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:260)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:94)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:91)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
at com.sun.proxy.$Proxy596.getUserFromEcas(Unknown Source)
at com.tdk.iop.controller.welcome.WelcomeController.handleRequest(WelcomeController.java:64)
at org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:48)
这个在web.xml上
<error-page>
<exception-type>com.tdk.iop.domain.security.ApplicationException</exception-type>
<location>/errors/error500.do</location>
</error-page>
但是应用程序没有到达Error500Controller
可以使用@ExceptionHandler处理这种异常。 例如,假设您可以通过usr.getNumberOfUserWithSameEmail()获取具有相同电子邮件的用户数量
public ModelAndView handleRequest(final HttpServletRequest request, HttpServletResponse response) throws Exception {
sessionHelper.invalidate(request, new String[] { SubmitController.CONFIRMATION_INFO,
SubmitController.CONFIRMATION_INFO2,
SubmitController.CONFIRMATION_INFO3,
"changePrdStatus" });
HttpSession session = request.getSession();
DetailedUser ecasUser = (DetailedUser)request.getUserPrincipal();
User usr = userService.getUserFromLDAP(ecasUser);
Integer numOfUser = usr.getNumberOfUserWithSameEmail();
if(numOfUser>1){
throw new SameEmailException("more than 1 user with the same email");
}
..
}
然后在上面的方法之后,创建了一个拦截此SameEmailException
:
@ExceptionHandler(SameEmailException.class) // UserNotFoundException.java
public ModelAndView handleException(SameEmailException e) {
ModelAndView modelAndView = new ModelAndView("errorView");
modelAndView.addObject("errorMessage", e.getMessage());
return modelAndView;
}
然后创建一个将显示消息的视图(errorView.jsp):
<%@ page contentType="text/html; charset=ISO-8859-1" %>
<html>
<head>
<title>Error! </title>
</head>
<body>
<h2>${errorMessage}</h2>
</body>
</html>
然后为SameEmailException
创建单独的类:
public class SameEmailException extends Exception {
public SameEmailException (String message) {
super(message);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.