7 Replies Latest reply on Mar 6, 2008 4:29 PM by franciscoperedo

    Can I bind form controls to hashtable like a pojo?

      Hi!
      I know it is really easy to bind form elements to a POJO... but I am having an special case where a form is dynamically generated at runtime, and I want to bind all the elements in a page to a Hashtable in my controller...


      Can I bind my JSF controls to a Hashtable in the same way I do it with a Pojo? or should I use a different technique to deal with forms dynamically generated at runtime?


      Thanks.
      Regards,


      LuxSpes

        • 1. Re: Can I bind form controls to hashtable like a pojo?
          stefans

          hi


          yes, it is possible to use a HashMap (or afaik any type of Map) to dynamically map values to your JSF controls. I did it like this:


          In the Bean I defined a HashMap like this (including getter/setter)


          private Map<String, Object> valueMap = new HashMap<String, Object>();
          



          the EL binding then looks like this:


          #{BeanName.valueMap['keyString']}



          you can also take this further and access a specific property of an POJO within the Map... e.g.:


          #{BeanName.valueMap['keyString'].property}



          cheers
          Stefan

          • 2. Re: Can I bind form controls to hashtable like a pojo?

            Or even



            #{BeanName.valueMap.keyString.property}



            should do, as long as the keyString doesn't have any spaces/dots/any other funky chars...

            • 3. Re: Can I bind form controls to hashtable like a pojo?
              franciscoperedo

              Hi! Thanks for answering!


              Your answer is is almost what I am looking for... but...
              I am hitting a big wall here... the keyString in

              #{BeanName.valueMap['keyString']}

              needs to be a variable!


              The thing is i am painting an input text for each dynamic field using a repeater... but now.. to bind that with the HashMap, I need to bind each input with a different key in the hashmap... and I can not find the way to do it!!!


              Regards,

              • 4. Re: Can I bind form controls to hashtable like a pojo?
                keithnaas

                Can you post your xhtml? 

                • 5. Re: Can I bind form controls to hashtable like a pojo?
                  franciscoperedo

                  Hi! thanks!


                  Here it is (I basically read de structure of the form some
                  tables in the database, and create it ):


                  <h:form>
                  <a:repeat value="#{inventario.plantilla.clasificacionCamposList}"
                  var="clasificacion">
                    <rich:panel>
                      <f:facet name="header">#{clasificacion.nombre}</f:facet>
                      <a:repeat value="#{clasificacion.camposList}" var="campo">
                        <s:decorate template="../layout/edit.xhtml">
                        <ui:define name="label">#{campo.nombre}</ui:define>
                        <h:selectOneMenu rendered="#{not empty campo.caracteristicaList}">
                        <s:selectItems value="#{campo.caracteristicaList}" var="caracteristica"
                        label="#{caracteristica.nombre}">
                        </s:selectItems>
                        <s:convertEntity/>
                        </h:selectOneMenu>
                        <h:inputText rendered="#{empty campo.caracteristicaList}"></h:inputText>
                        </s:decorate>
                      </a:repeat>
                      <br/>
                      <br/>
                    </rich:panel>
                    <br/>
                  </a:repeat>
                  </h:form>
                  



                  Any hints?

                  • 6. Re: Can I bind form controls to hashtable like a pojo?

                    Here is a simplified version:


                     
                        <a:repeat value="#{controller.fieldList}" var="field">
                          <h:outputText value="#{field.name}"/>
                          <h:inputText/>
                          </s:decorate>
                        </a:repeat>
                    
                    



                    How do I bind this dynamically generated inputTexts to a HashMap using the field.name? I mean, AFAIK this will not work:




                     
                        <a:repeat value="#{controller.fieldList}" var="field">
                          <h:outputText value="#{field.name}"/>
                          <h:inputText #{controller.valueMap[field]}/>
                          </s:decorate>
                        </a:repeat>
                    
                    



                    Any hints?

                    • 7. Re: Can I bind form controls to hashtable like a pojo?
                      franciscoperedo

                      Hi!
                      My mistake, the code in my controller had an error, the following works:


                       <a:repeat value="#{controller.fieldList}" var="field">
                            <h:outputText value="#{field.name}"/>
                            <h:inputText #{controller.valueMap[field]}/>
                            </s:decorate>
                          </a:repeat>
                      



                      Thanks! Seam is great!


                      Regards