2012-07-11 137 views
0

我剛剛在link之前看過這篇文章。通過ajax的JSF2導航

我試圖建立一個基於此的示例項目。只需用jsf commandButton替換primeface按鈕即可。請看下圖:

的template.xhtml

<html xmlns="http://www.w3.org/1999/xhtml" 
    xmlns:h="http://java.sun.com/jsf/html" 
    xmlns:f="http://java.sun.com/jsf/core" 
    xmlns:ui="http://java.sun.com/jsf/facelets"> 

<h:head> 
    <!-- Links to CSS stylesheets... --> 
    <title>title</title> 
</h:head> 

<h:body> 
    <div id="wrapper"> 
     <div id="header">header</div> 
     <script>alert("test");</script> 
     <h:panelGroup id="content"> 
      <ui:insert name="content"> 
       Sample content. 
      </ui:insert> 
     </h:panelGroup> 
    </div> 

    <div id="footer">footer</div> 
</h:body> 

page1.xhtml看起來是這樣的:

<ui:composition xmlns:ui="http://java.sun.com/jsf/facelets" 
      xmlns:h="http://java.sun.com/jsf/html" 
      xmlns:f="http://java.sun.com/jsf/core" 
      template="./template.xhtml"> 

<ui:define name="content"> 
    <h2>Page 1</h2> 

    <h:form> 
     <p>bla</p> 
     <h:commandButton value="page2" action="page2"> 
      <f:ajax render=":content"></f:ajax> 
     </h:commandButton> 
    </h:form> 
</ui:define> 

終於page2.xhtml看起來像這樣:

<ui:composition xmlns:ui="http://java.sun.com/jsf/facelets" 
      xmlns:h="http://java.sun.com/jsf/html" 
      xmlns:f="http://java.sun.com/jsf/core" 
      template="./template.xhtml"> 

<ui:define name="content"> 
    <h2>Page 2</h2> 

    <h:form> 
     <p>blabla</p> 
     <h:commandButton value="page1" action="page1"> 
      <f:ajax render=":content"></f:ajax> 
     </h:commandButton> 
    </h:form> 
</ui:define> 

正如你所看到的,我在template.xhtml上有一個簡短的javascript。 我現在想要從page1更改爲page2時不調用此JavaScript。 所以我想用這個ajax導航只是內容部分更新和腳本不會調用頁面更改。

但是,當我點擊commandButton腳本將被執行。 我看了一下螢火蟲,看到洞體部分更新了,不僅內容div。

我做錯了什麼?使用這種模板技術可以僅在內容部分進行導航嗎?還是我必須尋找另一種方法來解決這個問題?

我使用的是tomcat和myfaces。

在此先感謝!

+0

我再次檢查了螢火蟲輸出鋸,服務器在頁面上的響應變化是完整的HTML網站。所以它似乎是,阿賈克斯根本不起作用。 – Jurek 2012-07-11 13:59:40

回答

0

我不認爲這是可能的。如果您將視圖從page1更改爲page2,則基本上page2是從模板開始構建的。

如果您希望腳本爲page1運行,那麼將其包含在插入到模板中的page1的內容中,並且不要將其包含在page2中,這樣您只需在不需要的情況下調用腳本主模板。

+0

感謝您的提示。但該腳本僅用於指示id爲「content」的panelGroup外部的代碼被重新加載。我想要一個在頁面上加載的應用程序,只有通過ajax的內容部分。你有這個方法嗎? – Jurek 2012-07-13 10:59:46