JSF Primefaces selectonemenu在列表中打印对象哈希

时间:2014-09-18 06:18:59

标签: jsf primefaces selectonemenu

我遇到了selectonemenu的问题,似乎一切正常,但组件返回对象值哈希值而不是实际值本身。下面我提供了一个屏幕截图和支持bean与xhtml代码

提前感谢您的帮助!

截图

http://www.imagesup.net/?di=3141102127810

createCategory.xhtml

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:p="http://primefaces.org/ui">

<h:head>

    <title>Create Category</title>


</h:head>

<h:body>

    <h:outputText value="Create a new category" />
    <h:form id="createcat">

        <p:inputText id="catname" value="#{categorycontroller.categoryName}"
            required="true" label="Category Name" />

        <p:watermark for="catname" value="Category Name" id="watermark" />

        <p:inputText id="catdesc"
            value="#{categorycontroller.categoryDescription}" required="true"
            label="Category Description" />

        <p:watermark for="catdesc" value="Category Description"
            id="watermark2" />

        <h:commandButton action="#{categorycontroller.createCategory()}"
            value="Create Category" />
    </h:form>

    <h:outputText value="List of categories" />
    <p:dataList value="#{indexcontroller.categoryList}" var="val">

        <p:column>

            <f:facet name="header">Categories</f:facet>

            <h:outputLink value="displayCategory.jsf?catId=#{val.catid}">#{val.categoryName}</h:outputLink>

        </p:column>

    </p:dataList>

    <h:outputText value="Create a new post" />
    <h:form id="createpost">
        <p:selectOneMenu value="#{categorycontroller.categoryName}"
            style="width:150px">
            <f:selectItem itemLabel="Category" itemValue=""
                noSelectionOption="true" />
            <f:selectItems value="#{categorycontroller.categoryList}" />
        </p:selectOneMenu>

        <p:inputText id="postname" value="#{categorycontroller.postnametxt}"
            required="true" label="Post Name" />

        <p:watermark for="postname" value="Post Name" id="watermarkpost" />

        <h:commandButton action="#{categorycontroller.createPost()}"
            value="Create Post" />
    </h:form>
</h:body>

</html>

CategoryController.java

@ManagedBean(name = "categorycontroller")
@RequestScoped
public class CategoryController implements Serializable{

    /**
     * 
     */
    private static final long serialVersionUID = -4375358965234910850L;


    @ManagedProperty(value="#{portalService}")
    PortalService portalService;

    public void createCategory(){
        Category category = new Category();
        category.setCategoryName(categoryName);
        category.setCategoryDescription(categoryDescription);
        portalService.createCategory(category);
    }

    public void createPost(){
        Category category = new Category();
        category.setCatid(catid);

        Post post = new Post();
        post.setPostname(postnametxt);
        post.setCategory(category);
        portalService.createPost(post);
    }

    private int catid;
    private String categoryName;
    private String categoryDescription;
    private String postnametxt;
    private String selectedCategory ="";
    private List<Category> categoryList;
    private List<Post> postList;

    public PortalService getPortalService() {
        return portalService;
    }
    public void setPortalService(PortalService portalService) {
        this.portalService = portalService;
    }

    public List<Post> getPostList() {
        return postList;
    }
    public void setPostList(List<Post> postList) {
        this.postList = postList;
    }
    public int getCatid() {
        return catid;
    }
    public void setCatid(int catid) {
        this.catid = catid;
    }
    public String getCategoryName() {
        return categoryName;
    }
    public void setCategoryName(String categoryName) {
        this.categoryName = categoryName;
    }
    public String getCategoryDescription() {
        return categoryDescription;
    }
    public void setCategoryDescription(String categoryDescription) {
        this.categoryDescription = categoryDescription;
    }
    public List<Category> getCategoryList() {
        return portalService.getAllCategories();
    }
    public void setCategoryList(List<Category> categoryList) {
        this.categoryList = categoryList;
    }

    public String getPostnametxt() {
        return postnametxt;
    }

    public void setPostnametxt(String postnametxt) {
        this.postnametxt = postnametxt;
    }

    public String getSelectedCategory() {
        return selectedCategory;
    }

    public void setSelectedCategory(String selectedCategory) {
        this.selectedCategory = selectedCategory;
    }   

}

2 个答案:

答案 0 :(得分:0)

您应该更改f:selectItems,如下所示:

<f:selectItems value="#{categorycontroller.categoryList}"
    var="category" itemLabel="#{category.name}" itemValue="#{category.id}" />

答案 1 :(得分:-1)

<f:selectItems value="#{categorycontroller.categoryList}" />

以上行返回每个类别对象

类别类可能具有类别category和categoryValue等保持变量。

您必须使用<ui:repeat>

进行循环播放
<ui:repeat var="categoryVariable" value="#{categorycontroller.categoryList}" varStatus="status">
  <f:selectItem itemLabel="#{categoryVariable.categoryName}" 
                 itemValue="#{categoryVariable.categoryValue}"/>
   </ui:repeat>