Spring MVC:管理异常

时间:2016-11-15 12:49:54

标签: java spring spring-mvc

我的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

1 个答案:

答案 0 :(得分:0)

可以使用 @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);

    }
}