3 Replies Latest reply: Apr 23, 2012 11:50 PM by gebuh RSS

    How do you use s:selectItems on List of String?

    Nicholas Cruz Newbie

      Hi,

         I am using  seam 2.2.2.Final on JBoss AS 5. I am working on a multi-page wizard.On my first page, user will be able to enter several business names separated by a new line on a textarea.

              {code:xml}

                    <s:decorate id="businessNameTextAreaField" template="layout/edit.xhtml">

                              <ui:define name="label">Business Names</ui:define>

                              <h:inputTextarea id="businessNameTextArea"

                                             cols="80"

                                             rows="3"

                                         required="true"

                                            value="#{businessNameHome.instance.businessNameTextArea}"/>

                      </s:decorate>

             {code}

       

      Upon submission of the page, the system parses the inputed value and splits it into a list of strings

            {code}

           

                public String checkBusinessNames(){

                      String businessNameTextArea = this.getInstance().getbusinessNameTextArea();

                      String[] businessNameTextAreaArray = businessNameTextArea.split("\\n");

                     

                      List<SelectItem> businessNameChoices = new ArrayList<SelectItem>();

                                     

                      for(String businessNameText: businessNameTextAreaArray){           

                          businessNameChoices.add(new SelectItem(businessNameText));

                      }

                     

                      this.getInstance().setBusinessNameChoices(businessNameChoices);

                      return "valid";

                }

          {code}

       

      The user is then asked to select from the list of valid business names to register

          {code:xml}

      <s:decorate id="businessNameRegisterListField" template="layout/edit.xhtml">

           <ui:define name="label">Business Name</ui:define>

           <h:selectManyCheckbox  value="#{businessNameHome.instance.selectedbusinessName}" layout="pageDirection" immediate="true" >

           <s:selectItems value="#{businessNameHome.instance.businessNameChoices}" var="bn" label="#{bn.label}" />                                                                                      </h:selectManyCheckbox>                            

            </s:decorate>

      {code}

       

      selectedbusinessName is of type String while businessNameChoices is of List<SelectItem>

       

      Upon submission of the page, what is submitted as business names is something like this: javax.faces.model.SelectItem@135aa7c

       

      I have tried putting an itemValue on the s:selectItems but I get another error which is "Value is not valid"

        {code:xml}  <s:selectItems value="#{businessNameHome.instance.businessNameChoices}" var="bn" label="#{bn.label}" itemValue="#{bn.value}"/>  {code}

       

      Tried to use <s:convertEntity> but gets a NumberFormat exeption

       

      I have also tried to create my own converter

      {code}

      public class BusinessNameBeanConverter implements javax.faces.convert.Converter {

             

                  @Override

                  public Object getAsObject(FacesContext context, UIComponent cmp, String value) {

                      // TODO Auto-generated method stub

                      System.out.println("getAsObject "+value);

                      return value;

                  }

             

                  @Override

                  public String getAsString(FacesContext context, UIComponent cmp, Object value) {

                      // TODO Auto-generated method stub

                      System.out.println("getAsString "+((SelectItem)value).getValue());

                      return ((SelectItem)value).getValue();

                  }

             

              }

         

      {code}

       

      but I still get the same "Value is not valid" error.

       

      I don't know what to do anymore. Please help.

       

       

       

      Please help! Thanks,

      NIicholas

        • 1. Re: How do you use s:selectItems on List of String?
          gebuh Newbie

          I'm a little confused how you're doing this, but shouldn't your businessNameChoices be a list of Strings and not SelectItems?

          Change the list type and try this( it's a string, not an object so it'll print out the actual name)

          <s:selectItems value="#{businessNameHome.instance.businessNameChoices}" var="bn"/>
          
          • 2. Re: How do you use s:selectItems on List of String?
            Nicholas Cruz Newbie

            Hi Beth,

             

              Thanks. I have made the changes but I still get a "Value is not valid" error. It says here https://community.jboss.org/wiki/SeamProblemsFAQ that it should be in a long running conversation but it is already in a long running conversation as seen by the status message on the page "Conversation: id = 5, long running". If I check only on one checkbox, it submits successfully with the correct data. But if I check multiple boxes, it returns that error.

             

            Thanks!

            • 3. Re: How do you use s:selectItems on List of String?
              gebuh Newbie

              Here's your component:

              <s:decorate id="businessNameRegisterListField" template="layout/edit.xhtml">
                   <ui:define name="label">Business Name</ui:define>
                   <h:selectManyCheckbox  value="#{businessNameHome.instance.selectedbusinessName}" layout="pageDirection" immediate="true" >
                   <s:selectItems value="#{businessNameHome.instance.businessNameChoices}" var="bn" label="#{bn.label}" />                                                                                      
                   </h:selectManyCheckbox>                             
               </s:decorate> 
              

               

              selectManyCheckbox value is what you're going to set - so your choices will populate this value

              selectItems value is the list your selecting from

               

              It appears that selectedbusinessName is of type String, but it should be a list<String>.

               

              Can you post the methods (formatted please, the raw code is hard to read) from your backing bean?

               

               

               

              Nicholas Cruz wrote:

               

              Hi Beth,

               

                Thanks. I have made the changes but I still get a "Value is not valid" error. It says here https://community.jboss.org/wiki/SeamProblemsFAQ that it should be in a long running conversation but it is already in a long running conversation as seen by the status message on the page "Conversation: id = 5, long running". If I check only on one checkbox, it submits successfully with the correct data. But if I check multiple boxes, it returns that error.

               

              Thanks!