使用AJAX的JSF表单帖子

前端之家收集整理的这篇文章主要介绍了使用AJAX的JSF表单帖子前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我希望以下表单使用AJAX.因此,在单击命令按钮并且不重新加载页面后,将显示注释.使用Java Server Faces 2.0需要更改什么?

功能:此表单提供inputText来定义主题.按下commandButton后,将搜索有关此主题的注释.注释显示在dataTable中,如果有的话.否则显示Empty.

  1. <h:form id="myForm">
  2. <h:outputLabel value="Topic:" for="topic" />
  3. <h:inputText id="topic" value="#{commentManager.topic}" />
  4. <h:commandButton value="read" action="#{commentManager.findByTopic}" />
  5. <h:panelGroup rendered="#{empty commentManager.comments}">
  6. <h:outputText value="Empty" />
  7. </h:panelGroup>
  8. <h:dataTable
  9. id="comments"
  10. value="#{commentManager.comments}"
  11. var="comment"
  12. rendered="#{not empty commentManager.comments}"
  13. >
  14. <h:column>
  15. <h:outputText value="#{comment.content}"/>
  16. </h:column>
  17. </h:dataTable>
  18. </h:form>
您需要告诉命令按钮使用Ajax.它就像在其中嵌套 <f:ajax>标签一样简单.您需要指示它通过execute =“@ form”提交整个表单,并通过render =“comments”呈现带有ID注释的元素.
  1. <h:commandButton value="read" action="#{commentManager.findByTopic}">
  2. <f:ajax execute="@form" render="comments" />
  3. </h:commandButton>

不要忘记确保你有一个< h:head>而不是< head>在主模板中,以便自动包含必要的JSF ajax JavaScripts.

  1. <h:head>
  2. ...
  3. </h:head>

此外,具有ID注释的元素需要由JSF呈现给客户端,以便能够再次由JavaScript / Ajax更新(重新呈现).最好是把< h:dataTable>在< h:panelGroup>中有了这个ID.

  1. <h:panelGroup id="comments">
  2. <h:dataTable rendered="#{not empty commentManager.comments}">
  3. ...
  4. </h:dataTable>
  5. </h:panelGroup>

也可以看看:

> Understanding PrimeFaces process/update and JSF f:ajax execute/render attributes
> How to find out client ID of component for ajax update/render? Cannot find component with expression “foo” referenced from “bar”

猜你在找的Ajax相关文章