2016-06-15 5 views
0

私はMVCデザインパターンに完全に従ったプログラムを書いていますが、現在は例外をスローするメソッドがたくさんあります。すべての例外(MVC)を扱うクラスを持っていますか?

Iは等public void receiveRuntimeException(String message)receiveAnotherTypeOfException(...)ような方法を有することになるexceptionHandler呼ばコントローラでクラスを、追加の考え

次いでexceptionHandlerを参照(特に考慮して)、最もクラスを与えるたびにメソッドは例外をスローします。

try{ 
    methodThatWillThrowAwfulException() 
}catch(AwfulException e){ 
    exceptionHandler.receiveAwfulException("methodThatWillThrowAwfulException threw awful exception") 
} 

これは良い方法ですか?そうでない場合は、MVCでどのように例外を処理する必要がありますか?

答えて

0

スプリングには@ControllerAdviceという概念があり、エラーハンドリング(@ExceptionHandlerと注釈付けされています)などのさまざまな側面を処理するためにクラスにアノテーションを付けることができます。自分でSpringを使用することはできません。これは非常に一般的なMVCフレームワークで採用されているパターンです。その静脈で

、あなたが必要なとしてとして多くの例外を処理するために必要なとしてとして多くの方法を作るために努力すべきです。オムニ例外を処理するメソッドを1つ作成しないでください。これは、エラーをできるだけわかりやすいものにするためです。

+0

また見てください:http://stackoverflow.com/questions/3551221/guidelines-on-exception-propagation-in-java?answertab=active#tab-top – Jaumzera

+0

私はコントローラのアドバイスをよく理解していません注釈、それは何をすべきか?私が質問に示した選択肢は問題ないと思いますか? – csTroubled

0

エラー処理しますか? 1つのコントローラ。 commonExceptionHandler?

(のcontext.xml)

<!-- Exception Resolver --> 
<beans:bean 
    class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> 
    <beans:property name="defaultErrorView" value="exception/default"></beans:property> 
    <beans:property name="warnLogCategory" value="log"></beans:property> 
</beans:bean> 

(web.xmlファイル)

<!-- error --> 
<error-page> 
    <error-code>301</error-code> 
    <location>/exception/default.html</location> 
</error-page> 
<error-page> 
    <error-code>304</error-code> 
    <location>/exception/default.html</location> 
</error-page> 
<error-page> 
    <error-code>307</error-code> 
    <location>/exception/default.html</location> 
</error-page> 
<error-page> 
    <error-code>401</error-code> 
    <location>/exception/default.html</location> 
</error-page> 
<error-page> 
    <error-code>402</error-code> 
    <location>/exception/default.html</location> 
</error-page> 
<error-page> 
    <error-code>403</error-code> 
    <location>/exception/default.html</location> 
</error-page> 
<error-page> 
    <error-code>404</error-code> 
    <location>/exception/default.html</location> 
</error-page> 
<error-page> 
    <error-code>405</error-code> 
    <location>/exception/default.html</location> 
</error-page> 
<error-page> 
    <error-code>406</error-code> 
    <location>/exception/default.html</location> 
</error-page> 
<error-page> 
    <error-code>415</error-code> 
    <location>/exception/default.html</location> 
</error-page> 
<error-page> 
    <error-code>429</error-code> 
    <location>/exception/default.html</location> 
</error-page> 
<error-page> 
    <error-code>500</error-code> 
    <location>/exception/default.html</location> 
</error-page> 
<error-page> 
    <error-code>501</error-code> 
    <location>/exception/default.html</location> 
</error-page> 
<error-page> 
    <error-code>503</error-code> 
    <location>/exception/default.html</location> 
</error-page> 

(ハンドラコントローラ)

@ControllerAdvice("you.project.package") 
public class CommonExceptionHandler { 

    private static final Logger logger = LoggerFactory.getLogger(CommonExceptionHandler.class); 

    @ExceptionHandler(Exception.class) 
    public ModelAndView defaultException(HttpServletRequest req, Exception exception){ 
     logger.error("Request: " + req.getRequestURL() + " raised " + exception); 

     ModelAndView mav = new ModelAndView(); 
     mav.setViewName("exception/default"); 

     return mav; 
    } 
} 

は、Googleで検索することができます。あなたがエラーとコードを表示するために来た後。

関連する問題