8 Replies Latest reply on Aug 28, 2008 4:09 PM by Leandro Hermida

    Newbie help - trouble with EL and using it in Seam action bean

    Leandro Hermida Newbie

      Hello,


      I know this is probably a very stupid newbie question, but I cannot figure out how to use the EL variables declared in my XHTML forms properly in my Seam action bean.


      Here is the simple test form with the EL variable #{myURI}:


      <h:form>
          <h:inputText size="50" maxlength="500" required="true" value="#{myURI}"/>
          <h:commandLink id="load" styleClass="button sessionEventTrigger"
                         action="#{testAction.test}" value="Submit" />
      </h:form>
      



      Then in TestActionBean.java (its a sesion bean so also has interface TestAction.java), in the test method:


      public java.lang.String test()
      {
          try
          {
              log.info("#{myURI}");
              // this line here throws the exception 
              java.lang.URI physicalURI = java.lang.URI.create("#{myURI}");
              return null;
          }
          catch (java.lang.Exception ex)
          {
              FacesMessages.instance().add("Failure: " + ex.getMessage());
              return null;
          }
      }
      



      In the app server console the log.info() print of the EL variable prints exactly the correct URI I type into the form.  Yet creation of the URI from the EL variable using URI.create() throws an exception with a null message (strange).


      Yet if I hard code into the session bean the exact same URI I was typing into the form for #{myURI}:


      public java.lang.String test()
      {
          try
          {
              log.info("#{myURI}");
              // this line here throws the exception 
              java.lang.URI physicalURI = java.lang.URI.create("http://www.seamframework.org/");
              return null;
          }
          catch (java.lang.Exception ex)
          {
              FacesMessages.instance().add("Failure: " + ex.getMessage());
              return null;
          }
      }
      



      then no exception is thrown and everything is fine.  What am I not understanding about EL variables and their use in Java code?


      Thanks for any help,
      Leandro

        • 1. Re: Newbie help - trouble with EL and using it in Seam action bean
          Pawel Wrzeszcz Novice

          Hi Leonardo,


          EL Strings are only parsed in objects provided by Seam (log, faces messages). #{myURI} does not mean anything to the URI.crate() method. How would you like it to be implemented? Seam overriding java.lang classes on the fly to parse EL? ;)


          One solution is to inject myURI and pass it to the URI.create() method.


          Cheers,
          -Pawel

          • 2. Re: Newbie help - trouble with EL and using it in Seam action bean
            Leandro Hermida Newbie
            Hello,

            Thank you for you help. Basically, how do I simply grab the string from a form input text field and have it as a Java variable in the Seam action method after form submission?  I always see in the Seam examples all form fields bound to a backing entity bean (e.g. #{user.firstName}, #{user.lastName} where user is a full-fledged entity bean)...  But what if your form fields don't represent any entity bean?  What if you just need something simple and need a couple of input form field strings passed back?

            Leandro
            • 3. Re: Newbie help - trouble with EL and using it in Seam action bean
              Leandro Hermida Newbie

              Hi again,


              I tried what you said, to inject myURI into the Seam session bean TestActionBean.java:


              @In
              private java.lang.String myURI;
              
              public java.lang.String test()
              {
                  try
                  {
                      log.info(myURI);
                      // this line here throws the exception 
                      java.lang.URI physicalURI = java.lang.URI.create(myURI);
                      return null;
                  }
                  catch (java.lang.Exception ex)
                  {
                      FacesMessages.instance().add("Failure: " + ex.getMessage());
                      return null;
                  }
              }
              



              Then back in the XHTML form update the EL for the input field to testAction.myURI:


              <h:form>
                  <h:inputText size="50" maxlength="500" required="true" value="#{testAction.myURI}"/>
                  <h:commandLink id="load" styleClass="button sessionEventTrigger"
                                 action="#{testAction.test}" value="Submit" />
              </h:form>
              



              I get this error when I go to the web page:


              javax.el.PropertyNotFoundException: /test.xhtml @95,124 value="#{testAction.myURI}": Property 'myURI' not found on type org.javassist.tmp.java.lang.Object_$$_javassist_3
              

              • 4. Re: Newbie help - trouble with EL and using it in Seam action bean
                Leandro Hermida Newbie

                Hello,


                Everything working now.... big leap of understanding (which happens to newbies quite a few times :))


                For those other newbies out there, if you use the @In annotation like:


                @In
                private java.lang.String myURI;
                



                then in the web form you must use the EL #{myURI} and not #{testAction.myURI}


                If you do put the @In annotation on a getter:


                private java.lang.String myURI;
                
                @In
                public java.lang.String getMyURI()
                {
                   return this.myURI;
                }
                



                then you can use #{testAction.myURI}


                :)

                • 5. Re: Newbie help - trouble with EL and using it in Seam action bean
                  Pawel Wrzeszcz Novice

                  You don't need @In annotation. This would mean that you have a Seam component called myURI and you want to access it.


                  Add getter and setter for the myURI field to reference it as #{testAction.myURI} (property of testAction component) and get rid of the exception.


                  -Pawel


                  PS Sorry for misspelling your name in my previous post, Leandro :)

                  • 6. Re: Newbie help - trouble with EL and using it in Seam action bean
                    Francisco Jose Peredo Noguez Master

                    Hi!


                    First, I really really recommend you to read some JSF related books (I feel, by you question, that you need more knowledge in that area).


                    Now to solve your problem, AFAIK you don't need @In in this particular case; the error message is saying that  you are missing Property 'myURI' so you need to add the getter and setter for you myURI member :


                    private java.lang.String myURI;
                    
                    public void setMyURI(String newMyURI){
                      myURI = newMyURI;
                    }
                    
                    public String getMyURI(){
                      return myURI;
                    }
                    
                    
                    public java.lang.String test()
                    {
                            log.info(myURI);
                            // this line here throws the exception 
                            java.lang.URI physicalURI = java.lang.URI.create(myURI);
                            return null;   
                    }
                    



                    BTW, next time, please remember that if you have a problem causing an exception the best way to get others to understand your problem is post the stack trace in the forum (and of course include the relevant code fragment as you did).


                    To get the stacktrace in the console window of your IDE (Eclipse for example) it best if you do not swallow the exception:


                    catch (java.lang.Exception ex)
                        {
                            FacesMessages.instance().add("Failure: " + ex.getMessage());
                            return null;
                        }
                    
                    



                    This catch is actually preventing you from knowing and informing accurately the root cause of your problem, it only prints the name of the exception, but it hides the very useful stack trace information.

                    • 7. Re: Newbie help - trouble with EL and using it in Seam action bean
                      Francisco Jose Peredo Noguez Master

                      There is no need for @In and you are still missing the setter.

                      • 8. Re: Newbie help - trouble with EL and using it in Seam action bean
                        Leandro Hermida Newbie

                        Thanks for clarifying my error :)  with the getter and setter everything works like a charm and not using @In as that is only for Seam components


                        I am reading a JSF book I found on Safari so hopefully this will help!


                        -leandro