问题
My xhtml code is:
<p:tree id="attachTree" style="width: 100%;" value="#{detailsTaskBacking.attachRootNode}" selectionMode="single" selection="#{detailsTaskBacking.selectedNode}" var="node">
<p:ajax event="select" async="false" update=":roteiroAttachTab:formAttachForm:mediaPdf" listener="#{detailsTaskBacking.onNodeSelect}"/>
<p:treeNode expandedIcon="ui-icon-folder-open" collapsedIcon="ui-icon-folder-collapsed">
<h:outputText value="#{text['tasksbacking.tabAttach']}"/>
</p:treeNode>
<p:treeNode type="file" expandedIcon="ui-icon-document" collapsedIcon="ui-icon-document">
<h:outputText value="#{node.name}"/>
</p:treeNode>
</p:tree>
And my bean code is:
@request
...
public void onNodeSelect(NodeSelectEvent event) {...}
When run the project in debug mode selectedNode for argument event is null, why?
Why in primefaces tree ajax events not work when bean scope is request? I tested this with one bean in view scope and the selectedNode is not null, why?
回答1:
That will happen if the model behind #{detailsTaskBacking.attachRootNode} incompatibly changes during postback. E.g. when it's reinitialized to null. You need to make sure that the model is exactly the same across postbacks on the same view. In case of a request scoped bean, you'd need to make sure that you prepare exactly the same model in @PostConstruct as it was when the form is being displayed. Another way is to just make the bean @ViewScoped so that the same bean instance lives as long as you postback on the same view.
See also:
- How to choose the right bean scope?
- commandButton/commandLink/ajax action/listener method not invoked or input value not updated - point 4
来源:https://stackoverflow.com/questions/25422401/why-tree-ajax-events-in-primefaces-dont-work-when-bean-scope-is-request