H: inputText values ​​inside p: treeTable are not processed

I want to process this form (valueChangueListener is not valid in the real case).

This is the flip side of the bean:

@Component @Scope("request") public class TestBean extends PrivateBaseBean implements Serializable { protected static final Logger logger = Utils.loggerForThisClass(); private TreeNode root; @PostConstruct public void init() { root = new DefaultTreeNode("root", null); TreeNode child1 = new DefaultTreeNode(new Element("Total"), root); new DefaultTreeNode(new Element("Office"), child1); } public void saveAction() { StringBuilder textToShowInMessage = new StringBuilder(); for (TreeNode children : root.getChildren()) { logger.debug(((Element) children.getData()).getName() + "->" + ((Element) children.getData()).getValue()); for (TreeNode leaf : children.getChildren()) { logger.debug(((Element) leaf.getData()).getName() + "->" + ((Element) leaf.getData()).getValue()); } } } public TreeNode getRoot() { return root; } public void setRoot(TreeNode root) { this.root = root; } 

Elemental Model:

 public class Element { private String name; private String value; public Element(String name) { super(); this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getValue() { return value; } public void setValue(String value) { this.value = value; } 

Finally, the view:

 <h:form> <p:treeTable value="#{testBean.root}" var="element"> <p:column> <f:facet name="header"> Name </f:facet> #{element.name} </p:column> <p:column> <f:facet name="header"> Value </f:facet> <h:inputText value="#{element.value}" /> </p:column> </p:treeTable> <p:commandButton value="#{msg.save}" action="#{testBean.saveAction}" process="@all" icon="ui-icon-disk" update="@form" /> </h:form> 

I want to handle all form values ​​inside a TreeNode. But only the first value of the first childresn returns to the Backing bean.

This is intput:

Input form

This is the conclusion:

 DEBUG: com.smf.web.jsf.bean.TestBean - Total->1231231231 DEBUG: com.smf.web.jsf.bean.TestBean - Office->null 

I do not know how to handle all tree values ​​...

0
source share
1 answer

One solution:

1. Make an array of session objects with tree elements and elements:

 @Component @Scope("session") public class SessionObject { private Object[] root; public Object[] getRoot() { return root; } public void setRoot(Object[] root) { this.root = root; } } 

2.Inject in TestBean:

 @Autowired private SessionObject sessionObject; 

3. In the init () method, we must save everything in the session:

 Object[] array = new Object[] { root, element, element2 }; sessionObject.setRoot(array); 

4. Finally, in the saveAction () file, we can restore elements with new values:

 for (TreeNode children : ((DefaultTreeNode) ((Object[]) sessionObject .getRoot())[0]).getChildren()) { .... 

Each session item is required:

Backbean:

 @Component @Scope("request") public class TestBean extends PrivateBaseBean implements Serializable { @Autowired private SessionObject sessionObject; protected static final Logger logger = Utils.loggerForThisClass(); private TreeNode child1; private Element element; private Element element2; @PostConstruct public void init() { sessionObject = new SessionObject(); element = new Element("Total"); element2 = new Element("Oficina"); TreeNode root = new DefaultTreeNode("root", null); child1 = new DefaultTreeNode(element, root); new DefaultTreeNode(element2, root); Object[] array = new Object[] { root, element, element2 }; sessionObject.setRoot(array); } public void saveAction() { for (TreeNode children : ((DefaultTreeNode) ((Object[]) sessionObject .getRoot())[0]).getChildren()) { logger.debug(((Element) children.getData()).getName() + "->" + ((Element) children.getData()).getValue()); for (TreeNode leaf : children.getChildren()) { logger.debug(((Element) leaf.getData()).getName() + "->" + ((Element) leaf.getData()).getValue()); } } } public TreeNode getRoot() { return ((DefaultTreeNode) ((Object[]) sessionObject.getRoot())[0]); } public void setRoot(TreeNode root) { Object[] array = new Object[] { root, element, element2 }; sessionObject.setRoot(array); } } 

Now the conclusion:

 DEBUG: com.smf.web.jsf.bean.TestBean - Total->TotalValue DEBUG: com.smf.web.jsf.bean.TestBean - Oficina->OfficeValue 

Read more here ..

0
source

Source: https://habr.com/ru/post/944989/


All Articles