动态h:使用p:datatable的selectManyListbox

时间:2017-10-21 11:15:35

标签: primefaces jsf-2

我是JSF2的新手。我正在尝试使用p:dataTable创建动态h:selectManyListbox但不幸的是xhtml页面总是空白的,你能帮帮我PLZ:

这是我的xhtml部分:

<h:body>
Hi
    <p:dataTable id="dataTable" value="#{branchesController.dynamicListBoxBean}" var="item">
        Hi Man
    #{item.category}
    <h:selectManyListbox size="20" style="width: 300px" value="#{item.selectedBranches}" >
            <f:selectItems value="#{item.specificBranches}">

        </f:selectItems>


    </h:selectManyListbox>


    </p:dataTable>


</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 ...

请帮我解决这个问题。我是JSF 2的新手......它总是给我一个空白页......我该怎么办?

0 个答案:

没有答案