JSF2 Richfaces 4.1.0 Ajax partial rendering of tree

北战南征 提交于 2019-12-23 18:44:49

问题


I have a big tree structure (almost 6000 nodes and growing) that I display in my webapp as a Richfaces rich:tree.

When a node is selected a handler-function in the backing-bean runs some logic and works some magic.

One thing that happens is that the tree is re-rendered with an Ajax-call on every selection in the tree. This means that over 2MB is POSTed to the server every time the selection changes.

As you might guess my problem here is that this is very slow. What I would like to do is only have the tree-node that was selected re-rendered and not the entire tree since this would hopefully just POST a couple of KB to the server.

This is my first real project in JSF2.0 using RichFaces 4.1.0 so I've googled like crazy for a solution and scavenged through the documentation on the RichFaces-site but have yet to find a solution.

My code looks like this:

<h:form id="main">
<a4j:outputPanel ajaxRendered="true">
<rich:panel id="treePanel" header="Tree">
<rich:tree id="theTree" var="tree" value="#{treeBean.rootNodes}" selectionType="ajax" toggleType="client" selectionChangeListener="#{treeBean.selectionChanged}">
<rich:treeNode>
<h:outputText styleClass="#{tree.selected?'selectedNode':''}" value="#{tree.title}" />
</rich:treeNode>
</rich:tree>
</rich:panel>
</a4j:outputPanel>
</h:form>

回答1:


JSF applications do not work like traditional Ajax implementations in that they have something called the ViewState which is basically a collection of all the necessary user control data, page state data, and other data that is passed back and forth to the server on each request and response. The reason for this is that the HTTP communication is inherently stateless, so the ViewState being given to the server on request tells the server about every change to the model and triggered server event that needs to occur. When the server has finished processing this request, it sends its built response along with the modified ViewState back to the client. This ViewState now contains information about what page elements need to be updated and refreshed visually by JSF javascripts.

This large 2MB is probably mostly comprised of your ViewState because it exists solely on the client by default. One potential performance improvement that will reduce bandwidth and reduce your request/response size will be to store ViewState on the server. You can turn this on by adding the following to your web.xml.

<context-param>
  <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
  <param-value>server</param-value>
</context-param>

This should reduce the size of the ViewState on the client, however that 2MB will end up being stored on the server session in server memory. This puts more memory usage on the server so be aware and prepared for this.




回答2:


The large response is because you are setting AjaxRendered = "true" on your output panel. This is basically telling richfaces to update the whole panel and all its contents on every ajax request whether or not related to your tree. You might want to remove that.



来源:https://stackoverflow.com/questions/9226357/jsf2-richfaces-4-1-0-ajax-partial-rendering-of-tree

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!