问题
I'm creating dynamic h:selectManyListbox(es) using c:forEach but it seems that when I select different options from the h:selectManyListbox, the value corresponding to the selected options doesn't change. Here is my code. Please help I'm new with JSF 2.
This is my xhtml section (#{item.selectedBranches} not updated when options are selected) :
<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>
This is my Controller Code :
@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;
}
}
}
this is my Branches class :
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;
}
And finally this is my 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 ...
I really dont understand what's happening.. Shouldn't the item.selectedBranches points to the property in the managedbean ?
来源:https://stackoverflow.com/questions/46873016/values-of-selected-options-in-dynamic-hselectmanylistbox-dont-change