4 Replies Latest reply on Aug 24, 2009 1:18 PM by ARUN PRAKASH GANESAN

    Dynamic value expression in richfaces

    ARUN PRAKASH GANESAN Newbie

      Consider i have two controllers and a base controller

      abstract class BaseController{
      //some functionality
      abstract getName();
      }


      class A extends BaseController{

      public String getName(){
      return "Arun";
      }
      }


      class B extends BaseController{

      public String getName(){
      return "Ganesan";
      }
      }

      I have one and only one JSP with my code logics, i want to be able to use the instance of each of these controllers dynamically based on some condition.

      <managed-bean>
      <managed-bean-name>ABean</managed-bean-name>
      <managed-bean-class>org.A</managed-bean-class>
      <managed-bean-scope>session</managed-bean-scope>
      </managed-bean>

      <managed-bean>
      <managed-bean-name>BBean</managed-bean-name>
      <managed-bean-class>org.B</managed-bean-class>
      <managed-bean-scope>session</managed-bean-scope>
      </managed-bean>


      JSP

      <f:view>
      <h:form>

      <!-- Depending on a condition here i want to set the instance of
      baseController in the bottom to the respective instance Class A or class
      B -->
      <a4j:commandLink value="#{baseController.name}"/>


      </h:form>
      </f:view>


      Does Richfaces provide a mechanism to do this?

        • 1. Re: Dynamic value expression in richfaces
          ARUN PRAKASH GANESAN Newbie

          if the above is not possible how do i create this value expression dynamically through JAVA API, any suggestions?

          #{baseController.name}

          • 2. Re: Dynamic value expression in richfaces
            John Doe Newbie

            Hi!

            Why not use my DynamicEl Component for Seam Expressions? I think you can port it quite easily to other ELs. It's not optimal yet, but it's a start. It'd need to be enhanced to support more than one Expression on a page.

            public class DynamicExpressionLanguage
            {
             private String dynamicEL;
            
             public Expressions.ValueExpression<Object> getValueExpression()
             {
             Expressions expressions = Expressions.instance();
            
             String eval = "#{" + dynamicEL + "}";
            
             if (dynamicEL == null)
             {
             eval = "#{'itsnull'}";
             }
            
             Expressions.ValueExpression<Object> el = expressions.createValueExpression(eval);
             return el;
             }
            
             public void setCurrentValue(Object value)
             {
             Expressions.ValueExpression<Object> el = getValueExpression();
             el.setValue(value);
             }
            
             public Object getCurrentValue()
             {
             return getValueExpression().getValue();
             }
            
             public void invoke(Object... objects)
             {
             Expressions expressions = Expressions.instance();
            
             String eval = "#{" + dynamicEL + "}";
            
             if (dynamicEL == null)
             {
             eval = "#{'itsnull'}";
             }
            
             try
             {
             Expressions.MethodExpression<Object> el = expressions.createMethodExpression(eval);
             el.invoke(objects);
             }
             catch (Exception ex)
             {
             FacesContext.getCurrentInstance().addMessage("messages", new FacesMessage("Could not call desired action: " + eval));
             }
             }
            
             public void setDynamicEL(String dynString)
             {
             this.dynamicEL = dynString;
             }
            
             public String getDynamicEL()
             {
             return this.dynamicEL;
             }
            
            }


            Usage:

            <h:form>
            <h:inputText value="#{comp.dynel}" />
            <h:outputText value="#{comp.dynel.currentValue}" />
            <h:commandButton value="show" />
            </h:form>
            


            Now input an el expression (without #{ and } ) like
            "aComponent.aProperty" and watch the output.

            • 3. Re: Dynamic value expression in richfaces
              John Doe Newbie

              fixt

              <h:form>
              <h:inputText value="#{comp.dynel.dynamicEL}" />
              <h:outputText value="#{comp.dynel.currentValue}" />
              <h:commandButton value="show" />
              </h:form>
              


              • 4. Re: Dynamic value expression in richfaces
                ARUN PRAKASH GANESAN Newbie

                Sounds cool, thanks a ton. Will check it out.