我已经找到this话题,哪里还有一点code。使用Ajax来加载一个div。 我试图建立这样的例子,但没有成功。
I have found this topic, where's a bit of code to reload a div using Ajax. I've tried creating such example, but not successfully.
下面是我的code:
的index.xhtml
index.xhtml
<h:form>
<f:ajax render=":content">
<h:commandButton value="News" action="#{navigationBean.requestPage}">
<f:param name="requestedPage" value="news.xhtml"/>
</h:commandButton>
</f:ajax>
<br/><br/>
<f:ajax render=":content">
<h:commandButton value="News" action="#{navigationBean.requestPage}">
<f:param name="requestedPage" value="games.xhtml"/>
</h:commandButton>
</f:ajax>
</h:form>
</h:panelGroup>
<h:panelGroup id="content" layout="block">
<ui:include src="#{navigationBean.viewedPage}"/>
</h:panelGroup>
支持bean
backing bean
@ManagedBean
@ViewScoped
public class NavigationBean
{
private String viewedPage;
public NavigationBean()
{
viewedPage = "news.xhtml";
}
public String getViewedPage()
{
return viewedPage;
}
public void setViewedPage(String viewedPage)
{
this.viewedPage = viewedPage;
}
public void requestPage()
{
Map<String,String> map = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
viewedPage = map.get("requestedPage");
}
}
它的外观:
how it looks:
所以我缺少什么?可变viewedPage更新,正常的标签/文本等的更新,但无法获得该div的工作。
So what am I missing? Variable viewedPage is updated, normal labels/texts etc. are updated, but can't get this div working.
找到了解决办法:
<h:commandButton value="Games" action="#{navigationBean.requestPage}">
<f:param name="requestedPage" value="games.xhtml"/>
<f:ajax execute="@this" render="@none" />
<f:ajax render=":content"/>
</h:commandButton>
加改变@ViewScoped到@SessionScoped。
plus changing @ViewScoped to @SessionScoped.