7 Replies Latest reply on Dec 24, 2009 6:25 AM by alpha88

    How can i access a JSF inputText value by using Expression Language ???

    alpha88
      See this form input field

      <input type="text" name="myField"/>

      And if i need to access its value by using Expression language, i use

      param.myField

      Ok. But what should i use to retrieve a JSF inputText value as follows by using Expression Language ???

      <h:inputText id="myInputText"/>
        • 1. Re: How can i access a JSF inputText value by using Expression Language ???
          kragoth

          If you are using richfaces or jquery it is easy.


          Richfaces solution: (Book mark this link it is very usefull! http://mkblog.exadel.com/ria/richfaces-ria/richfaces-built-in-client-functions/


          #{rich:element(‘myInputText’)} //This is equivalent to document.getElementById('myInputText')
          



          Alternatively you could use a jQuery to get it. (I wont try to write one for you at the moment cause I can't remember it off the top of my head but it is something like.... jQuery(regex('.*myInputText')).... that's not right, but that's the idea.)


          If all else fails then JSF follows a naming convention. Which is basically (this is not exact) [NamingContainer]:componentId


          So in your example the following javascript would probably work.


          document.getElementById('FormId:myInputText')


          Replace formId with whatever the id of your form is.


          Hope this helps.

          • 2. Re: How can i access a JSF inputText value by using Expression Language ???
            alpha88
            Hi Tim,

            Thank you for your reply. But i do want to access a inputText value by using Expression Language on server side, not on client side. I know i can access it by using something like

            param["namingContainer:componetId"]

            But i would like to know whether i can access it without use of namingContainer, just the componentId

            Again, Thank you
            • 3. Re: How can i access a JSF inputText value by using Expression Language ???
              kragoth

              From what you are saying I think maybe you need to rethink your design.


              If you need to access a component on the server side then maybe there is a better way of doing what you want to do.


              Alternatively, just use the 'binding' attribute to allow access to the component on the Server side. I believe that is the only way to get access to it. I mean if you think about the JSF lifecycle and the UIViewRoot and how it is all built up there are times when that component does not exist.


              I'm about to finish up for Chirstmas in 10mins so... I wont be making another reply till the new year probably. Hopefully you get this sorted out :D

              • 4. Re: How can i access a JSF inputText value by using Expression Language ???
                alpha88
                It is just a way i can avoid @RequestParameter

                Suppose the following

                   public class Service implements Service {
                      
                      @RequestParameter
                      private Integer someId;

                      @RequestParameter
                      private BigDecimal someValue;

                      public void doSomething() {
                          // do something with someId and someValue
                      }

                   }

                Instead i can use

                   public class Service implements Service {
                      
                      public void doSomething(Integer someId, BigDecimal someValue) {
                          // do something with someId and someValue
                      }

                   }

                So in my JSF page
                  
                   <h:inputText id="someId" required="true">
                   <h:inputText id="someValue" required="true">
                   <h:commandButton action="#{service.doSomething(param['namingContainer:someId'], param['namingContainer:someValue'])}"/>

                I hope it can be useful to you

                regards,
                • 5. Re: How can i access a JSF inputText value by using Expression Language ???
                  kragoth

                  Ok, I have to be really fast with this reply so I hope it makes sense to you. (I have to finish up my wife is waiting for me :P)


                  instead of using


                     <h:inputText id="someId" required="true"\>
                     <h:inputText id="someValue" required="true"\>
                  



                  Make your components bound to your Seam bean like this.


                  <h:inputText id="someId" required="true" value="#{SeamBean.someId}"\>
                  <h:inputText id="someValue" required="true" value="#{SeamBean.someValue}"\>
                  



                  You will have a seam bean like this.


                  @Name("SeamBean")
                  @Scope(Page/Conversation whatever scope you want)
                  public class SeamBean {
                      private Integer someId;
                      private BigDecimal someValue;
                  
                      public Integer getSomeId() {
                          return this.someId;
                      }
                  
                      public void setSomeId(Integer id) {
                          this.someId = id;
                      }
                  
                      //... do the other getter and setter here.
                  
                      public String doSomething() {
                          //do something with someId and someValue here as JSF would have set them to the values entered by the user now.
                      }
                  }
                  



                  By doing things the way you are doing them at the moment is completely ignoring some of the major benefits of using a technology like JSF/Seam.


                  I'll be here for 2 mins more if you have any other questions!

                  • 6. Re: How can i access a JSF inputText value by using Expression Language ???
                    alpha88

                    ok. Thank you. Good night!

                    • 7. Re: How can i access a JSF inputText value by using Expression Language ???
                      alpha88
                      Just a second,

                      I know it is a good pattern to use a JSF backing bean. But i think there is some cases where a request paremeter fills better.

                      Suppose the following

                      @Stateless
                      @Name("accountService")
                      public class AccountServiceImpl implements AccountService {

                          @RequestParemeter
                          protected Integer accountId;

                          @RequestParemeter
                          protected double amount;

                          @PersistenceContext
                          private EntityManager manager;

                          public void deposit() {
                              Account account = manager.find(Account.class, accountId);

                              account.deposit(amount);
                          }

                      }

                      I think it could be better as follows

                      @Stateless
                      @Name("accountService")
                      public class AccountServiceImpl implements AccountService {

                          @PersistenceContext
                          private EntityManager manager;

                          public void deposit(Integer accountId, double amount) {
                              Account account = manager.find(Account.class, accountId);

                              account.deposit(amount);
                          }

                      }

                      regards,