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