Spring 3.1如何将所有异常发送到一个页面?

时间:2012-10-03 17:37:10

标签: java spring exception-handling error-handling

我有以下

的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


}

问题: 如何获得任何和所有内部服务器错误&amp;将异常/错误消息显示到单个页面的异常?

2 个答案:

答案 0 :(得分: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
}

答案 1 :(得分:3)

如果你想处理来自所有控制器的异常,你真的应该扩展SimpleMappingExceptionResolver,或者AbstractHandlerExceptionResolver,并在容器中配置它{{3} }。

这将阻止您(或在代码中工作的其他人)将所有控制器与一个地方一起子类化以处理异常。

使用注释和超类将起作用,但注释似乎更多地针对每个控制器使用。

此外,这两个建议仅适用于从控制器方法抛出的异常。

如果您担心.jsp文件中的异常,here应提供补充解决方案。

以上都不是web.xml错误页面配置的替代,因为它们的范围不同。对于讨论this post似乎是一个很好的起点。