1 Reply Latest reply on Sep 15, 2012 3:24 AM by mcmurdosound

    How to bind multiple UI components to backing bean

    jsface

      Is it possible to use array or list (like Struts) in backing bean to map to the multiple UI components (dynamically generated by loop such as <c:forEach>, <a4j:repeat>, etc.) in JSF page?  If so, how?  Can anybody help?

       

      I can bind single UI component to backing bean UI property successfully.  But I have trouble to bind multiple UI components to backing bean.  The problem is each UI component has to be bound to a single unique UI property in backing bean.

       

      Following is the example I am currently having problem with.  It's simplified just for easier review.  Basically, I try to attach an error message to the hidden input field whenever the save commandLink is clicked but the textarea for the note is still empty.  The error message is just telling user an empty note cannot be saved.

       

      It looks like one UI component property in backing bean can only be mapped to one UI component in JSF page.  I am thinking of using array or list but then the index has to be passed in with getter/setter which I don't know how.

       

       

                                   JSF page

       

                                    <c:forEach items="..." var="..." varStatus="status">

                                      <rich:panel>

                                          <h:panelGrid>

                                              <h:panelGroup><h:panelGrid>

                                                  ...

                                                  <h:column>

                                                      <a4j:commandLink id="lnkSave" value="#{m_.save}" action="#{logNotesBean.saveLogNote(note)}"  />

                                                  </h:column>

                                                 

                                              </h:panelGrid></h:panelGroup>

                                              <rich:message id="msgLogNoteValidationError" for="logNoteValidationError_#{status.index}" ajaxRendered="false"/>

                                              <h:inputHidden id="logNoteValidationError_#{status.index}" binding="#{logNotesBean.logNoteError}"/>

                                             

                                             <h:inputTextarea id="txaLogNote" rows="5" value="#{note.noteText}"/>

                                          </h:panelGrid>

                                      </rich:panel>

                                  </c:forEach>

       

       

       

                             Java bean

       

      @ManagedBean

      public class LogNotesBean {

           private HtmlInputHidden logNoteError;

       

           public HtmlInputHidden getLogNoteError() {

                return this.logNoteError;

           }

       

           public void setLogNoteError(HtmlInputHidden logNoteError) {

                this.logNoteError = logNoteError;

           }

       

           public saveLogNote(note) {

                if (note.getNoteText() == null) {

                     ...

                     FacesMessage facesMessage = ..."Empty log note will not be saved."...;

       

                     FacesContext.getCurrentInstance().addMessage(this.getLogNoteError().getClientId(), facesMessage);

                }

           }

      }

        • 1. Re: How to bind multiple UI components to backing bean
          mcmurdosound

          Why do you need compnentbinding in the first place? Just to access the component from the backing bean to add an error message? You can do the same without any binding. Just look the component up in the FacesContext!

           

           

          should be something like:

          FacesContext.getCurrentInstance().getUIViewRoot().findComponent(String id)

           

          But if you insist on component binding, you can try to use the status variable or something useful from the "var" variable to iterate and access a list / map of UIComponent.

           

          <c:forEach items=... var="item>

          <h:.. binding="#{manager.componentMap[item.id]} // whatever property item is in your case or componentMap[status.index]

           

          bean:

          HashMap<String, UIComponent> componentMap = new HashMap<....

           

          getter / setter

           

           

          these are just some thoughts. On the weekend I've no access to my work ;-)