本文介绍了h:commandButton在h:dataTable中不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我正试图通过,但是当 commandButton 被放置在数据表内部时,不会调用 p>I am trying to execute an action through commandButton inside a dataTable, but the action is not invoked when the commandButton is placed inside the 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>当我将 commandButton code> dataTable ,成功执行操作。 $ code> commandButton 在datatable里面有什么问题? commandLink 有同样的问题。When I move the commandButton out of dataTable, the action is successfully executed. What is the problem when commandButton is inside datatable? The commandLink has the same problem.推荐答案在#{bean.list} 中的列表在不是中完全一样,在HTTP请求期间处理表单提交,就像在请求期间一样显示表单。 JSF将重新遍历列表以找到按下的按钮并调用其操作。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.如果bean是请求作用域,并且在bean(post)构造期间列表不被重新填充,或者列表的总体取决于在表单中丢失的请求作用域变量提交,那么JSF将在处理表单提交时检索一个空的或完全不同的列表,因此将无法找到按下的按钮,并且不会调用任何操作。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.最好的解决方法是将bean放在视图范围内,并确保正确加载数据模型。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@ViewScopedpublic class Bean implements Serializable { private List<Item> list; @EJB private ItemService service; @PostConstruct public void init() { list = service.list(); } // ...} 另请参见: commandButton / commandLink / ajax action / listener方法未被调用或输入值未更新 - 第4点 @ViewScoped的优点和陷阱See also:commandButton/commandLink/ajax action/listener method not invoked or input value not updated - point 4Benefits and pitfalls of @ViewScoped 这篇关于h:commandButton在h:dataTable中不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-06 09:16