2011-12-13 60 views
0
<html xmlns="http://www.w3.org/1999/xhtml" 
    xmlns:h="http://java.sun.com/jsf/html" 
    xmlns:ui="http://java.sun.com/jsf/facelets"> 

<h:body> 
    <title><ui:define name="title">Page 2</ui:define></title> 
    <ui:composition template="template/common/commonLayout.xhtml"> 
     <ui:define name="content"> 
      This is the content of Page 2 page<br/> 
      <h:form prependId="false"> 
       <h:commandButton id="goToIndex" value="Go to Index" action="index" /> 
      </h:form> 
      <a href="index.xhtml">Index</a> 
     </ui:define> 

    </ui:composition> 

</h:body> 
</html> 

我有兩個相同的facelets頁面:index.xhtml和page2.xhtml,它們相互鏈接。我也有一個模板文件。除了標題,內容文本和命令按鈕值中的頁面名稱之外,索引和page2之間的代碼沒有區別。<a href>鏈接導航不檢測JSF 2.0中的模板Facelets頁面

當我點擊由表單實現的goToIndex按鈕導航到index.xhtml時,一切都按預期工作:它會轉到index.xhtml。但是,當我點擊[a href]鏈接實現的鏈接導航到index.xhtml時,它會轉到index.xhtml,但似乎忽略了頁面的所有模板設置,包括任何表單標記。呈現的唯一內容是限制在「content」定義中的文本,但沒有任何css格式。

這件事發生在雙方。 「index - > page2」和「pag2 - > index」

任何想法爲什麼會出現這種情況?

回答

3

<a href ...>調用的頁面不會被faces servlet處理,因此無法正確轉換並且不包含css/js。

查看您的web.xml並檢查face servlet是如何映射的。在那裏,你會發現這樣的:

<servlet-mapping> 
    <servlet-name>Faces Servlet</servlet-name> 
    <url-pattern>/faces/*</url-pattern> 
</servlet-mapping> 

您可以將模式改變爲:

<url-pattern>*.xhtml</url-pattern> 

然後使用XHTML前綴的所有文件將被面臨的servlet進行處理。但是,如果在項目中將xhtml前綴用於facelet之外的其他用途,則可能會導致問題。

另一種方法是使用h:link,而不是a:href

<h:link value="Index" outcome="index" > 

在結果屬性接受目標頁面沒有.xhtml。

+0

謝謝,馬特。它像一個魅力。 –