8 Replies Latest reply on Feb 8, 2008 1:24 AM by nickarls

    <s:selectItems> label and value

    monkeyden

      I don't see a way to specify a different label and value when using <s:selectItems>. I see the value attribute ties in to the list of results. Functionally everything works fine. I would just prefer to use region.regionCode as the value and region.regionName as the label. Am I missing something? Thanks.


      <h:selectOneMenu id="regionSelect" value="#{contentAdmin.selectedRegion}" styleClass="formElement">
       <s:selectItems var="region" value="#{regions.resultList}" label="#{region.regionCode}"/>
       <f:converter converterId="entityConverter" />
      </h:selectOneMenu>



      <framework:entity-query name="pages" ejbql="select p from Page p" order="pageCode"/>
      <framework:entity-query name="regions" ejbql="select r from Region r" order="regionCode"/>
      <component name="entityConverter"
       class="org.jboss.seam.ui.converter.EntityConverter">
       <property name="entityManager">#{entityManager}</property>
      </component>


        • 1. Re: <s:selectItems> label and value
          dhinojosa

          Well, if I understand you correctly, the value cannot be adjusted because Seam needs that to be able to convert that value into an entity. But I don't see why you can't use region.regionName in the label. The label is free for your use.

          • 2. Re: <s:selectItems> label and value
            monkeyden

            I'm likely misunderstanding it's usage. It now seems as though the value of the component becomes an instance of the Entity, for example:

            <h:selectOneMenu id="regionSelect" value="#{contentAdmin.selectedRegion}" styleClass="formElement">
             <s:selectItems var="region" value="#{regions.resultList}" label="#{region.regionCode}" noSelectionLabel="Please select a region..."/>
             <f:converter converterId="entityConverter" />
            </h:selectOneMenu>


            public Region getSelectedRegion() {
             return selectedRegion;
            }
            public void setSelectedRegion(Region selectedRegion) {
             this.selectedRegion = selectedRegion;
            }


            Is this true? I was attempting to use the PK of the entity (a String).

            Thanks

            • 3. Re: <s:selectItems> label and value
              dhinojosa

              'instance of the Entity' is a weird term to me....

              Seam, when it was in CR, created an arbitrary value to represent the entity and made that number the value of the selectitem when it was rendered. The convertEntity tags takes that choice, grabs it's corresponding entity and injects that into whatever value you set it to.

              • 4. Re: <s:selectItems> label and value
                pmuir

                Dan is correct. However 2.0.1 allows you two options to customize the value. If you don't use the entity converter, use the itemValue attribute on s:selectItems. If you do, you need to implement a custom entity loading strategy. I'll blog about that soon.

                • 5. Re: <s:selectItems> label and value
                  nickarls

                   

                  "pete.muir@jboss.org" wrote:
                  Dan is correct. However 2.0.1 allows you two options to customize the value. If you don't use the entity converter, use the itemValue attribute on s:selectItems. If you do, you need to implement a custom entity loading strategy. I'll blog about that soon.


                  What do you mean by the "If you don't use the entity converter, use the itemValue attribute on s:selectItems"?

                  I'm trying to do a case of an entity

                  @Entity
                  public class User {
                   @Id @GeneratedValue
                   private Integer id;
                   private String code;
                   private String name
                   ... getters/setters/hashCode/equals
                  


                  and would like to do something like
                  <f:selectOneMenu value="#{userBean.user.code}" required="true">
                   <s:selectItems
                   value=#{userBean.userList}
                   var="user"
                   label="#{user.name}"
                   itemValue="#{user.code}"
                   >
                   <s:convertEntity/>
                   </selectItems>
                  </f:selectOneMenu>
                  


                  where userBean.userList is a List

                  should this be doable (displaying name, selecting code) with convertEntity?

                  I keep getting "Value is required" on it despite of equals/hashcode...


                  • 6. Re: <s:selectItems> label and value
                    nickarls

                    The strange this is that equals() and hashCode() are not being accessed at all. Neither is the setter of the value. It just makes up that the value is null although the selectbox option value and label are correct...

                    • 7. Re: <s:selectItems> label and value
                      dhinojosa

                      @nickarls

                      You have to choose either of these options.

                      Note:removed <s:convertEntity> because a java.lang.String is not an entity

                      <f:selectOneMenu value="#{userBean.user.code}" required="true">
                       <s:selectItems
                       value=#{userBean.userList}
                       var="user"
                       label="#{user.name}"
                       itemValue="#{user.code}"
                       >
                       </selectItems>
                      </f:selectOneMenu>
                      


                      or

                      Note: I am using <s:convertEntity> because User is indeed an entity.
                      <f:selectOneMenu value="#{userBean.user}" required="true">
                       <s:selectItems
                       value=#{userBean.userList}
                       var="user"
                       label="#{user.name}"
                       >
                       <s:convertEntity/>
                       </selectItems>
                      </f:selectOneMenu>
                      


                      • 8. Re: <s:selectItems> label and value
                        nickarls

                        Thanks, that did it!

                        I was under the impression that I had to use the entityConverter since the backing data weren't SelectItems, but it seems that the entityConverter (and the SMPC to avoid LIE:s) are only required when actually inserting an entity?

                        That pretty much moves my SelectItem usage into the legacy-hierarchy, I won't miss it.