Primefaces 3.3.1(Glassfish 3.0.1で実行)を使用するWebアプリがあり、ユーザーはメニューで目的のコンテンツを選択できます。ページの一部だけが更新されるので、AJAXで部分的なページ更新を使用したいと思います。URLを格納するメソッドは、commandButtonのactionListener属性で提供されますが、更新する必要のあるコンポーネントのIDは、commandButtonのupdate属性で提供されます。
最初にサンプルのソースコードを添付します。これは、PPRに関して別のスレッドから取得したものですが、機能していることが確認されています。
main.xhtml
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui"
xmlns:ui="http://java.sun.com/jsf/facelets">
<h:body>
<h:panelGroup id="content" layout="block">
<ui:include src="#{navBean.activePage}" />
</h:panelGroup>
<h:form>
<p:commandButton value="next"
actionListener="#{navBean.next}"
update=":content" />
<p:commandButton value="back"
actionListener="#{navBean.back}"
update=":content" />
</h:form>
</h:body>
</html>
NavBean
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.ActionEvent;
@ManagedBean
@SessionScoped
public class NavBean {
private String activePage = "somepage.xhtml";
public String getActivePage() {
return activePage;
}
public void setActivePage(String activePage) {
this.activePage = activePage;
}
public void next(ActionEvent e) {
this.setActivePage("someotherpage.xhtml");
}
public void back(ActionEvent e) {
this.setActivePage("somepage.xhtml");
}
}
含まれているページのサンプル( "somepage.xhtml")
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets">
<h2>Just some content for testing purposes</h2>
</ui:composition>
さて、問題は、それgetActivePage
が常に前に呼び出されることですsetActivePage
。これは、新しいコンテンツを取得するためにボタンを2回クリックする必要があるため、明らかに問題です。
私はここで重要な何かを見逃しているように感じますが、Google/stackoverflowで耳を傾けても適切な結果は得られませんでした。
更新:での置き換えは機能actionListener
しaction
ませんでした(もちろん、呼び出されたメソッドの入力パラメーターをそれに応じて変更しました)。
erencanが以下のコメントで示唆しているように、文字列のみを表示するため<ui:include>
にをに置き換えました。<h:outputText>
<h:outputText value="#{navBean.activePage}" />
私の予想に反して、これはうまく機能しています。では、なぜ機能<h:outputText>
するのに、<ui:include>
機能しないのでしょうか。JSFテンプレート機能を使用したいので、前者を使用することはできません。