2016-11-15 84 views
1

我在我的Spring Web模型 - 视图 - 控制器(MVC)框架中有这个类。春天的Web模型 - 视图 - 控制器(MVC)架构的版本是3.2.8Spring MVC:管理异常

我有这个控制器

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) 

这在网络上。 xml

<error-page> 
       <exception-type>com.tdk.iop.domain.security.ApplicationException</exception-type> 
       <location>/errors/error500.do</location> 
     </error-page> 

但是应用程序没有达到Error500Controller

+0

我觉得你的问题是连接方法签名。你配置* spring-mvc *来处理* ApplicationException *,但是你的方法抛出了一个更通用的* Exception *。也许你必须改进* spring-mvc *配置。 –

+0

您必须在Spring配置xml中添加SimpleMappingExceptionHandler bean。点击此链接https://www.mkyong.com/spring-mvc/spring-mvc-exception-handling-example/ – Hiren

回答

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); 

    } 
}