2012-10-03 18 views
4

我有以下Spring 3.1如何將所有異常發送到一個頁面?

的web.xml

<error-page> 
    <error-code>500</error-code> 
    <location>/WEB-INF/views/errorPages/500.jsp</location> 
</error-page> 

<error-page> 
    <exception-type>java.lang.Exception</exception-type> 
    <location>/WEB-INF/views/errorPages/error.jsp</location> 
</error-page> 

<error-page> 
    <exception-type>java.lang.Throwable</exception-type> 
    <location>/WEB-INF/views/errorPages/500.jsp</location> 
</error-page> 

彈簧的context.xml

<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> 
    <property name="exceptionMappings"> 
    <props> 
     <prop key="java.lang.Exception">error</prop> 
    </props> 
    </property> 
</bean> 

<bean id="exceptionResolver" 
    class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> 

    <property name="exceptionMappings"> 
     <props> 
      <prop key="com.company.server.exception.GenericException">GenericExceptionPage</prop> 
      <prop key="java.lang.Exception">error</prop> 
     </props> 
    </property> 

    <property name="defaultErrorView" value="defaulterror"></property> 
</bean> 

<bean id="viewResolver" 
    class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
    <property name="prefix"> 
     <value>/WEB-INF/views/</value> 
    </property> 
    <property name="suffix"> 
     <value>.jsp</value> 
    </property> 
</bean> 

GenericException.java

public class GenericException extends RuntimeException 
{ 
    private static final long serialVersionUID = 1L; 

    private String customMsg; 
    public String message; 

    public String getCustomMsg() 
    { 
    return customMsg; 
    } 

    public void setCustomMsg(String customMsg) 
    { 
    this.customMsg = customMsg; 
    } 

    public GenericException(String customMsg) 
    { 
    this.customMsg = customMsg; 
    } 

    @Override 
    public String getMessage() 
    { 
    return message; 
    } 

} 

myController.java

@Controller 
@RequestMapping(value = "/admin/store") 
public class AdminController 
{ 


//bunch of restful drivin requestMappings 


} 

問題: 我如何獲得任何及所有內部服務器錯誤&異常顯示異常/錯誤信息單頁?

+0

您是否檢查過[this](http://forum.springsource.org/archive/index.php/t-77927.html)? –

回答

11

我會考慮在我的控制器的方法中使用@ExceptionHandler註釋。這個註釋標記了一個方法,當一個Exception冒泡通過控制器時,它將被Spring調用。

這樣,當你的@RequestMapping方法之一拋出一個Exception,那麼這個方法將被調用,並且可以返回你想要的任何錯誤信息。

public class BaseController { 
    @ExceptionHandler(Throwable.class) 
    public String handleException(Throwable t) { 
     return "redirect:/errorPages/500.jsp"; 
    } 

    @ExceptionHandler(Exception.class) 
    public String handleException(Throwable t) { 
     return "redirect:/errorPages/error.jsp"; 
    } 
} 

@Controller 
@RequestMapping(value = "/admin/store") 
public class AdminController extends BaseController 
{ 
    @RequestMapping(...) 
    //Some method 

    @RequestMapping(...) 
    //Another method 
} 
+0

鑑於您希望在整個應用程序中捕獲所有異常,你需要爲每個正在使用的控制器創建一個@ExceptionHandler嗎? – stackoverflow

+1

您可以使用通用基類來保存'@ ExceptionHandler'註釋的方法。 –

+0

我對Spring有點新鮮。你能提供一個通用的例子,或者將我指向其他人做這件事的例子。非常感謝nicholas.hauschild – stackoverflow

3

如果要處理來自所有你的控制器你真的應該延伸SimpleMappingExceptionResolver,或者AbstractHandlerExceptionResolver並提到here在容器中進行配置例外。

這會阻止您(或其他在代碼中工作的人)必須繼承所有控制器以及單個地方來處理異常。

使用註釋和超類將會工作,但註釋似乎更多地針對每個控制器的使用。

此外,這兩個建議僅適用於控制器方法拋出的異常。

如果您擔心.jsp文件中的異常,應該給出一個補充解決方案,而不是this post

以上都不是web.xml錯誤頁面配置的替代品,因爲它們的範圍不一樣。討論this post似乎是一個很好的起點。

相關問題