0
votes

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?

1
Init() is not supposed to be called, only when the bean is created. So you need a RequestScoped bean if you want it called, but then it will be completely new. I think you would want to keep it ViewScoped and do your job in insertPrefNodes(), but I don't understand exactly what you are trying to do - Jaqen H'ghar
Using a @PostConstruct method... does the print get called when you access the page for the first time? Also, on the button, instead of an actionListener have you tried using action? - rion18
@JaqenH'ghar I'm just tryind to add a new node and display it on the tree without having to manually refresh the webpage. I set the selectedNode = null and the new tree in the insertPrefNodes() method but it does not work. It's like the update is not working. - Benjamin Jimenez
@rion18 yes the print is called everytime I refresh the webpage (F5) so when I add a new node and refresh, the new node is shown in the tree however is not shown without manually refreshing the webpage. The button is working either with action or actionlistener but neither update the form. - Benjamin Jimenez
Can you try doing it programatically? As in, in your insertPrefNodes method, add a RequestContext.getCurrentInstance().update("preferenciasForm"); - rion18

1 Answers

0
votes

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.