1 Reply Latest reply on May 4, 2011 12:55 AM by gebuh

    parameters and variables

    Peter Ondrus Newbie

      Hello, I'm new to seam and actually in web development at all, so there are probably many things I do not understand, or I think I understand, but I'm wrong :)


      The thinks I need should be relatively simple:


      1) take a parameter discountId from an URL:
      http://127.0.0.1:8080/seam-booking/home.seam?discountId=33
      and send it to the session bean's method to get the object with corresponding ID.
      So I have:


      @Stateful
      @Name("DiscountController")
      @Scope(SESSION)
      @TransactionAttribute(REQUIRES_NEW)
      public class DiscountControllerAction implements DiscountController {
        
        @PersistenceContext
        private EntityManager em;
        
        @RequestParameter
        String discountId;
      
        public Discount getDiscountById() {
          String sQuery = "Select dsc from Discount dsc WHERE dsc.id = ?1";
          Query query = em.createQuery(sQuery);
          Long dscId = Long.decode(discountId);
          query.setParameter(1, dscId);
          return (Discount) query.getSingleResult();
        }
      


      and in home.xhtml:



      <h:outputText id="outText1" value="#{DiscountController.getDiscountById().formatedText}" escape="false"/>
      





      This was the only way I could make it work but I'd prefer to store that discountId url parametr in some variable "variable" and then use something like:


      <h:outputText id="outText1" value="#{DiscountController.getDiscountById(variable).formatedText}" escape="false"/>



      such "variable" I could also use later, but I do not know how to do that and if it is possible at all.


      Actually, that's related to my second question:


      2) I need to display more properties from the "Discount" what is the return type from the


      #{DiscountController.getDiscountById()}



      So shoud I each time use


      <h:outputText id="outText1" value="#{DiscountController.getDiscountById().formatedText}" escape="false"/>
      <h:outputText id="outText2" value="#{DiscountController.getDiscountById().name}"/>
      <h:outputText id="outText2" value="#{DiscountController.getDiscountById().address}"/>
      


      or is there a way to create variable "discount" and use


      <h:outputText id="outText1" value="#{discount.formatedText}" escape="false"/>
      <h:outputText id="outText2" value="#{discount.name}"/>
      <h:outputText id="outText2" value="#{discount.address}"/>
      



      thanks a lot!


      Peter

        • 1. Re: parameters and variables
          gebuh Newbie

          That ?discountId=33 is a requestParam you can access from your page.xml. If you take a look at it you should see something like:



          <param name="discountId" value="#{DiscountController.discountId}"/>




          this calls the setter for discountId
          To use this in a query I would use an entityQuery or you could generate a query on the fly, but I would hesitate putting it in a getter.
          Maybe you could use an @Observer in conjunction with raise event to update the discount along with @Factory. So something like:





          @Stateful
          @Name("DiscountController")
          @Scope(SESSION)
          @TransactionAttribute(REQUIRES_NEW)
          public class DiscountControllerAction implements DiscountController {
            
            @PersistenceContext
            private EntityManager em;
            
            @RequestParameter
            String discountId;
          
            private Discount discount;
          
            @Observer("setDiscountId")
            @Factory(value="discount")
            public Discount findDiscountById() {
              String sQuery = "Select dsc from Discount dsc WHERE dsc.id = ?1";
              Query query = em.createQuery(sQuery);
              Long dscId = Long.decode(discountId);
              query.setParameter(1, dscId);
              return (Discount) query.getSingleResult();
            }
          
          public Discount getDiscount(){
               return this.discount;
          }
          
          public void setDiscount(Discount discount){
               this.discount = discount;
          }
          public String getDiscountId(){
               return this.discountId;
          }
          
          public String setDiscountId(String discountId){
               this.discountId = discountId;
               Events.instance().raiseEvent("setDiscountId");
          }
          
          
          



          and in xhtml:


          <h:outputText id="outText1" value="#{discount.formatedText}" escape="false"/>
          <h:outputText id="outText2" value="#{discount.name}"/>
          <h:outputText id="outText2" value="#{discount.address}"/>
          
          


          I'm not sure where you want to set the discountId but if you did something like:



          <h:inputText id="in1" value="#{DiscountController.discountId}" />



          maybe with some reRendering tags to change the values of your output.
          I'm unsure how this works with stateful beans, I use this with stateless pojo's, so I could be completely off base.  Output could be different, I know how to set a var for a resultList that would be used in a dataTable, but I'm unsure about using singleResult.
          HTH