单击复选框时从bean调用selectAll
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();
}
}
答案 0 :(得分:3)
使用<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;
}
理想情况下,应将@ViewScoped
放置在视图范围内,以使其在同一视图上的ajax请求中保持活动状态。
除非您有非常好的原因,否则不要将binding
用于bean属性。它旨在将整个UIComponent
绑定到允许动态组件操作的bean,但是通常会有更简单且不那么突兀的方式。