5 Replies Latest reply on Mar 25, 2009 7:38 PM by norman

    Dynamic expression evaluation

    stephanos

      Hi guys,


      I was wondering if I could get JSF/Seam to dynamically evaluate a EL construct entered via the UI.
      e.g. having a input box and it would execute the command / print the variable string value


      I'm aware of the security issues of that approach, I intended it only for testing purposes (making Selenium tests more 'white box' like). (Before anyone suggests JSFUnit for this, I think with Hot Deployment and Selenium I can do much faster testing/dev-cycles, because I don't need to restart the server each time, or am I wrong?)



      Cheers :-)


      Stephan

        • 1. Re: Dynamic expression evaluation
          norman

          Absolutely.  In fact, it's something I've thought we should add to the debug page for some time.

          • 2. Re: Dynamic expression evaluation
            stephanos

            Do you have an idea how to actually build it?
            I've got no clue how to make Seam/JSF do it...

            • 3. Re: Dynamic expression evaluation
              barakka

              Hi,


              i guess you can use the Expressions.instance().createValueExpression(String) method to create the EL expressions to get values of component properties and the createMethodExpression(String) for calling components methods. I've no idea, however, if these would work over any scoped object or only seam component.


              Otherwise, you'll have to look into the javax.el package to create dynamically el expression (and check how seam gets the current EL context - the Expressions class contains methods to do just that).


              Hope this help,
              Riccardo. 

              • 4. Re: Dynamic expression evaluation
                stephanos

                Thanks for the tip!


                I managed to get a trivial example execute successfully:



                @Name("actionEL")
                public class ActionEL {
                
                    @In(required = false)
                    private String dynamicEL;
                
                    public void invoke () {
                
                        Expressions expressions = Expressions.instance ();
                        Expressions.MethodExpression<Object> el = expressions.createMethodExpression (dynamicEL);
                        el.invoke ();
                    }
                }



                In the UI I got a simply input and command control:


                <h:form>
                    <h:inputText value="#{dynamicEL}"/>
                    <h:commandButton value="Invoke" action="#{actionEL.invoke}" />
                </h:form>



                I tried entering #{identity.login} and magically the login was triggered :-)

                • 5. Re: Dynamic expression evaluation
                  norman

                  That's really all there is to it. I also often use a similar strategy for testing ejbql queries, though I admit that if I actually learned how to use an IDE, I'm sure the hibernate tools could do that out of the box.