2012-10-03 15 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似乎是一个很好的起点。

相关问题