1 2 Previous Next 19 Replies Latest reply on Nov 2, 2008 12:25 AM by Val Sw

    Pls help Issue with displaying persited value for h:selectOneMenu, s:selectItems

    Val Sw Expert
      Values are populated in a dropdown (say Experiment Name) list from resultset. We have "ExperimentTableIndex" which fetches the values and returns resultList.
      Example "Exp 1", "Exp 2", "Exp 3", "Exp 4"... so on

      ISSUE :
      We can create Experiment Name and suppose while creating we selected experiment name is "Exp 3", which is persited correctly.
      Now, when we click "Edit", default value selected should the one of existing experiment name (i.e. "Exp 3"), but default value of dropdown is the first value in list i.e("Exp 1").

      Please suggest I am stuck, how can I handle this while editing, this is xhtml code we use.
      For testing I have placed s:label which displays correct existing value i.e. "Exp 3"

      <s:decorate id="experimentNameDecoration" template="layout/edit.xhtml">
           <ui:define name="label">Experiment Name</ui:define>
           <h:selectOneMenu value="#{quantExperimentHome.instance.experimentTableIndex}" label="#{quantExperimentHome.instance.experimentName}">
            <s:selectItems value="#{experimentTableIndexList.resultList}"
                var="experimentTableIndex" label="#{experimentTableIndex.experimentName}"
                />          
            <s:convertEntity/>                    
           </h:selectOneMenu>
           <s:label value="#{quantExperimentHome.instance.experimentName}"></s:label>                    
      </s:decorate>
        • 1. Re: Pls help Issue with displaying persited value for h:selectOneMenu, s:selectItems
          Ingo Jobling Master

          Try replacing  


          <s:entityConverter



          with



          @Name("experimentTableIndexConverter")
          @org.jboss.seam.annotations.faces.Converter(forClass = ExperimentTableIndex.class)
          public class ExperimentTableIndexConverter implements Converter, Serializable {
          
               private static final long serialVersionUID = 1L;
          
               @In
               private EntityManager entityManager;
               
               // default constructor
               public ExperimentTableIndexConverter(){
               }
               
               @Transactional
               public Object getAsObject(FacesContext context, UIComponent component, String value) {
                    if (value != null) {
                         try {
                              Integer id = Integer.parseInt(value);
                              if (id != null) {
                                   return entityManager.find(ExperimentTableIndex.class, id);
                              }
                         } catch (NumberFormatException e) {
                              throw new ConverterException("Invalid id, must be an Integer value: " + value);
                         }
                    }
                    return null;
               }
          
               public String getAsString(FacesContext context, UIComponent component, Object value) {
                    if (value instanceof ExperimentTableIndex) {
                         ExperimentTableIndex experimentTableIndex = (ExperimentTableIndex) value;
                         return experimentTableIndex.getId().toString();
                    } else {
                         return null;
                    }
               }
          
          }
          
          


          • 3. Re: Pls help Issue with displaying persited value for h:selectOneMenu, s:selectItems
            Val Sw Expert
            Hi Ingo,

            I am inthe process of copying your code, really impressed by your wonderful approach... need you to verify ...

            Do I need to change anything in xhtml code also?
            xhtml
            -----
            <s:decorate id="experimentNameDecoration" template="layout/edit.xhtml">
            <ui:define name="label">Experiment Name</ui:define>
            <h:selectOneMenu value="#{quantExperimentHome.instance.experimentTableIndex}" label="#
                  {quantExperimentHome.instance.experimentName}">
            <s:selectItems value="#{experimentTableIndexList.resultList}"
            var="experimentTableIndex" label="#{experimentTableIndex.experimentName}"/>          
            <s:convertEntity/>                    
            </h:selectOneMenu>
            </s:decorate>



            This is "ExperimentTableIndexConverter", placed in src/action package, please check if imports are correct, specially for converter.
            ---------------------------------------------------------------------
            import java.io.Serializable;
            import javax.faces.component.UIComponent;
            import javax.faces.context.FacesContext;
            import javax.faces.convert.Converter;
            import javax.faces.convert.ConverterException;
            import javax.persistence.EntityManager;
            import org.jboss.seam.annotations.In;
            import org.jboss.seam.annotations.Name;
            import org.jboss.seam.annotations.Transactional;
            import com.ExperimentTableIndex;

            @Name("experimentTableIndexConverter")
            @org.jboss.seam.annotations.faces.Converter(forClass = ExperimentTableIndex.class)
            public class ExperimentTableIndexConverter implements Converter, Serializable {

                 private static final long serialVersionUID = 1L;

                 @In
                 private EntityManager entityManager;
                 
                 // default constructor
                 public ExperimentTableIndexConverter(){
                 }
                 
                 @Transactional
                 public Object getAsObject(FacesContext context, UIComponent component, String value) {
                      if (value != null) {
                           try {
                                Integer id = Integer.parseInt(value);
                                if (id != null) {
                                     return entityManager.find(ExperimentTableIndex.class, id);
                                }
                           } catch (NumberFormatException e) {
                                throw new ConverterException("Invalid id, must be an Integer value: " + value);
                           }
                      }
                      return null;
                 }

                 public String getAsString(FacesContext context, UIComponent component, Object value) {
                      if (value instanceof ExperimentTableIndex) {
                           ExperimentTableIndex experimentTableIndex = (ExperimentTableIndex) value;
                           return experimentTableIndex.getExpTableId().toString();
                      } else {
                           return null;
                      }
                 }

            }
            • 4. Re: Pls help Issue with displaying persited value for h:selectOneMenu, s:selectItems
              Ingo Jobling Master

              Just remove <s:convertEntity/> in the .xhtml


              I think your imports are OK, but just in case, here are mine:



              import java.io.Serializable;
              
              import javax.faces.component.UIComponent;
              import javax.faces.context.FacesContext;
              import javax.faces.convert.Converter;
              import javax.faces.convert.ConverterException;
              import javax.persistence.EntityManager;
              
              import org.jboss.seam.annotations.In;
              import org.jboss.seam.annotations.Name;
              import org.jboss.seam.annotations.Transactional;
              


              • 5. Re: Pls help Issue with displaying persited value for h:selectOneMenu, s:selectItems
                Val Sw Expert
                I tried exactly the same code as above only changed id to Long, refer ExperimentTableIndex entity also.

                We have reference to ExperimentTableIndex entity in QuantExperiment.java with getters & setters of ExperimentTableIndex, dropdown value is persisted in QuantExperiment table.... is it related to it... not sure :(

                Dropdown still displays the first value in the list... pls save me....

                xhtml:
                ------
                <s:decorate id="experimentNameDecoration" template="layout/edit.xhtml">
                     <ui:define name="label">Experiment Name</ui:define>
                         <h:selectOneMenu value="#{quantExperimentHome.instance.experimentTableIndex}"
                            label="#{quantExperimentHome.instance.experimentName}">
                        <s:selectItems value="#{experimentTableIndexList.resultList}"
                         var="experimentTableIndex" label="#{experimentTableIndex.experimentName}"/>                                       
                     </h:selectOneMenu>
                         <s:label value="#{quantExperimentHome.instance.experimentName}"></s:label>
                </s:decorate>

                ExperimentTableIndexConverter.java
                ----------------------------------
                import java.io.Serializable;
                import javax.faces.component.UIComponent;
                import javax.faces.context.FacesContext;
                import javax.faces.convert.Converter;
                import javax.faces.convert.ConverterException;
                import javax.persistence.EntityManager;
                import org.jboss.seam.annotations.In;
                import org.jboss.seam.annotations.Name;
                import org.jboss.seam.annotations.Transactional;
                import com.entity.ExperimentTableIndex;

                @Name("experimentTableIndexConverter")
                @org.jboss.seam.annotations.faces.Converter(forClass = ExperimentTableIndex.class)
                public class ExperimentTableIndexConverter implements Converter, Serializable {

                     private static final long serialVersionUID = 1L;

                     @In
                     private EntityManager entityManager;
                     
                     // default constructor
                     public ExperimentTableIndexConverter(){
                     }
                     
                     @Transactional
                     public Object getAsObject(FacesContext context, UIComponent component, String value) {
                          if (value != null) {
                               try {
                                    //Integer id = Integer.parseInt(value);
                                    Long id = Long.parseLong(value);
                                    if (id != null) {
                                         return entityManager.find(ExperimentTableIndex.class, id);
                                    }
                               } catch (NumberFormatException e) {
                                    throw new ConverterException("Invalid id, must be an Integer value: " + value);
                               }
                          }
                          return null;
                     }

                     public String getAsString(FacesContext context, UIComponent component, Object value) {
                          if (value instanceof ExperimentTableIndex) {
                               ExperimentTableIndex experimentTableIndex = (ExperimentTableIndex) value;
                               return experimentTableIndex.getExpTableId().toString();
                          } else {
                               return null;
                          }
                     }

                }


                ExperimentTableIndex.java Entity
                --------------------------
                @Entity(name = "ExperimentTableIndex")
                @Table(name = "EXPERIMENT_TABLE_INDEX")
                public class ExperimentTableIndex
                    implements Equals, HashCode, ToString
                {
                    protected String experimentName;
                    protected Long expTableId;
                    .....
                     @Id
                    @Column(name = "EXP_TABLE_ID")
                    public Long getExpTableId() {
                        return expTableId;
                    }....

                --------------------
                • 6. Re: Pls help Issue with displaying persited value for h:selectOneMenu, s:selectItems
                  Ingo Jobling Master

                  Maybe a problem with the implementation of equals ?  Try removing it?

                  • 7. Re: Pls help Issue with displaying persited value for h:selectOneMenu, s:selectItems
                    Val Sw Expert

                    Thanks for replying Ingo...


                    Did you mean removing equals from ExperimentTableIndex.java entity?

                    • 8. Re: Pls help Issue with displaying persited value for h:selectOneMenu, s:selectItems
                      Ingo Jobling Master

                      Yes, I would try removing equals from ExperimentTableIndex.java entity, just to confirm that it is not the problem.


                      If that doesn't work another thing is to verify that the id of the selection is being passed in the request (POST or GET), for example, by using HTTP Headers or Firebug plugin for Firefox.


                      • 9. Re: Pls help Issue with displaying persited value for h:selectOneMenu, s:selectItems
                        Ingo Jobling Master

                        Or maybe just set a breakpoint in the equals method to confirm that it is matching one of the entities in the list (JSF enforces a validation that the selected item must be present in the list).  If this is the problem you will see a one line message in the console log (which is very easy to miss).

                        • 10. Re: Pls help Issue with displaying persited value for h:selectOneMenu, s:selectItems
                          Val Sw Expert
                          Here is the complete update....

                          When I click on edit, page loads and prints all the Ids in console of experiment table names in "ExperimentTableIndexConverter.getAsString()" method
                          ----------------------------------
                          System.out.println("ExperimentTableIndexConverter.getAsString(), getExpTableId = "+experimentTableIndex.getExpTableId().toString());
                          ----------------------------------
                          When I click update after changing the drop-down value, it prints value as
                          "ExperimentTableIndexConverter.getAsObject(), value = 4" in "ExperimentTableIndexConverter.getAsObject()" method

                          Which is in the list of entities...

                          The behavior is same if I use equals or not.

                          Anyother thing we are missing ? I have posted all the related code, sorry for it... as I don't want anything to miss...

                          <s:decorate id="experimentNameDecoration" template="layout/edit.xhtml">
                               <ui:define name="label">Experiment Name</ui:define>
                               <h:selectOneMenu value="#{quantExperimentHome.instance.experimentTableIndex}"
                               label="#{quantExperimentHome.instance.experimentName}">
                                <s:selectItems value="#{experimentTableIndexList.resultList}"
                                    var="experimentTableIndex" label="#{experimentTableIndex.experimentName}"/>                                       
                               </h:selectOneMenu>
                               <s:label value="#{quantExperimentHome.instance.experimentName}"></s:label>                    
                          </s:decorate>

                          ExperimentTableIndexConverter
                          -----------------------------
                          @Name("experimentTableIndexConverter")
                          @org.jboss.seam.annotations.faces.Converter(forClass = ExperimentTableIndex.class)
                          public class ExperimentTableIndexConverter implements Converter, Serializable {

                               private static final long serialVersionUID = 1L;

                               @In
                               private EntityManager entityManager;
                               
                               // default constructor
                               public ExperimentTableIndexConverter(){
                               }
                               
                               @Transactional
                               public Object getAsObject(FacesContext context, UIComponent component, String value) {
                                    if (value != null) {
                                         try {
                                              System.out.println("ExperimentTableIndexConverter.getAsObject(), value = "+value);
                                              //Integer id = Integer.parseInt(value);
                                              Long id = Long.parseLong(value);
                                              
                                              if (id != null) {
                                                   return entityManager.find(ExperimentTableIndex.class, id);
                                              }
                                         } catch (NumberFormatException e) {
                                              throw new ConverterException("Invalid id, must be an Integer value: " + value);
                                         }
                                    }
                                    return null;
                               }

                               public String getAsString(FacesContext context, UIComponent component, Object value) {
                                    if (value instanceof ExperimentTableIndex) {
                                         ExperimentTableIndex experimentTableIndex = (ExperimentTableIndex) value;
                                         System.out.println("ExperimentTableIndexConverter.getAsString(), getExpTableId = "+experimentTableIndex.getExpTableId().toString());
                                         return experimentTableIndex.getExpTableId().toString();
                                    } else {
                                         return null;
                                    }
                               }
                          }


                          ExperimentTableIndex Entity
                          ---------------------------
                          @Entity(name = "ExperimentTableIndex")
                          @Table(name = "EXPERIMENT_TABLE_INDEX")
                          public class ExperimentTableIndex implements Serializable
                          {   
                              protected String experimentName; 
                              protected String experimentTableName;   
                              protected String protXml;  
                              protected Date dateCreated;   
                              protected Long expTableId;

                               @Basic
                              @Column(name = "EXPERIMENT_NAME")
                              public String getExperimentName() {
                                  return experimentName;
                              }

                              public void setExperimentName(String value) {
                                  this.experimentName = value;
                              }
                               
                               @Id
                              @Column(name = "EXP_TABLE_ID")
                              public Long getExpTableId() {
                                  return expTableId;
                              }

                              public void setExpTableId(Long value) {
                                  this.expTableId = value;
                              }
                               ..............
                            }
                          }

                          QuantExperiment entity
                          ----------------------
                          @Entity(name = "QuantExperiment")
                          @Table(name = "QUANT_EXPERIMENT")
                          public class QuantExperiment implements Equals, HashCode, ToString
                          {
                               private ExperimentTableIndex experimentTableIndex;
                               ...........
                               @Transient
                               public ExperimentTableIndex getExperimentTableIndex() {
                                    return experimentTableIndex;
                               }

                               public void setExperimentTableIndex(ExperimentTableIndex experimentTableIndex) {
                                    this.experimentTableIndex = experimentTableIndex;
                               }
                               ......
                               
                          • 11. Re: Pls help Issue with displaying persited value for h:selectOneMenu, s:selectItems
                            Ingo Jobling Master

                            <h:selectOneMenu value="#{quantExperimentHome.instance.experimentTableIndex}" is bound to a method on the Home interface which is @Transient.


                            The scope of Home is @Scope(ScopeType.CONVERSATION).


                            Is a conversation active?  Check the cid request parameter.


                            As I am writing this it occurs to me that it probably sounds like gibberish :-)  Anyways, I hope it helps somewhat ....






                            • 12. Re: Pls help Issue with displaying persited value for h:selectOneMenu, s:selectItems
                              Val Sw Expert
                              Hmmm Ingo, cid is changing, here is the sequence of events... I think you can save me ..:))

                              a) Create screen cid = 4, filled the form fields with experiment name dropdown, other fields... and clicked "Save" on the address bar I can see cid=5.

                              b) Clicked "Done", it takes me to main create screen, cid=5

                              c) I click "Edit" now cid=6....

                              We have not added any scope, should we add any scope ?

                              here is QuantExperimentHome
                              ---------------------------
                              @Name("quantExperimentHome")
                              public class QuantExperimentHome extends EntityHome<QuantExperiment> {

                                   public void setQuantExperimentHjid(Long id) {
                                        setId(id);
                                   }

                                   public Long getQuantExperimentHjid() {
                                        return (Long) getId();
                                   }

                                   @Override
                                   protected QuantExperiment createInstance() {
                                        QuantExperiment quantExperiment = new QuantExperiment();
                                        return quantExperiment;
                                   }

                                   public void wire() {
                                        getInstance();
                                   }

                                   public boolean isWired() {
                                        return true;
                                   }

                                   public QuantExperiment getDefinedInstance() {
                                        return isIdDefined() ? getInstance() : null;
                                   }
                                   
                                   public String update(){
                                        QuantExperiment qe=getDefinedInstance();          
                                        //Adding Experiment table details
                                        qe.setExperimentName(qe.getExperimentTableIndex().getExperimentName());
                                        //End adding Experiment table details          
                                        return super.update();
                                   }
                              }
                              • 13. Re: Pls help Issue with displaying persited value for h:selectOneMenu, s:selectItems
                                Ingo Jobling Master

                                The cid changing is not good, that is why you are losing the selected value.


                                The way we are using for converstations is in mypage.page.xml


                                <begin-conversation join="true" />



                                But there are many other ways,


                                here and
                                here

                                • 14. Re: Pls help Issue with displaying persited value for h:selectOneMenu, s:selectItems
                                  Val Sw Expert
                                  Thanks a lot for the links, I am very much new to it and still in the process of understanding it... and seam looks very good.

                                  Actually, we have 5 entities, each having separate CRUD functionality, we needed to show fields of all entities in single xhtml...

                                  Here, we have main root entity as Study, which have realtionship with QuantExperiment and we added reference of ExperimentTableIndex(getters & setters) in QuantExperiment entity just to read the values from ExperimentTableIndex and persist in QuantExperiment table.

                                  There are xxxx.page.xml generated by seam generate-ui, as we have embedded all in root entity (Study), and StudyEdit.xhtml is the page where we have dropdown and other fields, it already have "<begin-conversation join="true"/>" but it ends on persist, update and remove...

                                  Please suggest what should be right approach and what should I change.....

                                  StudyEdit.page.xml
                                  ------------------
                                  <page xmlns="http://jboss.com/products/seam/pages"
                                        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                                        xsi:schemaLocation="http://jboss.com/products/seam/pages http://jboss.com/products/seam/pages-2.0.xsd"
                                        no-conversation-view-id="/StudyList.xhtml"
                                        login-required="true">  
                                     <begin-conversation join="true"/>  
                                     <action execute="#{studyHome.wire}"/>  
                                     <param name="studyFrom"/>
                                     <param name="studyHjid" value="#{studyHome.studyHjid}"/>

                                     <navigation from-action="#{studyHome.persist}">
                                         <end-conversation/>      
                                         <redirect view-id="/Study.xhtml"/>
                                     </navigation>
                                    
                                     <navigation from-action="#{studyHome.update}">
                                         <end-conversation/>
                                         <redirect view-id="/Study.xhtml"/>
                                     </navigation>
                                    
                                     <navigation from-action="#{studyHome.remove}">
                                         <end-conversation/>
                                         <redirect view-id="/StudyList.xhtml"/>
                                     </navigation>  
                                  </page>


                                  1 2 Previous Next