我正在使用c:forEach创建动态h:selectManyListbox,但似乎当我从h:selectManyListbox中选择不同的选项时,对应于所选选项的值不会改变。这是我的代码。请帮助我使用JSF 2。
这是我的xhtml部分(#{item.selectedBranches}在选择选项时未更新):
<h:body>
Hi
<h:form id="hehe">
<c:forEach items="#{branchesController.dynamicListBoxBean}" var="item" >
<h3> <h:outputLabel value="#{item.category}"/> </h3>
<h:selectManyListbox size="20" style="width: 300px" value="#{item.selectedBranches}" id="#{item.category}" >
<f:selectItems value="#{item.specificBranches}" >
</f:selectItems>
</h:selectManyListbox>
</c:forEach>
</h:form>
</h:body>
这是我的控制器代码:
@ManagedBean(name = "branchesController")
@RequestScoped
public class BranchesController implements Serializable {
@EJB
private entities.BranchesFacade ejbFacade;
private List<Branches> items = null;
private List<Branches> selectedItems = null;
private List<DynamicListBoxBean> dynamicListBoxBean = new ArrayList<>();
public BranchesController() {
}
private BranchesFacade getFacade() {
return ejbFacade;
}
public List<Branches> getItems() {
if (items == null) {
items = getFacade().findAll();
}
return items;
}
public List<DynamicListBoxBean> getDynamicListBoxBean() {
List<Branches> a = new ArrayList<>();
a= getFacade().findAll();
/* a.set(0, new Branches("1","Bib","Small"));
a.set(1, new Branches("2","Bob","Small"));
a.set(2, new Branches("3","jbb","Small"));*/
List<Branches> s = new ArrayList<>();
s = getFacade().findAll();
/* s.set(0, new Branches("1","Bib","Small"));
s.set(1, new Branches("2","Bob","Small"));
s.set(2, new Branches("3","jbb","Small"));*/
DynamicListBoxBean x = new DynamicListBoxBean("Small",0,a,s);
List<DynamicListBoxBean> abc = new ArrayList<DynamicListBoxBean>();
abc.add(0, x);
dynamicListBoxBean = abc;
return dynamicListBoxBean;
}
public void setDynamicListBoxBean(List<DynamicListBoxBean>
dynamicListBoxBean) {
this.dynamicListBoxBean = dynamicListBoxBean;
}
@FacesConverter(forClass = Branches.class)
public static class BranchesControllerConverter implements Converter {
@Override
public Object getAsObject(FacesContext facesContext, UIComponent
component, String value) {
if (value == null || value.length() == 0) {
return null;
}
BranchesController controller = (BranchesController)
facesContext.getApplication().getELResolver().
getValue(facesContext.getELContext(), null,
"branchesController");
return controller.getFacade().find(getKey(value));
}
java.lang.String getKey(String value) {
java.lang.String key;
key = value;
return key;
}
String getStringKey(java.lang.String value) {
StringBuilder sb = new StringBuilder();
sb.append(value);
return sb.toString();
}
@Override
public String getAsString(FacesContext facesContext, UIComponent
component, Object object) {
if (object == null) {
return null;
}
if (object instanceof Branches) {
Branches o = (Branches) object;
return getStringKey(o.getId());
} else {
Logger.getLogger(this.getClass().getName()).log(Level.SEVERE,
"object {0} is of type {1}; expected type: {2}", new Object[]{object,
object.getClass().getName(), Branches.class.getName()});
return null;
}
}
}
这是我的Branches类:
public class Branches implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 20)
@Column(name = "ID")
private String id;
@Size(max = 20)
@Column(name = "NAME")
private String name;
@Size(max = 20)
@Column(name = "CATEGORY")
private String category;
public Branches() {
}
public Branches(String id, String name, String category) {
this.id = id;
this.name = name;
this.category = category;
}
最后这是我的DynamicListBoxBean:
public class DynamicListBoxBean {
private String category;
private int cbValue;
private List<Branches> specificBranches;
private List<Branches> selectedBranches;
public DynamicListBoxBean(String category, int cbValue, List<Branches>
specificBranches, List<Branches> selectedBranches) {
this.category = category;
this.cbValue = cbValue;
this.specificBranches = specificBranches;
this.selectedBranches = selectedBranches;
}
With gettters and setters ...
我真的不明白发生了什么。不应该将item.selectedBranches指向managedbean中的属性吗?