2

I am having a problem with selectonemenu, it seems to be all working fine but the component returns object value hashes instead of the actual values themselves. The below i provide a screen shot and backing bean with xhtml code

Thanks for your help in advance!

Screenshot

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

}
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Tugrul ASLAN
  • 354
  • 1
  • 3
  • 16
  • Hint: hover the `[selectonemenu]` tag below the question until a popbox shows up and then click the *info* link therein. – BalusC Sep 18 '14 at 07:07

2 Answers2

0

you should change f:selectItems like below:

<f:selectItems value="#{categorycontroller.categoryList}"
    var="category" itemLabel="#{category.name}" itemValue="#{category.id}" />
tt_emrah
  • 1,043
  • 1
  • 8
  • 19
  • Thanks for your comment I went for your solution. How about I want to get the selected items id number? – Tugrul ASLAN Sep 18 '14 at 07:03
  • well, that's a different question. you should use a converter for that. take a look at here please: http://www.mkyong.com/jsf2/custom-converter-in-jsf-2-0/ – tt_emrah Sep 18 '14 at 07:05
  • Thanks! I have found out a different way, when I use categoryName i can get acquire the value, I've already set it in xhtml file – Tugrul ASLAN Sep 18 '14 at 08:32
-1
<f:selectItems value="#{categorycontroller.categoryList}" />

The above line returns each category object

Category class might have holding variable like categoryName and categoryValue.

You have to loop through using <ui:repeat>

<ui:repeat var="categoryVariable" value="#{categorycontroller.categoryList}" varStatus="status">
  <f:selectItem itemLabel="#{categoryVariable.categoryName}" 
                 itemValue="#{categoryVariable.categoryValue}"/>
   </ui:repeat>
  • As long as you can't answer workable code off top of head, please test code first before doing off as an answer. This code ain't gonna work. – BalusC Sep 18 '14 at 07:04