h:commandButton 在 h:dataTable 内不起作用 [英] h:commandButton not working inside h:dataTable
问题描述
我试图在 dataTable
中通过 commandButton
执行一个 action
,但是没有调用 action
当 commandButton
被放置在数据表中时,如下所示
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
移出 dataTable
时,action
成功执行.commandButton
在数据表中时会出现什么问题?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 的(后)构造期间没有重新填充列表,或者列表的填充取决于在表单提交期间丢失的请求范围变量,那么 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
@ViewScoped
public 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 的好处和缺陷
这篇关于h:commandButton 在 h:dataTable 内不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!