使用AJAX的JSF表单帖子
我希望以下表单使用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>
您需要告诉命令按钮使用Ajax.它就像在其中嵌套
<f:ajax> 标签一样简单.您需要指示它通过execute =“@ form”提交整个表单,并通过render =“comments”呈现带有ID注释的元素.
<h:commandButton value="read" action="#{commentManager.findByTopic}"> <f:ajax execute="@form" render="comments" /> </h:commandButton> 不要忘记确保你有一个< h:head>而不是< head>在主模板中,以便自动包含必要的JSF ajax JavaScripts. <h:head> ... </h:head> 此外,具有ID注释的元素需要由JSF呈现给客户端,以便能够再次由JavaScript / Ajax更新(重新呈现).最好是把< h:dataTable>在< h:panelGroup>中有了这个ID. <h:panelGroup id="comments"> <h:dataTable rendered="#{not empty commentManager.comments}"> ... </h:dataTable> </h:panelGroup> 也可以看看: > Understanding PrimeFaces process/update and JSF f:ajax execute/render attributes (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |