7 Replies Latest reply on Feb 19, 2007 8:24 AM by grdzeli_kaci

    Fill selectOneMenu on page load.

      hi all, i'm newbie in seam.
      i've one simple question. (i've seam documentation : seam_refference.pdf but could't resolv the problem)

      ok, i have login page and there are some selectOneMenu - components. for example applications or languages. i need fill them on page sturtup from database. is there any doc or example which can help me ? or could anybody help me how to do it?

      1.here is my login.xhtml file :

      <ui:composition xmlns="http://www.w3.org/1999/xhtml"
       xmlns:s="http://jboss.com/products/seam/taglib"
       xmlns:ui="http://java.sun.com/jsf/facelets"
       xmlns:f="http://java.sun.com/jsf/core"
       xmlns:h="http://java.sun.com/jsf/html"
       template="layout/template.xhtml">
      
       <ui:define name="body">
       <h:form id="loginForm">
       <table width="400" cellpadding="5" cellspacing="0" style="border: 1px solid #9e2629">
       <tr>
       <td class="LogonText" colspan="2" align="center">Login to System</td>
       </tr>
       <tr>
       <td class="td_new">
       <table width="100%" cellpadding="5" cellspacing="0" border="0">
       <tr>
       <td class="td_new" rowspan="5"><h:graphicImage value="/images/login_pic.gif"/></td>
       <td class="td_new">UserName</td>
       <td class="td_new">
       <h:inputText styleClass="font_style" value="" />
       </td>
      
       </tr>
      
       <tr>
       <td class="td_new">Password</td>
       <td class="td_new">
       <h:inputSecret styleClass="font_style" value="" />
       </td>
      
       </tr>
      
       <tr>
       <td class="td_new">Application</td>
       <td class="td_new">
       <h:selectOneMenu styleClass="font_style" value="">
       <!-- how i could write to fill this component? -->
       </h:selectOneMenu>
       </td>
      
       </tr>
      
       <tr>
       <td class="td_new">Language</td>
       <td class="td_new">
       <h:selectOneMenu styleClass="font_style" required="true" value="">
       <f:selectItems value="#{loginBean.langItems}" />
       <f:convertNumber type="number" />
       </h:selectOneMenu>
       </td>
      
       </tr>
       <tr>
       <td class="td_right" colspan="2">
       <h:commandButton id="LoginBtn"
       name="LoginBtn" class="Btn2"
       value="Login"
       onblur="return this.myonblur();"
       onfocus="return this.myonfocus();"
       onmouseout="return this.myonmouseout();"
       onmouseover="return this.myonmouseover();"/>
       <script type="text/javascript"><!--
       sjwuic_assign_button('loginForm:LoginBtn', defaultButtonStrings, true, false, false);
       //--></script>
       </td>
       <!--actionListener="" -->
       </tr>
       </table>
       </td>
       </tr>
       <tr>
       <td class="td_new" colspan="2"></td>
       </tr>
       <tr>
       <td class="td_new" colspan="2"></td>
       </tr>
       </table>
       </h:form>
       </ui:define>
      </ui:composition>
      


      2. and here is my entity bean for application :
      @Entity
      @Table(name = "LANGUAGE")
      @Name("user")
      @Scope(ScopeType.SESSION)
      public class Language implements Serializable {
       @Id()
       @GeneratedValue(generator="LangIdGenerator")
       @SequenceGenerator(name = "LangIdGenerator", sequenceName = "SEQ_LANGUAGE_ID")
       @Column(name="LANGUAGE_ID", unique=true, nullable=false, precision=22)
       private Long languageId;
      
       @Column(name = "NAME")
       private String name;
      
       @Column(name = "SHORTNAME")
       private String shortname;
      
       @Lob
       @Column(name = "IMAGE")
       private byte [] image;
      
       @Lob
       @Column(name = "LOCALE")
       private byte [] locale;
      
       @Column(name = "REMARK")
       private String remark;
      
       @Column(name = "USER_ID")
       private BigInteger userId;
      
       @Column(name = "REC_STATUS")
       private BigInteger recStatus;
      
       @Column(name = "REG_DATE")
       @Temporal(TemporalType.DATE)
       private Date regDate;
      
       @Column(name = "UPD_DATE")
       @Temporal(TemporalType.DATE)
       private Date updDate;
      
       @OneToMany(mappedBy = "languageId")
       private Collection<LanguageProperty> languagePropertyCollection;
      
       transient private List<SelectItem> applications;
      
       /** Creates a new instance of Language */
       public Language() {
       }
      
       /**
       * Creates a new instance of Language with the specified values.
       * @param languageId the languageId of the Language
       */
       public Language(Long languageId) {
       this.languageId = languageId;
       }
      
       /**
       * Gets the languageId of this Language.
       * @return the languageId
       */
       public Long getLanguageId() {
       return this.languageId;
       }
      
       /**
       * Sets the languageId of this Language to the specified value.
       * @param languageId the new languageId
       */
       public void setLanguageId(Long languageId) {
       this.languageId = languageId;
       }
      
       /**
       * Gets the name of this Language.
       * @return the name
       */
       public String getName() {
       return this.name;
       }
      
       /**
       * Sets the name of this Language to the specified value.
       * @param name the new name
       */
       public void setName(String name) {
       this.name = name;
       }
      
       /**
       * Gets the shortname of this Language.
       * @return the shortname
       */
       public String getShortname() {
       return this.shortname;
       }
      
       /**
       * Sets the shortname of this Language to the specified value.
       * @param shortname the new shortname
       */
       public void setShortname(String shortname) {
       this.shortname = shortname;
       }
      
       /**
       * Gets the image of this Language.
       * @return the image
       */
       public byte [] getImage() {
       return this.image;
       }
      
       /**
       * Sets the image of this Language to the specified value.
       * @param image the new image
       */
       public void setImage(byte [] image) {
       this.image = image;
       }
      
       /**
       * Gets the locale of this Language.
       * @return the locale
       */
       public byte [] getLocale() {
       return this.locale;
       }
      
       /**
       * Sets the locale of this Language to the specified value.
       * @param locale the new locale
       */
       public void setLocale(byte [] locale) {
       this.locale = locale;
       }
      
       /**
       * Gets the remark of this Language.
       * @return the remark
       */
       public String getRemark() {
       return this.remark;
       }
      
       /**
       * Sets the remark of this Language to the specified value.
       * @param remark the new remark
       */
       public void setRemark(String remark) {
       this.remark = remark;
       }
      
       /**
       * Gets the userId of this Language.
       * @return the userId
       */
       public BigInteger getUserId() {
       return this.userId;
       }
      
       /**
       * Sets the userId of this Language to the specified value.
       * @param userId the new userId
       */
       public void setUserId(BigInteger userId) {
       this.userId = userId;
       }
      
       /**
       * Gets the recStatus of this Language.
       * @return the recStatus
       */
       public BigInteger getRecStatus() {
       return this.recStatus;
       }
      
       /**
       * Sets the recStatus of this Language to the specified value.
       * @param recStatus the new recStatus
       */
       public void setRecStatus(BigInteger recStatus) {
       this.recStatus = recStatus;
       }
      
       /**
       * Gets the regDate of this Language.
       * @return the regDate
       */
       public Date getRegDate() {
       return this.regDate;
       }
      
       /**
       * Sets the regDate of this Language to the specified value.
       * @param regDate the new regDate
       */
       public void setRegDate(Date regDate) {
       this.regDate = regDate;
       }
      
       /**
       * Gets the updDate of this Language.
       * @return the updDate
       */
       public Date getUpdDate() {
       return this.updDate;
       }
      
       /**
       * Sets the updDate of this Language to the specified value.
       * @param updDate the new updDate
       */
       public void setUpdDate(Date updDate) {
       this.updDate = updDate;
       }
      
       /**
       * Gets the languagePropertyCollection of this Language.
       * @return the languagePropertyCollection
       */
       public Collection<LanguageProperty> getLanguagePropertyCollection() {
       return this.languagePropertyCollection;
       }
      
       /**
       * Sets the languagePropertyCollection of this Language to the specified value.
       * @param languagePropertyCollection the new languagePropertyCollection
       */
       public void setLanguagePropertyCollection(Collection<LanguageProperty> languagePropertyCollection) {
       this.languagePropertyCollection = languagePropertyCollection;
       }
      
       /**
       * Returns a hash code value for the object. This implementation computes
       * a hash code value based on the id fields in this object.
       * @return a hash code value for this object.
       */
       @Override
       public int hashCode() {
       int hash = 0;
       hash += (this.languageId != null ? this.languageId.hashCode() : 0);
       return hash;
       }
      
       /**
       * Determines whether another object is equal to this Language. The result is
       * <code>true</code> if and only if the argument is not null and is a Language object that
       * has the same id field values as this object.
       * @param object the reference object with which to compare
       * @return <code>true</code> if this object is the same as the argument;
       * <code>false</code> otherwise.
       */
       @Override
       public boolean equals(Object object) {
       // TODO: Warning - this method won't work in the case the id fields are not set
       if (!(object instanceof Language)) {
       return false;
       }
       Language other = (Language)object;
       if (this.languageId != other.languageId && (this.languageId == null || !this.languageId.equals(other.languageId))) return false;
       return true;
       }
      
       /**
       * Returns a string representation of the object. This implementation constructs
       * that representation based on the id fields.
       * @return a string representation of the object.
       */
       @Override
       public String toString() {
       return "com.magti.seam.beans.billAdmin.Language[languageId=" + languageId + "]";
       }
      }
      


      3. and also i have Local Stateless session bean
      @Local(BillAdminManagement.class)
      @Stateless
      @Name("billAdmin")
      public class BillAdminManagementBean implements BillAdminManagement{
      
       @PersistenceContext
       private EntityManager entManager;
      
       @Logger
       private Log log;
      
       public List<Language> getLanguages() throws Exception {
       try {
       List<Language> list = entManager.createNamedQuery("Language.getAllLanguages").getResultList();
       log.info("List Size = "+list.size());
       return null;
       } catch (Exception e) {
       log.error("Error While Getting Languages",e);
       throw e;
       }
       }
      }
      


      how i can fill dropdownbox component from this method on page startup?

      Regards,
      Paata.




        • 1. Re: Fill selectOneMenu on page load.
          pmuir

          try

          <h:selectOneMenu value="#{foo.bar}">
           <s:selectItems value="#{billAdmin.languages}" var="language" label="#{language.name}" />
           <ec:convertEntity>
          </h:selectOneMenu>


          • 2. Re: Fill selectOneMenu on page load.

            hi petemuir.
            i tryed this:

            1.my session bean :

            @Local(BillAdminManagement.class)
            @Stateless
            @Name("billAdmin")
            @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
            public class BillAdminManagementBean implements BillAdminManagement{
            
             @PersistenceContext
             private EntityManager entManager;
            
             @Logger
             private Log log;
            
             private List<SelectItem> langItems;
            
             Converter converter;
            
             public List<Language> getlangItems() throws Exception {
             try {
             List<Language> list = entManager.createNamedQuery("Language.getAllLanguages").getResultList();
             langItems = new ArrayList<SelectItem>(list.size());
             for (int i = 0; i < list.size(); i++) {
             SelectItem item = new SelectItem();
             item.setValue(list.get(i));
             item.setLabel(list.get(i).getName());
             item.setDescription("Blaaa");
             langItems.add(item);
             }
             log.info("List Size = "+list.size());
             return list;
             } catch (Exception e) {
             e.printStackTrace();
             log.error("Error While Getting Languages",e);
             throw e;
             }
             }
             public Converter getConverter() {
             converter = new ReferenceConverter(langItems);
             return converter;
             }
            }
            
            

            2. and also i have ReferenceConverter class from here
            http://wiki.jboss.org/wiki/Wiki.jsp?page=SelectItems

            3. and here is fragment from my page :
            <h:selectOneMenu styleClass="font_style" required="true" value="" converter="#{billAdmin.converter}">
             <s:selectItems value="#{billAdmin.langItems}" var="langItems" label="#{language.name}" />
             </h:selectOneMenu>
            


            but i got an error :
            Value is no String (class=com.magti.seam.beans.billAdmin.Language, value=English) and component loginForm:_id24with path: {Component-Path : [Class: javax.faces.component.UIViewRoot,ViewId: /login.xhtml][Class: javax.faces.component.html.HtmlForm,Id: loginForm][Class: javax.faces.component.html.HtmlSelectOneMenu,Id: _id24]} does not have a Converter
            


            • 3. Re: Fill selectOneMenu on page load.
              pmuir

              That (obviously) is a problem with your converter. Either try the entityConverter as I suggested or take a look at the Seam ui-example for how to write a suitable converter.

              • 4. Re: Fill selectOneMenu on page load.

                 

                "petemuir" wrote:
                That (obviously) is a problem with your converter. Either try the entityConverter as I suggested or take a look at the Seam ui-example for how to write a suitable converter.


                ok i tryed to write own converter, i got it from wiki :
                1.Converter
                import java.io.Serializable;
                import java.util.List;
                
                import javax.faces.component.UIComponent;
                import javax.faces.context.FacesContext;
                import javax.faces.convert.Converter;
                import javax.faces.convert.ConverterException;
                
                public class ReferenceConverter implements Converter, Serializable{
                 private static final long serialVersionUID = -6268456071880135380L;
                 private static final String EMPTY = "__EMPTY__";
                 private List<Object> values;
                 public ReferenceConverter(List vals) {
                 this.values = vals;
                 }
                 public String getAsString(FacesContext facesContext,
                 UIComponent component,
                 Object obj) {
                 if (obj == null) return EMPTY;
                 if ("-1".equals(obj)) return EMPTY;
                
                 String val = String.valueOf( obj.hashCode() );
                 return val;
                 }
                 public Object getAsObject(FacesContext facesContext,
                 UIComponent component,
                 String str)
                 throws ConverterException {
                 if (str == null || str.length()==0 || EMPTY.equals(str)) {
                 return null;
                 }
                 int hash = Integer.valueOf(str).intValue();
                 for (Object val : values) {
                 if ( val!=null && val.hashCode()==hash ) {
                 return val;
                 }
                 }
                 return null;
                 }
                }
                


                2. i posted session bean above;
                3 and jps :

                <h:selectOneMenu styleClass="font_style" required="true" value="" converter="#{billAdmin.converter}">
                <s:selectItems value="#{billAdmin.langItems}" var="langItems" label="#{langItems.name}" />
                </h:selectOneMenu>
                


                what i did wrong ?

                i could not find about converter into seam-ui examples.


                • 5. Re: Fill selectOneMenu on page load.
                  pmuir

                   

                  "grdzeli_kaci" wrote:
                  i could not find about converter into seam-ui examples.


                  examples/ui/src/org/jboss/seam/example/ui/Converters

                  • 6. Re: Fill selectOneMenu on page load.

                     

                    "petemuir" wrote:
                    "grdzeli_kaci" wrote:
                    i could not find about converter into seam-ui examples.


                    examples/ui/src/org/jboss/seam/example/ui/Converters


                    i've downloaded both of them jboss-seam-1.1.5.GA and jboss-seam-1.1.5.GA.
                    both of them contains examples package but none of them does not contains ui folder.

                    is there any other examples download link ?


                    • 7. Re: Fill selectOneMenu on page load.

                      Great Thanks petemuir,

                      i got seam from CVS and i found your examples and all works fine.