11 Replies Latest reply on Jul 10, 2007 6:25 PM by Pete Muir

    converter and dropdowns

    lee jones Newbie

      hello im trying to understand converters and also their place in dropdowns.. I have seen many posts about seams new s:convertEntity but i dont want an entity. Im thinking i want something like this..

      a base class that all objects of the type i want to display will extend.

      
      public class ReferenceData {
       private String name;
       private Integer id;
      
       public ReferenceData(Integer id, String name) {
       this.id = id;
       this.name = name;
       }
      
       public Integer getId() {
       return id;
       }
      
       public void setId(Integer id) {
       this.id = id;
       }
      
       public String getName() {
       return name;
       }
      
       public void setName(String name) {
       this.name = name;
       }
      }
      
      


      then i have a class that extends this. with nothing else in it.

      i then use this to make my list of objects to display on the screen


      public static final Vector<JobType> getMyJobTypes() {
       Vector<JobType> types = new Vector<JobType>();
       types.add(new JobType(new Integer(1), "hello1"));
       types.add(new JobType(new Integer(2), "hello2"));
       types.add(new JobType(new Integer(3), "hello3"));
      
       return types;
       }
      


      with this

      <h:selectOneMenu value="#{jobManagerHome.selectedJobType}" converter="#{converters.referenceDataConverter}">
       <s:selectItems
       value="#{jobManagerHome.jobDetailTypesRefData}"
       var="jt"
       label="#{jt.name}" />
       <a:support event="onchange" />
      
       </h:selectOneMenu>
      



      this is the converter

      
      @Name("referenceDataConverter")
      @Intercept(NEVER)
      @Converter
      public class ReferenceDataConverter implements javax.faces.convert.Converter {
       List<ReferenceData> list;
      
       public String getAsString(FacesContext facesContext, UIComponent component, Object obj) {
       if (obj == null)
       return null;
      
       ReferenceData referenceData = (ReferenceData) obj;
       String val = String.valueOf(referenceData.getName());
       return val;
       }
      
       public Object getAsObject(FacesContext facesContext, UIComponent component, String string)
       throws ConverterException {
       if (string == null || string.length() == 0) {
       return null;
       }
      
       int id = Integer.valueOf(string).intValue();
       for (ReferenceData referenceData : list) {
       if (referenceData.getId() == id) {
       return referenceData;
       }
       }
      
       return null;
       }
      
      }
      


      however i get this error

      
      java.lang.IllegalArgumentException: Value is no String (class=uk.co.anotion.entity.jobs.JobType, value=uk.co.anotion.entity.jobs.JobType@5d8897) and component jobDetail:_id0with path: {Component-Path : [Class: org.ajax4jsf.framework.ajax.AjaxViewRoot,ViewId: /JobDetailEditComponent.xhtml][Class: javax.faces.component.html.HtmlForm,Id: jobDetail][Class: org.richfaces.component.html.HtmlPanel,Id: jobSelectionPanel][Class: javax.faces.component.html.HtmlSelectOneMenu,Id: _id0]} does not have a Converter
       at org.apache.myfaces.shared_impl.renderkit.RendererUtils.getConvertedStringValue(RendererUtils.java:536)
       at org.apache.myfaces.shared_impl.renderkit.RendererUtils.getConvertedStringValue(RendererUtils.java:555)
       at org.apache.myfaces.shared_impl.renderkit.html.HtmlRendererUtils.renderSelectOptions(HtmlRendererUtils.java:398)
       at org.apache.myfaces.shared_impl.renderkit.html.HtmlRendererUtils.internalRenderSelect(HtmlRendererUtils.java:298)
       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.ajax4jsf.framework.renderer.RendererBase.renderChild(RendererBase.java:286)
       at org.ajax4jsf.framework.renderer.RendererBase.renderChildren(RendererBase.java:262)
       at org.richfaces.renderkit.html.PanelRenderer.doEncodeChildren(PanelRenderer.java:189)
       at org.richfaces.renderkit.html.PanelRenderer.doEncodeChildren(PanelRenderer.java:184)
       at org.ajax4jsf.framework.renderer.RendererBase.encodeChildren(RendererBase.java:121)
       at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:524)
       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.FaceletViewHandler.renderView(FaceletViewHandler.java:573)
       at org.ajax4jsf.framework.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:108)
       at org.ajax4jsf.framework.ajax.AjaxViewHandler.renderView(AjaxViewHandler.java:229)
       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.ExceptionFilter.doFilter(ExceptionFilter.java:57)
      
      



      any help welcome

        • 1. Re: converter and dropdowns
          Pete Muir Master

          Try

          <h:selectOneMenu value="#{jobManagerHome.selectedJobType}" converter="referenceDataConverter">
           <s:selectItems value="#{jobManagerHome.jobDetailTypesRefData}" var="jt" label="#{jt.name}" />
           <a:support event="onchange" />
          </h:selectOneMenu>


          • 2. Re: converter and dropdowns
            lee jones Newbie

            yes this works now, however i am finding it difficult to achieve my objective. When the user selects an item i want to be able to find out the value that has been selected and use this to control what is rendered on the screen..

            i assumed that
            <h:selectOneMenu value="#{jobManagerHome.selectedJobType}">

            would set the value of selectedJobType on my Home class but it does not seem to be working..

            can anyone advise

            thank you.

            • 3. Re: converter and dropdowns
              lee jones Newbie

              hmm im still stuck.. do i need to use injection to get my values back?

              • 4. Re: converter and dropdowns
                lee jones Newbie

                can no one tell me how to access my selected value in a dropdown from my home class i must be missing a very basic concept here :(

                • 5. Re: converter and dropdowns
                  Pete Muir Master

                  Post the jobManagerHome component.

                  • 6. Re: converter and dropdowns
                    lee jones Newbie

                     

                    
                    @Name("jobManagerHome")
                    public class JobManagerHome extends EntityHome<JobDetail> {
                    
                    
                     private int MODE = 0;
                    
                     public static final int jobDetailMode = 1;
                     public static final int tankeringMode = 2;
                     public static final int jettingMode = 3;
                     public static final int cctvMode = 4;
                    
                    
                    
                     JobType selectedJobType;
                    
                     @In(create = true)
                     JobHome jobHome;
                    
                     public JobManagerHome() {
                    
                     }
                    
                     public void setJobDetailId(Integer id) {
                     setId(id);
                     }
                    
                     public Integer getJobDetailId() {
                     return (Integer) getId();
                     }
                    
                     @Override
                     protected JobDetail createInstance() {
                     JobDetail jobDetail = new JobDetail();
                     return jobDetail;
                     }
                    
                     public void wire() {
                     Job job = jobHome.getDefinedInstance();
                     if (job != null) {
                     getInstance().setJob(job);
                     }
                     }
                    
                     public boolean isWired() {
                     if (getInstance().getJob() == null)
                     return false;
                     return true;
                     }
                    
                     public JobDetail getDefinedInstance() {
                     return isIdDefined() ? getInstance() : null;
                     }
                    
                     public void addToJob(){
                     System.out.println("attempt add to job");
                     getInstance().setJob(jobHome.getInstance());
                     jobHome.getInstance().addJobDateail(getInstance());
                     persist();
                     System.out.println("finished add to job");
                     }
                    
                    
                    
                     public void changed(ValueChangeEvent event){
                     System.out.println("changed");
                     if(getInstance() != null){
                     System.out.println(getInstance().getJob_type());
                     }
                    
                     }
                     public void test(){
                     System.out.println("test");
                     }
                    
                     public void isTest(){
                     System.out.println("is test");
                     }
                    
                     public void setJobDetailMode(int mode) {
                     MODE = mode;
                     }
                    
                     // provides the drop down info
                     public Vector<JobType> getJobDetailTypesRefData() {
                     return JobDetailTypesRefData.getJobDetailTypes();
                    
                     }
                    
                    
                     public JobType getSelectedJobType() {
                     return selectedJobType;
                     }
                    
                     public void setSelectedJobType(JobType selectedJobType) {
                     this.selectedJobType = selectedJobType;
                     }
                    
                     public boolean isJobDetailMode(){
                     return true;
                     /*if(selectedJobType != null && selectedJobType.getId()==2){
                     return true;
                     }return false;*/
                     }
                    
                    
                    
                    
                    
                    }
                    
                    


                    • 7. Re: converter and dropdowns
                      Pete Muir Master

                      So the problem is that the setter doesn't get called? Use your debugger to check. Do you get any JSF error messges from h:mesasges?

                      • 8. Re: converter and dropdowns
                        lee jones Newbie

                        no when i debug it goes into the get method as the page renders but when i change the value it does not call the set.. cant see any messages.

                        
                        <rich:panel id="jobSelectionPanel">
                        
                         <h:selectOneMenu id="selectedJobType" value="#{jobManagerHome.selectedJobType}" converter="#{referenceDataConverter}" >
                        
                         <s:selectItems
                         value="#{jobManagerHome.jobDetailTypesRefData}"
                         var="jt"
                         label="#{jt.name}"
                         />
                        
                        
                        
                         <a:support event="onchange" reRender="jobSelectionPanel" action="submit()"/>
                        
                         </h:selectOneMenu>
                        
                         </rich:panel>
                        


                        my screen now looks like this was trying out different things..

                        • 9. Re: converter and dropdowns
                          Pete Muir Master

                          For a start the action parameter on a:support should be a method binding not JS. Try getting this to work *without* ajax4jsf in the picture - just use a form with a commandButton - and then add a4j back in.

                          • 10. Re: converter and dropdowns
                            lee jones Newbie

                            well i got rid of the tag and it still didnt work after much fiddling about i have found a solution..

                            for some reason when i used an s:button i could not get the name value but iafter changing this to h:comandButton it worked

                            can anyone explain whats going on here and also i cant seem to see any s: type tags in exadel has this support been added to the jboss tools stuff? or perhaps there is a way to add them manually


                            anyway thanks for the help.

                            • 11. Re: converter and dropdowns
                              Pete Muir Master

                              All over the places (the docs, the SeamProblemsFAQ, this forum many times) it is stated that s:button/s:link don't submit the form. This is why its always worth submitting a simple but working bean/page as then you get saved "much fiddling" ;)

                              I had jboss tools picking s: tags at one point (and in fact any custom tags in your classpath I think).