尽管视图可以从bean中获取所有值,但当复选框(h:selectBooleanCheckbox
)被切换时,bean不会更新。永远不会从视图中调用Item
中的setSelected()
方法。JSF复选框值未发送到bean
我试着将bean的作用域更改为应用程序,使用地图而不是所选属性的值以及编写我自己的ajax javascript函数的愚蠢尝试。
我正在使用的应用程序有点遗留,所以我使用的是Tomcat 6,JSF 1.2和Richfaces 3.3.3.Final。
它看起来应该很简单,我想我错过了一些显而易见的东西,但我已经在这里呆了两天,并且无法弄清楚为什么bean没有更新。
我的看法是:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:c="http://java.sun.com/jstl/core"
xmlns:fn="http://java.sun.com/jsp/jstl/functions"
xmlns:a4j="http://richfaces.org/a4j"
xmlns:rich="http://richfaces.org/rich">
<head>
<title>JSF</title>
</head>
<body>
<h:form id="tableForm">
<rich:dataTable id="table" value="#{managedBean}" var="item" width="100%">
<rich:column label="Select" align="center" width="40px" >
<f:facet name="header">
<h:outputText value="Select" />
</f:facet>
<h:selectBooleanCheckbox value="#{item.selected}" >
<a4j:support execute="@this" event="onclick" ajaxSingle="true"/>
</h:selectBooleanCheckbox>
</rich:column>
<rich:column label="Name" align="center" width="40px">
<f:facet name="header">
<h:outputText value="Name" />
</f:facet>
<h:outputText value="#{item.name}" />
</rich:column>
</rich:dataTable>
</h:form>
</body>
</html>
我在会话范围内管理bean ItemBean:
import org.richfaces.model.ExtendedTableDataModel;
public class ItemBean extends ExtendedTableDataModel<Item>{
public ItemBean() {super(new ItemProvider());}
}
ItemProvider是:
import org.richfaces.model.DataProvider;
public class ItemProvider implements DataProvider<Item>{
private List<Item> items = new ArrayList<Item>();
public ItemProvider(){
for(int i = 0; i < 10; i++){
items.add(new Item(i, "Item "+i));
}
}
public Item getItemByKey(Object key) {return items.get((Integer)key);}
public List<Item> getItemsByRange(int fromIndex, int toIndex) {
return new ArrayList<Item>(items.subList(fromIndex, toIndex));
}
public Object getKey(Item arg0) {return arg0.getId();}
public int getRowCount() {return items.size();}
}
和项目是:
public class Item {
private final int id;
private final String name;
private boolean selected;
public Item(int id, String name) {
this.id = id;
this.name = name;
selected = false;
}
public int getId(){
return id;
}
public String getName(){
return name;
}
public boolean isSelected(){
return selected;
}
public void setSelected(boolean selected){
this.selected = selected;
}
}
感谢您的答复,不幸的是,问题是,我已经错过了在web.xml配置。在JSF中工作时,在您的答案中使用标记是否是最佳做法?看起来好像是这样。 – Sam