2016-05-03 74 views
0

我需要有人修改URL將用戶重定向到默認的錯誤頁面春天

防爆用戶重定向到一個特定的錯誤頁面: - 如果有人從http://localhost:8085/PSST/PSS URL更改爲http://localhost:8085/DLMS_Client/PS那麼應用程序應該將用戶重定向到一個默認的錯誤頁面。

我很努力的解決了過去幾天的問題。

任何幫助將appriciated。

在此先感謝。

回答

0

因此,您可以使用Spring的SimpleExceptionMappingResolver。您需要設置你的projet-servlet.xml

<!-- ExceptionResolverMapping: generate exception specific view --> 

    <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver"> 
     <property name="exceptionMappings"> 
      <props> 
       <prop key="com.rail.eseva.exception.UnexpectedException">/unexpectedExceptionView</prop> 
      </props> 
     </property> 
     <property name="defaultErrorView" value="/genericExceptionView"/> 
    </bean> 

創建一個自定義異常類喜歡 -

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


    private String message; 
    private Date date; 
    public UnexpectedException(String message, Date date) { 
     this.date=date; 
     this.message=message; 
    } 
    /** 
    * @return the message 
    */ 
    @Override 
    public String getMessage() { 
     return message; 
    } 
    /** 
    * @return the date 
    */ 
    public Date getDate() { 
     return date; 
    } 

    @Override 
    public String toString() { 
     return message +" "+date.getTime(); 
    } 
} 

和通用除外圖頁

<body> 

    <h2>Exception occured at: <fmt:formatDate value="${exception.date}" pattern="dd.MM.yyyy"/></h2> 
    <h2>Exception message :</h2> ${exception.message} 

</body> 

現在你拋出此異常,根據您的要求:

throw new UnexpectedException(e.getMessage(), new Date()); 
+0

嗨,謝謝你的回覆。在我的情況下,如果有人修改網址,那麼如何顯示該默認錯誤頁面。在這種情況下,控制器名稱將找不到。然後我需要顯示默認的錯誤頁面。 – suneel

+0

如果這不是一個有效的URL,那麼Page將不會被顯示(瀏覽器自己的功能),否則如果這是有效的URL並且您不想處理它,那麼您可以處理控制器中的邏輯。 –

相關問題