我希望以下表单使用AJAX.因此,在单击命令按钮并且不重新加载页面后,将显示注释.使用Java Server Faces 2.0需要更改什么?
功能:此表单提供inputText来定义主题.按下commandButton后,将搜索有关此主题的注释.评论显示在dataTable中,如果有的话.否则显示Empty.
<h:form id="myForm">
<h:outputLabel value="Topic:" for="topic" />
<h:inputText id="topic" value="#{commentManager.topic}" />
<h:commandButton value="read" action="#{commentManager.findByTopic}" />
<h:panelGroup rendered="#{empty commentManager.comments}">
<h:outputText value="Empty" />
</h:panelGroup>
<h:dataTable
id="comments"
value="#{commentManager.comments}"
var="comment"
rendered="#{not empty commentManager.comments}"
>
<h:column>
<h:outputText value="#{comment.content}"/>
</h:column>
</h:dataTable>
</h:form>
Run Code Online (Sandbox Code Playgroud)
Bal*_*usC 21
您需要告诉命令按钮使用Ajax.它就像在其中嵌套<f:ajax>标签一样简单.您需要指示它通过提交整个表单execute="@form"并渲染ID的元素comments通过render="comments".
<h:commandButton value="read" action="#{commentManager.findByTopic}">
<f:ajax execute="@form" render="comments" />
</h:commandButton>
Run Code Online (Sandbox Code Playgroud)
不要忘记确保在主模板中使用<h:head>而不是a <head>,以便自动包含必要的JSF ajax JavaScripts.
<h:head>
...
</h:head>
Run Code Online (Sandbox Code Playgroud)
另外,与ID的元素comments需要被已经呈现给由JSF客户端,以便能够被由JavaScript/Ajax的再次更新(重新呈现).所以,最好是把<h:dataTable>一个<h:panelGroup>与该ID.
<h:panelGroup id="comments">
<h:dataTable rendered="#{not empty commentManager.comments}">
...
</h:dataTable>
</h:panelGroup>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
22102 次 |
| 最近记录: |