2016-11-15 78 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); 

    } 
}