2 Replies Latest reply on Jul 8, 2010 5:10 AM by Heri Bender

    Seam Drop Down Menu

    nish patel Newbie

      Hello all,
      I am a newbie to web development. I just started learning Seam. I was reading to through some tutorials and decided to create a form with 3 fields, 2 drop down and 1 text field.
      I am using 3 entities to create this form, category, subcategory and request.


      Schema:


      category(category-id, category-description, workgroup-id),


      subcategory(subcategory-id, subcategory-description, category-id),


      request(request-id, request-notes, subcategory-id)



      What I want to do is, first allow user to select category via drop down. Now, based on selected category, let user select subcategory via drop down, and then add notes to create request.



      Category.java



      package com.mydomain.second.model;
      // Generated Jun 29, 2010 9:09:06 AM by Hibernate Tools 3.2.4.GA
      
      import java.util.HashSet;
      import java.util.Set;
      import javax.persistence.Column;
      import javax.persistence.Entity;
      import javax.persistence.FetchType;
      import javax.persistence.GeneratedValue;
      import static javax.persistence.GenerationType.IDENTITY;
      import javax.persistence.Id;
      import javax.persistence.OneToMany;
      import javax.persistence.Table;
      import javax.persistence.UniqueConstraint;
      import org.hibernate.validator.Length;
      import org.hibernate.validator.NotNull;
      import org.jboss.seam.annotations.Name;
      
      /**
       * Category generated by hbm2java
       */
      @Entity
      @Name("category")
      @Table(name = "category", catalog = "etech", uniqueConstraints = @UniqueConstraint(columnNames = "category_description"))
      public class Category implements java.io.Serializable {
      
           private Integer categoryId;
           private String categoryDescription;
           private int workgroupId;
           private Set<Subcategory> subcategories = new HashSet<Subcategory>(0);
      
           public Category() {
           }
      
           public Category(String categoryDescription, int workgroupId) {
                this.categoryDescription = categoryDescription;
                this.workgroupId = workgroupId;
           }
           public Category(String categoryDescription, int workgroupId,
                     Set<Subcategory> subcategories) {
                this.categoryDescription = categoryDescription;
                this.workgroupId = workgroupId;
                this.subcategories = subcategories;
           }
      
           @Id
           @GeneratedValue(strategy = IDENTITY)
           @Column(name = "category_id", unique = true, nullable = false)
           public Integer getCategoryId() {
                return this.categoryId;
           }
      
           public void setCategoryId(Integer categoryId) {
                this.categoryId = categoryId;
           }
      
           @Column(name = "category_description", unique = true, nullable = false, length = 50)
           @NotNull
           @Length(max = 50)
           public String getCategoryDescription() {
                return this.categoryDescription;
           }
      
           public void setCategoryDescription(String categoryDescription) {
                this.categoryDescription = categoryDescription;
           }
      
           @Column(name = "workgroup_id", nullable = false)
           public int getWorkgroupId() {
                return this.workgroupId;
           }
      
           public void setWorkgroupId(int workgroupId) {
                this.workgroupId = workgroupId;
           }
      
           @OneToMany(fetch = FetchType.LAZY, mappedBy = "category")
           public Set<Subcategory> getSubcategories() {
                return this.subcategories;
           }
      
           public void setSubcategories(Set<Subcategory> subcategories) {
                this.subcategories = subcategories;
           }
      
      }
      




      Subcategory.java



      package com.mydomain.second.model;
      // Generated Jun 29, 2010 9:09:06 AM by Hibernate Tools 3.2.4.GA
      
      import java.util.HashSet;
      import java.util.Set;
      import javax.persistence.Column;
      import javax.persistence.Entity;
      import javax.persistence.FetchType;
      import javax.persistence.GeneratedValue;
      import static javax.persistence.GenerationType.IDENTITY;
      import javax.persistence.Id;
      import javax.persistence.JoinColumn;
      import javax.persistence.ManyToOne;
      import javax.persistence.OneToMany;
      import javax.persistence.Table;
      import org.hibernate.validator.Length;
      import org.hibernate.validator.NotNull;
      import org.jboss.seam.annotations.Name;
      
      /**
       * Subcategory generated by hbm2java
       */
      @Entity
      @Name("subcategory")
      @Table(name = "subcategory", catalog = "etech")
      public class Subcategory implements java.io.Serializable {
      
           private Integer subcategoryId;
           private Category category;
           private String subcategoryDescription;
           private Set<Request> requests = new HashSet<Request>(0);
      
           public Subcategory() {
           }
      
           public Subcategory(Category category, String subcategoryDescription) {
                this.category = category;
                this.subcategoryDescription = subcategoryDescription;
           }
           public Subcategory(Category category, String subcategoryDescription,
                     Set<Request> requests) {
                this.category = category;
                this.subcategoryDescription = subcategoryDescription;
                this.requests = requests;
           }
      
           @Id
           @GeneratedValue(strategy = IDENTITY)
           @Column(name = "subcategory_id", unique = true, nullable = false)
           public Integer getSubcategoryId() {
                return this.subcategoryId;
           }
      
           public void setSubcategoryId(Integer subcategoryId) {
                this.subcategoryId = subcategoryId;
           }
      
           @ManyToOne(fetch = FetchType.LAZY)
           @JoinColumn(name = "category_id", nullable = false)
           @NotNull
           public Category getCategory() {
                return this.category;
           }
      
           public void setCategory(Category category) {
                this.category = category;
           }
      
           @Column(name = "subcategory_description", nullable = false, length = 50)
           @NotNull
           @Length(max = 50)
           public String getSubcategoryDescription() {
                return this.subcategoryDescription;
           }
      
           public void setSubcategoryDescription(String subcategoryDescription) {
                this.subcategoryDescription = subcategoryDescription;
           }
      
           @OneToMany(fetch = FetchType.LAZY, mappedBy = "subcategory")
           public Set<Request> getRequests() {
                return this.requests;
           }
      
           public void setRequests(Set<Request> requests) {
                this.requests = requests;
           }
      
      }
      




      Request.java



      package com.mydomain.second.model;
      // Generated Jun 29, 2010 9:09:06 AM by Hibernate Tools 3.2.4.GA
      
      import javax.persistence.Column;
      import javax.persistence.Entity;
      import javax.persistence.FetchType;
      import javax.persistence.GeneratedValue;
      import static javax.persistence.GenerationType.IDENTITY;
      import javax.persistence.Id;
      import javax.persistence.JoinColumn;
      import javax.persistence.ManyToOne;
      import javax.persistence.Table;
      import org.hibernate.validator.Length;
      import org.hibernate.validator.NotNull;
      
      /**
       * Request generated by hbm2java
       */
      @Entity
      @Table(name = "request", catalog = "etech")
      public class Request implements java.io.Serializable {
      
           private Integer requestId;
           private Subcategory subcategory;
           private String requestNote;
      
           public Request() {
           }
      
           public Request(Subcategory subcategory, String requestNote) {
                this.subcategory = subcategory;
                this.requestNote = requestNote;
           }
      
           @Id
           @GeneratedValue(strategy = IDENTITY)
           @Column(name = "request_id", unique = true, nullable = false)
           public Integer getRequestId() {
                return this.requestId;
           }
      
           public void setRequestId(Integer requestId) {
                this.requestId = requestId;
           }
      
           @ManyToOne(fetch = FetchType.LAZY)
           @JoinColumn(name = "subcategory_id", nullable = false)
           @NotNull
           public Subcategory getSubcategory() {
                return this.subcategory;
           }
      
           public void setSubcategory(Subcategory subcategory) {
                this.subcategory = subcategory;
           }
      
           @Column(name = "request_note", nullable = false, length = 65535)
           @NotNull
           @Length(max = 65535)
           public String getRequestNote() {
                return this.requestNote;
           }
      
           public void setRequestNote(String requestNote) {
                this.requestNote = requestNote;
           }
      
      }
      




      Session Bean



      package com.mydomain.second.action;
      
      import java.util.*;
      
      import javax.ejb.Remove;
      import javax.ejb.Stateful;
      import javax.persistence.EntityManager;
      import javax.persistence.PersistenceContext;
      
      import org.jboss.seam.annotations.Create;
      import org.jboss.seam.annotations.Name;
      import org.jboss.seam.annotations.In;
      import org.jboss.seam.annotations.Logger;
      import org.jboss.seam.annotations.Out;
      import org.jboss.seam.log.Log;
      import org.jboss.seam.international.StatusMessages;
      import org.hibernate.search.annotations.Factory;
      import org.hibernate.validator.Length;
      
      import com.mydomain.second.model.Category;
      import com.mydomain.second.model.Request;
      import com.mydomain.second.model.Subcategory;
      
      @Stateful
      @Name("requestManager")
      public class RequestManagerBean implements RequestManager
      {
          @Logger private Log log;
          
          @In StatusMessages statusMessages;
          
          @PersistenceContext
          private EntityManager entityManager;
          
          @Out private Request request;
        
          @Out(required=false)
          private static List<Category> categories = new LinkedList<Category>();
      
          public void createRequest()
          {
              // implement your business logic here
              log.info("requestManager.createRequest() action called with: #{requestManager.value}");
              statusMessages.add("createRequest #{requestManager.value}");
          }
      
          public Request getRequest() {
                return request;
           }
      
           public void setRequest(Request request) {
                this.request = request;
           }
      
           public List<Category> getCategories() {
                categories = entityManager.createQuery("select c from category c").getResultList();
                return categories;
                //return categories;
           }
           
           public void setCategories(List<Category> categories) {
                this.categories = categories;
           }
           
           @Create
           public void findCategories() {
                System.out.println("Nishant patel");
                try {
                     categories = entityManager.createQuery("select c from category c").getResultList();
                     System.out.println(categories);
                }
                catch(Exception e) {
                     System.out.println("Error");
                     e.printStackTrace();
                }
           }
           @Remove
          public void destroy() {}
      
      }
      




      Page



      <h:outputText value="Category" />
                     <h:selectOneMenu value="#{category.categoryDescription}" required="true" >
                          <s:selectItems value="#{categories}" 
                                              var="category" 
                                              label="#{category.categoryDescription}"/>
                          <s:convertEntity/>
                     </h:selectOneMenu>




      I am getting following error:
      Expected a child component type of UISelectItem/UISelectItems for component type javax.faces.SelectOne(j_id15). Found null.



      Thanks guys
      nish