2011-11-17 77 views
0

下面是我的Spring配置文件:的SimpleMappingExceptionResolver不解決404

<bean class="com.web.handler.CustomSimpleMappingExceptionResolver" > 
    <property name="exceptionMappings"> 
     <props>    
      <prop key="java.lang.Throwable">error</prop> 
     </props> 
    </property> 
</bean> 

CustomSimpleMappingExceptionResolver

public class CustomSimpleMappingExceptionResolver extends SimpleMappingExceptionResolver{ 
    @Override 
    public ModelAndView resolveException(HttpServletRequest request, 
      HttpServletResponse response, Object handler, Exception ex) { 

    if(int a = 1) 
     return new ModelAndView("ViewName1"); 
    else 
     return new ModelAndView("ViewName2"); 
    } 

web.xml沒有錯誤頁面。我正在尋找根據我的邏輯resolveException()顯示不同的觀點。

resolveException()CustomSimpleMappingExceptionResolver類不被稱爲404

回答

1

設置錯誤頁面

<error-page> 
    <error-code>404</error-code> 
    <location>/error.html</location> 
</error-page> 

您的錯誤頁面,只要它打開重定向。

<html> 
    <head> 
    <title>Your Page Title</title> 
    <meta http-equiv="REFRESH" content="0;url=error.htm"> 
    </head> 
    <body> 
    </body> 
</html> 

應該有你的控制器來處理error.htm請求的請求映射。

@RequestMapping(value={"/error.htm"}) 
    ModelAndView routToErrorHandler(HttpServletRequest request, HttpServletResponse response) { 
//any logic for your themes 
} 
+0

重定向給出了一個HTTP狀態代碼404。春天是否也這樣做? – pshirishreddy

+0

檢查您的網址是否正確,如html或htm。 –

1

的聲明可能是不正確的情況下, use a map而不是屬性。

<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> 
<property name="exceptionMappings"> 
    <map> 
    <entry key="DataAccessException" value="data-error" /> 
    <entry key="com.stuff.MyAppRuntimeException" value="app-unchecked-error" /> 
    <entry key="com.stuff.MyAppCheckedException" value="app-checked-error" /> 
    </map> 
</property> 
<property name="defaultErrorView" value="general-error"/> 
</bean> 

而且,我不知道的SimpleMappingExceptionResolver處理找到一個處理程序,而是它處理從內部處理程序引發的錯誤時引發的錯誤。也就是說,我不確定404可以這樣捕捉。

如果你把一個錯誤處理程序在web.xml中,將返回到servlet,你可以處理它,你喜歡的任何方式。在web.xml