5 Replies Latest reply on Sep 16, 2008 2:39 PM by shadowcreeper

    Determine wich component called the ValueBinding method

    gizola

      Hi!

      I have a problem here. I must know in the backing bean, wich component called the getter method for a property. This is because multiple components on the form use the same getter method for it's property, and the return value should be created regarding the calling component.

      Eg.:

      <h:inputText value="#{bean.property}" id="input1"/>
      <h:inputText value="#{bean.property}" id="input2"/>
      <h:inputText value="#{bean.property}" id="input3"/>
      


      In the backing bean:

      public Object getproperty()
      {
       String component_id = SOMEHOW_DETERMINE_WICH_COMPONENT_CALLED_THIS_METHOD;
      
       if (component_id.equals("input1"))
       {
       return "this is for input1";
       }
       else if (component_id.equals("input2"))
       {
       return "this is for input2";
       }
       ... etc.
       // of course I will do here more complex processing, this is only for example.
      }
      


      I need this solution, because I use the same generic backing bean for all forms, and the forms are dinamically created runtime in the bean constructor. So I can not use dedicated properties, but it will be a huge advantage if I could use a general property getter, and must not code some complicated methods, to generate the initial/refreshed value for a component.

      Thanks in advance!

      Regards,

      Gizola

        • 1. Re: Determine wich component called the ValueBinding method
          ilya_shaikovsky

          you should define actionListener on the components and get the action source from event.getComponent in listener.

          • 2. Re: Determine wich component called the ValueBinding method
            gizola


            Hi!

            Thanks for relpying.

            ActionListener is not working for a couple of components, like inputText, outputLabel, etc.

            So actionListener is only good solution for components, wich do some action. For value retrieval (like value for an imputtext or value for a selectonelistbox) it is not good.

            Also the actionlistener is only called, when some action is performed on the component, but the value is accessed on first displaying the component, on rerendering the component, etc.

            Regards,

            Gizola

            • 3. Re: Determine wich component called the ValueBinding method
              nbelaevski

              Gizola,

              Value expressions do not have component context, so looks like it is not possible to determine which component is accessing it now. What about different expressions referring to the same method? The simplest variant is to use map and request data from it using component id, e.g.:

              <h:inputText value="#{bean.propertyMap['input1']}" id="input1"/>
              <h:inputText value="#{bean.propertyMap['input2']}" id="input2"/>
              <h:inputText value="#{bean.propertyMap['input3']}" id="input3"/>



              • 4. Re: Determine wich component called the ValueBinding method
                gizola

                Hi!

                I had also the idea to use a HashMap with a String key (component id), but than I dropped it, because so I can get back only the object stored in the HashMap, but I can not make any processing, before I give the value back to the component.

                But may be I can figure out some other method to use a Map to store the components values by component ID.

                Thanks for the idea.

                One question: will be there maybe implementation for component context in value expressions? Or is there any way to override some class or method to have control how the value expression method is called? (maybe put the component to the sessionmap before calling the method?)

                Regards

                Gizola

                • 5. Re: Determine wich component called the ValueBinding method
                  shadowcreeper

                  You can extent Map to do processing.

                  Ilya pointed me to some code that does just that...

                  The javax.faces.UIComponentBase class has a getAttributes method which returns a javax.faces.component._ComponentAttributesMap which overrides the get() method to do EL translation.

                  You could do something similar.