How to collect UIInput component values ​​inside a UIData component in a bean action method?

I am trying to collect the values ​​of the UIInput component inside the UIData component during the action of the bean method to check for duplicate values. I tried to bind the UIInput component to the bean property and get its value, but it prints null . If I put it outside the data limits, then it prints the expected value. Is there something wrong with the data?

 <rich:dataTable binding="#{bean.table}" value="#{bean.data}" var="item"> <h:column> <f:facet name="header"> <h:outputText value="Field1" /> </f:facet> <h:inputText binding="#{bean.input}" value="#{item.field1}" /> </h:column> </rich:dataTable> 

Here is the bean code:

 private UIData table; private UIInput input; public void save() { System.out.println(input.getId() + " - " + input.getValue()); } 
+2
source share
1 answer

There is nothing wrong with the data. There is only one UIInput component in the JSF component tree, the state of which changes each time the parent UIData component UIData over each element of the model. Thus, the state is available only during UIData iteration, and not before or after. You are trying to access the value of a single component in a bean action method, while the parent UIData component UIData not iterate over it, so the values ​​will always return null .

You need to visit the UIComponent#visitTree() component tree on UIData and collect the information of interest in the VisitCallback implementation.

 table.visitTree(VisitContext.createVisitContext(FacesContext.getCurrentInstance()), new VisitCallback() { @Override public VisitResult visit(VisitContext context, UIComponent target) { if (target instanceof UIInput) { UIInput input = (UIInput) target; System.out.println("id: " + input.getId()); System.out.println("value: " + input.getValue()); } return VisitResult.ACCEPT; } }); 

By the way, you usually do validation with a normal Validator on a UIInput component UIInput or, in this particular case, perhaps better, a ValueChangeListener . This simplifies invalidation and message processing.

+1
source

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


All Articles