【问题标题】:JSF Primefaces selectonemenu prints object hash in list [duplicate]JSF Primefaces selectonemenu在列表中打印对象哈希[重复]
【发布时间】:2014-11-12 08:56:20
【问题描述】:

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

提前感谢您的帮助!

截图

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;
    }   

}

【问题讨论】:

  • 提示:将[selectonemenu]标签悬停在问题下方,直到弹出框出现,然后单击其中的信息链接。

标签: jsf primefaces selectonemenu


【解决方案1】:

你应该像下面这样更改f:selectItems

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

【讨论】:

  • 感谢您的评论,我寻求您的解决方案。我想获取选中的商品id号怎么样?
  • 嗯,这是一个不同的问题。你应该为此使用转换器。请看这里:mkyong.com/jsf2/custom-converter-in-jsf-2-0
  • 谢谢!我发现了一种不同的方式,当我使用 categoryName 时,我可以获得该值,我已经在 xhtml 文件中设置了它
【解决方案2】:
<f:selectItems value="#{categorycontroller.categoryList}" />

以上行返回每个类别对象

Category 类可能包含 categoryName 和 categoryValue 等变量。

你必须循环使用&lt;ui:repeat&gt;

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

【讨论】:

  • 只要你无法回答可行的代码,请先测试代码,然后再作为答案。这段代码行不通。
猜你喜欢
  • 1970-01-01
  • 2016-12-18
  • 1970-01-01
  • 2021-03-03
  • 2014-04-01
  • 1970-01-01
  • 2022-12-10
  • 1970-01-01
  • 2012-10-26
相关资源
最近更新 更多