h:commandButton在h:dataTable中不工作 [英] h:commandButton not working inside h:dataTable

查看:164
本文介绍了h:commandButton在h:dataTable中不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正试图通过,但是当 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
@ViewScoped
public class Bean implements Serializable {

    private List<Item> list;

    @EJB
    private ItemService service;

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

    // ...
}



< h3>另请参见:

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆