4

selectAll当我单击复选框时,从 bean调用 Java 方法的适当方法是什么?

<f:facet name="header">
      <h:selectBooleanCheckbox  binding="#{bean.selectAll}" onclick="highlight(this)" class="checkall"/>
</f:facet>

binding不管用。我只想在这个 Java 方法中执行代码。

编辑

private HashMap<String, Boolean> selected = new HashMap<>();
public void selectAll() throws Exception {

        String SqlStatement = null;

        if (ds == null) {
            throw new SQLException();
        }

        Connection conn = ds.getConnection();
        if (conn == null) {
            throw new SQLException();
        }

        SqlStatement = "SELECT ID FROM ACTIVESESSIONSLOG";

        PreparedStatement ps = null;
        ResultSet resultSet = null;
        int count = 0;

        try {
            conn.setAutoCommit(false);
            boolean committed = false;
            try {
                ps = conn.prepareStatement(SqlStatement);
                resultSet = ps.executeQuery();
                selected.clear();

                while (resultSet.next()) {

                    selected.put(resultSet.getString("ID"), true);
                }
/*
                for (Map.Entry<String, Boolean> entry : selectedIds.entrySet()) {
                    entry.setValue(true);
                }

*/                conn.commit();
                committed = true;
            } finally {
                if (!committed) {
                    conn.rollback();
                }
            }
        } finally {
            ps.close();
            conn.close();
        }
    }
4

1 回答 1

4

用于<f:ajax>发送 ajax 请求。

<h:selectBooleanCheckbox value="#{bean.selectAll}" onclick="highlight(this)" class="checkall">
    <f:ajax listener="#{bean.onSelectAll}" render="@form" />
</h:selectBooleanCheckbox>

private boolean selectAll;

public void onSelectAll(AjaxBehaviorEvent event) {
    // If you're using a boolean property on the row object.
    for (Item item : list) {
        item.setSelected(selectAll);
    }

    // Or if you're using a Map<Long, Boolean> on item IDs
    for (Entry<Long, Boolean> entry : selected.entrySet()) {
        entry.setValue(selectAll);
    }
}

public boolean isSelectAll() {
    return selectAll;
}

public void setSelectAll(boolean selectAll) {
    this.selectAll = selectAll;
}

理想情况下,应将 bean 放置在视图范围内,@ViewScoped以使其在同一视图上的 ajax 请求中保持活动状态。

binding除非您有充分的理由,否则不要使用bean 属性。它旨在将整体绑定UIComponent到允许动态组件操作的 bean,但通常有更简单且不那么突兀的方法。

于 2012-05-24T15:07:02.357 回答