简体   繁体   中英

Primefaces cannot update p:tree

I'm working with primefaces 4.0 and JSF 2.2 and I'm currently trying to update a form with a p:tree on it. The commandButton works correctly but it does not update the form or call the init() method until I manually refresh the page. I don't know what I'm doing wrong since the same code does work for a DataTable element.

Here's the code:

<h:form id="preferenciasForm">
 <div id="panelTree">
    <p:panel id="defTree" style="margin-bottom: 20px">
        <p:tree value="#{dtPreferencesBuilder.root}" var="node"
            selectionMode="checkbox"
            selection="#{dtPreferencesBuilder.selectedNodes}"
            style="width:100%; height:100%;" animate="true">
               <p:treeNode>
                    <h:outputText value="#{node.label}" />
               </p:treeNode>
        </p:tree>
        <p:commandButton value="Add preferences"
            icon="ui-icon-pencil"
            actionListener="#{dtPreferencesBuilder.insertPrefNodes()}"
            update=":preferenciasForm" ajax="true" />
    </p:panel>
 </div>
</h:form>

And here's is the java class.

@ManagedBean(name="dtPreferencesBuilder")
@ViewScoped //I've tried with or without the ViewScoped, neither work
public class PreferencesBuilderBean {
  private TreeNode root;
  private TreeNode prefRoot;
  private TreeNode[] selectedNodes;

 @PostConstruct
 public void init() {
    System.out.println("Building Tree");
    selectedNodes=null;
    root=null;
    prefRoot=null;
    root=getStandardTree();
    prefRoot=getPreferedTree();
 }

The init() is not called as the print is only show on manual reload so the tree is not updated nor the selectedNodes refreshed. Any ideas why it doesn't work?

As I cannot describe bean scopes better than excellent answers already given for similar questions I'll just refer you to the answers by BalusC and Kishor P here .

The init-method (or any method with the @PostConstruct-annotation) will be called by the framework only when the bean is created, after injections and therefore after the constructor has run as rion18 said. It would not be normal to use the method for anything else than initializing work. So create other methods, and call those from actions and actionListeners.

If you want the bean to be the same when you call it with ajax (as you do) it needs to be at least ViewScoped. If you really do want to call the init() every time it should be RequestScoped, but then the bean will be new when you call it with ajax and not remember a thing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM