2017-02-21 83 views
1

如果我指定一個錯誤頁面是這樣的:如何在web.xml中指定'Not found'錯誤頁面,以便url也被更改?

<error-page> 
    <error-code>404</error-code> 
    <location>/pages/not-found.html</location> 
</error-page> 

,並觸及URL localhost:8080/app/something/else(返回404錯誤),然後在錯誤頁面的內容,在瀏覽器上傾倒在瀏覽器的URL仍然localhost:8080/app/something/else。這裏的問題是,如果我指定任何像這樣的CSS資源:

<link type="text/css" rel="stylesheet" href="resources/css/bootstrap.css" /> 

然後,瀏覽器嘗試加載localhost:8080/app/something/else/resources/css/bootstrap.css,而不是localhost:8080/app/resources/css/bootstrap.css,這顯然會失敗。我們也不能使用

<link type="text/css" rel="stylesheet" href="/resources/css/bootstrap.css" />

的瀏覽器,然後將加載localhost:8080/resources/css/bootstrap.css

的基本問題是相對路徑依賴有在URL中斜槓的數量。

在我看來,如果我能重定向的頁面/pages/not-found.html,那麼我可以指定CSS文件(的一貫路徑,而不必擔心斜線的數量URL)

回答

0

如果你是在談論一個JSF應用中也許你可以使用的PhaseListener:

在的PhaseListener可以讓JSF做其工作,趕上java.io.FileNotFoundException。萬一出現異常時,您可以將用戶轉發到特定的網站:

FacesContext context = event.getFacesContext(); 
context.getExternalContext().redirect("/pages/not-found.html"); 
0

你應該爲CSS文件

<h:outputStylesheet library="css" name="bootstrap.css"/> 

使用像 和JavaScript文件

<h:outputScript library="js" name="bootstrap.js"/> 

或如果你想在你使用

<link type="text/css" rel="stylesheet" href="#{request.contextPath}/resources/css/bootstrap.css" /> 

但是最好先使用它

相關問題