7 Replies Latest reply on Mar 10, 2007 4:36 PM by Christopher no

    Problem with s:selectItems

    Christopher no Newbie

      Hi,

      I can't force s:selectItems to work. Even the easiest example doesn't work.

      Name("personHome")
      public class PersonHome extends EntityHome<Person> {
      
      
      
      public List<SelectItem> countries = new ArrayList();
      public String country = "";
      
      public void setCountries(List<Country> countries) {}
      
      public List<SelectItem> getCountries(){
      
       for(int i=0;i<3;i++){
       countries.add( new SelectItem());
       }
       countries.get(0).setLabel("Great Britain");
       countries.get(0).setValue("GB");
       countries.get(1).setLabel("Germany");
       countries.get(1).setValue("D");
       countries.get(2).setLabel("Italy");
       countries.get(2).setValue("I");
      
       return countries;
       }
      }
      
      


      And in xhtml:

      <h:selectOneMenu value="#{personHome.country}" >
       <s:selectItems value="#{personHome.countries}" var="country" label="#{country.label}" />
      </h:selectOneMenu>
      
      


      And then I get error:

      Value is no String (class=javax.faces.model.SelectItem, value=javax.faces.model.SelectItem@1e76a88) and component person:_id38with path: {Component-Path : [Class: org.ajax4jsf.framework.ajax.AjaxViewRoot,ViewId: /specialist/spec_registration.xhtml][Class: javax.faces.component.html.HtmlForm,Id: person][Class: org.jboss.seam.ui.UIValidateAll,Id: _id7][Class: org.jboss.seam.ui.UIDecorate,Id: _id37][Class: javax.faces.component.html.HtmlSelectOneMenu,Id: _id38]} does not have a Converter

      Everythig is String so what does the error mean?
      What am I doing wrong?

      Thanks in advance,
      jquery

        • 1. Re: Problem with s:selectItems
          H K Newbie

          Curious, why do you have setCountries() accepting a list of Country while getCountries() is returning a list of SelectItem?

          Anyway, in your code, you are creating instances of SelectItem yourself. If this is what you want, then use the standard JSF <f:selectItems> instead. However, you lose two things by doing that:

          1- Because you will not be using Seam JSF <s:selectItems>, you can not take advantage of the convenient noSelectionLabel attribute.
          2- Your code is now tied to a SelectItem class.

          Let?s assume that you have a class called Country. Then, to use Seam JSF <s:selectItems>, simply return a list of Country and do not wrap them with SelectItem. Also, make sure that the value binding for the <h:selectOneMenu> resolves to a type Country and not String.

          • 2. Re: Problem with s:selectItems
            H K Newbie

            You will also need a converter. If you want to see how it is done, have a look at the ui example that comes with seam. You will find the example under :
            jboss-seam-1.2.0.PATCH1\examples\ui

            • 3. Re: Problem with s:selectItems
              Christopher no Newbie

              Actually the difference between parameters in set and get methods appeared because I was trying different combination of collections.
              It was my mistake.
              Now as you said the I have following code:

              @Name("personHome")
              public class PersonHome extends EntityHome<Person> {
              
               public List<Country> countries = new ArrayList<Country>();
               public long countryId = 1;
              
              
               public long getCountryId() {
               return countryId;
               }
              
               public void setCountryId(long countryId) {
               this.countryId = countryId;
               }
              
               public void setCountries(List<Country> countries) {
              
               }
              
               public List<Country> getCountries(){
               Country country = new Country();
               country.setId(1);
               country.setName("Poland");
               countries.add(country);
              
               return countries;
               }
              }
              


              And xhtml:

              
               <h:selectOneMenu value="#{personHome.countryId}" converter="#{countryConverter.converter}">
               <s:selectItems value="#{personHome.countries}" var="country" label="#{country.name}" />
               </h:selectOneMenu>
              
              


              And a converter which is based on ui seam example:

              
              @Name("countryConverter")
              public class CountryConverter {
               Converter getConverter(){
               return new Converter(){
               @Transactional
               public Object getAsObject(FacesContext arg0, UIComponent arg1, String arg2) throws ConverterException
               {
               if (arg2 == null) {
               return null;
               }
               try {
               return ((EntityManager) Component.getInstance("entityManager")).find(Country.class, Integer.valueOf(arg2));
               } catch (NumberFormatException e) {
               throw new ConverterException("Cannot find selected country", e);
               }
               }
              
               @Transactional
               public String getAsString(FacesContext arg0, UIComponent arg1, Object arg2) throws ConverterException
               {
               if (arg2 instanceof Country)
               {
               Country country = (Country) arg2;
               return (new Long(country.getId())).toString();
               }
               else
               {
               return null;
               }
               }
              
               };
               }
              
              }
              


              But I still get error:

              Value is no String (class=java.lang.Long, value=1) and component person:_id38with path: {Component-Path : [Class: org.ajax4jsf.framework.ajax.AjaxViewRoot,ViewId: /specialist/spec_registration.xhtml][Class: javax.faces.component.html.HtmlForm,Id: person][Class: org.jboss.seam.ui.UIValidateAll,Id: _id7][Class: org.jboss.seam.ui.UIDecorate,Id: _id37][Class: javax.faces.component.html.HtmlSelectOneMenu,Id: _id38]} does not have a Converter
              
              


              I don't know where the error comes from and why the converter is not visible.
              I would apprecite any suggestions.


              jquery

              • 4. Re: Problem with s:selectItems
                Pete Muir Master

                Get Seam from CVS and use s:convertEntity - check out the ui example.

                Btw - you want the value attribute of the select and the value attribute of selectItems to be of type Foo and List respectively - don't make the value point at an Id, a String etc.

                • 5. Re: Problem with s:selectItems
                  Christopher no Newbie

                  I've got Seam from CVS then changed xhtml to:

                  <h:selectOneMenu value="#{personHome.countryId}" >
                   <s:selectItems value="#{personHome.countries}" var="country" label="#{country.name}" />
                   <s:convertEntity/>
                  </h:selectOneMenu>
                  
                  


                  And then I got NPE

                  
                  java.lang.NullPointerException
                   at org.jboss.seam.ui.EntityConverter.getIdFromEntity(EntityConverter.java:116)
                   at org.jboss.seam.ui.EntityConverter.getAsString(EntityConverter.java:171)
                   at org.apache.myfaces.shared_impl.renderkit.RendererUtils.getConvertedStringValue(RendererUtils.java:544)
                   at org.apache.myfaces.shared_impl.renderkit.html.HtmlRendererUtils.getSubmittedOrSelectedValuesAsSet(HtmlRendererUtils.java:321)
                   at org.apache.myfaces.shared_impl.renderkit.html.HtmlRendererUtils.internalRenderSelect(HtmlRendererUtils.java:296)
                   at org.apache.myfaces.shared_impl.renderkit.html.HtmlRendererUtils.renderMenu(HtmlRendererUtils.java:252)
                   at org.apache.myfaces.shared_impl.renderkit.html.HtmlMenuRendererBase.encodeEnd(HtmlMenuRendererBase.java:54)
                   at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:536)
                   at org.jboss.seam.ui.JSF.renderChild(JSF.java:179)
                   at org.jboss.seam.ui.JSF.renderChildren(JSF.java:162)
                   at org.jboss.seam.ui.UIDecorate.encodeChildren(UIDecorate.java:178)
                   at com.sun.facelets.tag.jsf.ComponentSupport.encodeRecursive(ComponentSupport.java:244)
                   at com.sun.facelets.tag.jsf.ComponentSupport.encodeRecursive(ComponentSupport.java:249)
                   at com.sun.facelets.tag.jsf.ComponentSupport.encodeRecursive(ComponentSupport.java:249)
                   at com.sun.facelets.tag.jsf.ComponentSupport.encodeRecursive(ComponentSupport.java:249)
                   at com.sun.facelets.FaceletViewHandler.renderView(FaceletViewHandler.java:573)
                   at org.ajax4jsf.framework.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:101)
                   at org.ajax4jsf.framework.ajax.AjaxViewHandler.renderView(AjaxViewHandler.java:221)
                   at org.apache.myfaces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:384)
                   at javax.faces.webapp.FacesServlet.service(FacesServlet.java:138)
                   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
                   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
                   at org.jboss.seam.web.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:63)
                   at org.jboss.seam.web.RedirectFilter.doFilter(RedirectFilter.java:45)
                   at org.jboss.seam.web.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:49)
                   at org.jboss.seam.debug.hot.HotDeployFilter.doFilter(HotDeployFilter.java:60)
                   at org.jboss.seam.web.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:49)
                   at org.jboss.seam.web.ExceptionFilter.doFilter(ExceptionFilter.java:57)
                   at org.jboss.seam.web.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:49)
                   at org.jboss.seam.web.MultipartFilter.doFilter(MultipartFilter.java:79)
                   at org.jboss.seam.web.SeamFilter$FilterChainImpl.doFilter(SeamFilter.java:49)
                   at org.jboss.seam.web.SeamFilter.doFilter(SeamFilter.java:84)
                   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
                   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
                   at org.ajax4jsf.framework.ajax.xmlfilter.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:75)
                   at org.ajax4jsf.framework.ajax.xmlfilter.BaseFilter.doFilter(BaseFilter.java:213)
                   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
                   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
                   at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
                   at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
                   at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
                   at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
                   at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
                   at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:175)
                   at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:432)
                   at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:74)
                   at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
                   at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
                   at org.jboss.web.tomcat.tc5.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:156)
                   at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
                   at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
                   at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
                   at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
                   at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
                   at org.apache.tomcat.util.net.MasterSlaveWorkerThread.run(MasterSlaveWorkerThread.java:112)
                   at java.lang.Thread.run(Thread.java:595)
                  


                  And why is there NPE connected with getting entity id although I set it on 1 (in code above)?

                  Thanks for any suggestions.

                  • 6. Re: Problem with s:selectItems
                    Pete Muir Master

                     

                    "jquery@interia.pl" wrote:
                    <h:selectOneMenu value="#{personHome.countryId}" >
                     <s:selectItems value="#{personHome.countries}" var="country" label="#{country.name}" />
                     <s:convertEntity/>
                    </h:selectOneMenu>


                    Yup, thats right

                    And why is there NPE connected with getting entity id although I set it on 1 (in code above)?


                    Oops, fixed in CVS, sorry about that


                    • 7. Re: Problem with s:selectItems
                      Christopher no Newbie

                      Thanks a lot, now <s:selectItems> seams to appear on my xhtml page :)

                      jquery