[英]How to call bean method when clicking on a checkbox
當我點擊復選框時從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();
}
}
使用<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
將bean放在視圖范圍內,以使其在同一視圖上的ajax請求中保持活動狀態。
除非你有充分的理由,否則不要使用binding
到bean屬性。 它的目的是將整個UIComponent
綁定到允許動態組件操作的bean,但通常會有更簡單且不那么突兀的方式。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.