1 Reply Latest reply on Jun 23, 2006 12:54 PM by Fabrizio Boco

    ListBox & unexpected wrong validation

    Fabrizio Boco Newbie

      Hi,

      I am approaching seam just now.

      I am trying to read a selected value from a ListBox.

      Each time I press the go button I get the message ?Validation Error?.

      I don't think to had set any validation check !!!

      What's wrong ?

      Thanks

      Fabrizio

      
      <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
      <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
      <%@ taglib uri="http://jboss.com/products/seam/taglib" prefix="s"%>
      <html>
      <head>
      <title>Test List</title>
      <f:loadBundle basename="messages" var="msgs" />
      </head>
      <body>
      <f:view>
       <h:form>
       <table border="0">
       <tr>
       <td>B3</td>
       <td><h:selectOneMenu value="#{B3Manager.b3}" converter="#{B3Manager.converter}">
       <f:selectItems value="#{B3Manager.b3List}" />
       </h:selectOneMenu></td>
       </tr>
       </table>
       <h:messages />
       <h:commandButton type="submit" value="Go" action="#{B3Manager.go}" />
       </h:form>
      </f:view>
      </body>
      </html>
      
      



      
      @Stateful
      @Name("B3Manager")
      @Scope(ScopeType.EVENT)
      public class B3ManagerBean implements Serializable, B3Manager
      {
       @In(create = true)
       @Out(required = true)
       private B3 b3;
      
       @DataModel
       private List<B3> b3List;
      
       private Map<String, B3> b3Map;
      
       @PersistenceContext(type = PersistenceContextType.EXTENDED)
       private EntityManager em;
      
       @Logger
       private Log log;
      
      
      
       @Create()
       public void findAll()
       {
       b3List = em.createQuery("from B3 o order by o.id desc").getResultList();
      
       Map<String, B3> results = new TreeMap<String, B3>();
      
       for (B3 localB3 : b3List)
       {
       results.put(localB3.getLabel(), localB3);
       }
      
       b3Map = results;
      
       b3 = new B3(new Long(0), "");
       }
      
       public String go()
       {
       log.info("Selected #{b3.id}, #{b3.label}");
      
       return "/B3.jsp";
       }
      
       @Remove
       @Destroy
       public void destroy()
       {
       ;
       }
      
      
      
       public Map<String, B3> getB3List()
       {
       System.out.println("getB2List");
      
       System.out.println("Size b3List " + b3List.size());
       System.out.println("Size b3Map " + b3Map.size());
       System.out.println("b3Map " + b3Map);
      
       return b3Map;
       }
      
      
      
       public Converter getConverter()
       {
       return new B3Converter(b3List);
       }
      
      
      
       static public class B3Converter implements Converter, Serializable
       {
       List<B3> b3List;
      
       public B3Converter(List<B3> b3List)
       {
       this.b3List = b3List;
       }
      
       public String getAsString(FacesContext facesContext, UIComponent component, Object obj)
       {
       if (obj == null)
       return null;
      
       B3 b3 = (B3) obj;
      
       return b3.getId().toString();
       }
      
      
      
       public Object getAsObject(FacesContext facesContext, UIComponent component, String str) throws ConverterException
       {
       if (str == null || str.length() == 0)
       {
       return null;
       }
      
       Long id = Long.valueOf(str);
      
       for (B3 b3 : b3List)
       {
       if (b3.getId().longValue() == id.longValue())
       {
       return b3;
       }
       }
      
       return null;
       }
       }
      
      
      
       public B3 getB3()
       {
       return b3;
       }
      
      
      
       public void setB3(B3 b3)
       {
       this.b3 = b3;
       }
      
      }
      
      
      


      
      @Entity
      @Name("b3")
      @Scope(SESSION)
      @Table(name = "b3")
      public class B3 implements Serializable
      {
       private static final long serialVersionUID = 1881413500711441951L;
      
       private Long id;
       private String label;
      
       public B3(Long id, String label)
       {
       this.id = id;
      
       this.label = label;
       }
      
       public B3()
       {
       }
      
       @Id
       @GeneratedValue
       public Long getId()
       {
       return id;
       }
      
      
       public void setId(Long id)
       {
       this.id = id;
       }
      
      
       @NotNull()
       public String getLabel()
       {
       return label;
       }
      
      
       public void setLabel(String p1)
       {
       this.label = p1;
       }
      
       public String toString()
       {
       return "B3(" + id + " , " + label + ")";
       }
      
       public boolean equals(Object other)
       {
       if (other == null || !(other instanceof Category))
       {
       return false;
       }
      
       B3 otherB3 = (B3) other;
      
       return (getId().longValue() == otherB3.getId().longValue());
       }
      
      
      
       public int hashcode()
       {
       return 37 * getId().intValue() + 97;
       }
      }
      


        • 1. Re: ListBox & unexpected wrong validation
          Fabrizio Boco Newbie

          It was my BIG fault.

          The equals was wrong, I checked for an instance of another bean (Category).

          
           public boolean equals(Object other)
           {
           if (other == null || !(other instanceof B3))
           {
           return false;
           }
          
           B3 otherB3 = (B3) other;
          
           return (getId().longValue() == otherB3.getId().longValue());
           }
          
          


          This mistake made me crazy .....

          Be carefull ...

          Ragards

          Fabrizio