2016-11-15 7 views
1

私はSpring Web Model-View-Controller(MVC)フレームワークでこのクラスを持っています。春のWebモデル・ビュー・コントローラ(MVC)フレームワークのバージョンは、私は、ウェブ上でこの例外Spring MVC:例外を管理する

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) 

これをスローこのコントローラ

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


.. 
} 

を有する3.2.8

あります。 xml

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

しかし、アプリケーションはError500Controller

+0

私はあなたの問題は、メソッドのシグネチャに接続されていると思います。あなたは* ApplicationException *を処理するために* spring-mvc *を設定しますが、あなたのメソッドはより汎用的な* Exception *を投げます。おそらく、あなたは* spring-mvc *設定を改良する必要があります。 –

+0

SimpleMappingExceptionHandler beanをバネ設定XMLに追加する必要があります。このリンクを確認してくださいhttps://www.mkyong.com/spring-mvc/spring-mvc-exception-handling-example/ – Hiren

答えて

0

この種の例外は、@ExceptionHandlerで処理できます。 の場合

@ExceptionHandler(SameEmailException.class) // UserNotFoundException.java 
    public ModelAndView handleException(SameEmailException e) { 
     ModelAndView modelAndView = new ModelAndView("errorView"); 
     modelAndView.addObject("errorMessage", e.getMessage()); 
     return modelAndView; 
    } 

はこのSameEmailExceptionを傍受するメソッドを作成し、右上記の方法の後に続いて

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"); 
     } 
.. 
} 

)あなたはusr.getNumberOfUserWithSameEmail(経由同じ電子メールでユーザー数を取得することができたとし次に、メッセージ(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); 

    } 
} 
関連する問題