h:commandButton not working inside h:dataTable

This problem can happen when the list behind #{bean.list} is not exactly the same during the HTTP request of processing the form submit as it was during the request of displaying the form. JSF will namely re-iterate over the list to locate the button pressed and invoke its action.

If the bean is request scoped and the list is not repopulated during bean’s (post)construction, or the list’s population depends on a request scoped variable which was lost during the form submit, then JSF will retrieve an empty or a completely different list while processing the form submit and thus won’t be able to locate the button pressed and won’t invoke any action.

The best fix is to put the bean in the view scope and ensuring that you’re loading the data model the proper way.

@ManagedBean
@ViewScoped
public class Bean implements Serializable {

    private List<Item> list;

    @EJB
    private ItemService service;

    @PostConstruct
    public void init() {
        list = service.list();
    }

    // ...
}

See also:

Leave a Comment