H: commandButton not working inside h: dataTable

I am trying to execute an action via commandButton inside a dataTable , but the action not called when commandButton is placed inside a datatable, as shown below

 <h:form> <h:dataTable value="#{bean.list}" var="item"> <h:column> <h:commandButton value="submit" action="#{bean.submit}" /> </h:column> </h:dataTable> </h:form> 

When I move the commandButton from the dataTable , the action succeeds. What is the problem when commandButton is inside datatable? commandLink has the same problem.

+4
source share
1 answer

This problem may occur if the list behind #{bean.list} does not match in the exact same way during the HTTP form processing request as it did during the form display request. JSF will iterate over the list to find the pressed button and trigger its action.

If the bean is the request area and the list is not populated during the bean (post) construction, or the list population depends on the request area variable that was lost when the form was submitted, then JSF will retrieve an empty or completely different list when processing the submit form and therefore, it will not be able to find the pressed button and will not cause any action.

The best solution is to place the bean in the viewport and make sure that you have loaded the data model correctly.

 @ManagedBean @ViewScoped public class Bean implements Serializable { private List<Item> list; @EJB private ItemService service; @PostConstruct public void init() { list = service.list(); } // ... } 

See also:

+13
source

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


All Articles