13 Replies Latest reply on Mar 26, 2009 6:18 PM by msantana

    Button action fires on each page refresh

    msantana

      Hi! I have a problem, when i try to fire the action of a button. The action fires each time i refresh the page.
      Anyone who has a tip?
      Thanx in advice!



      The CarritoBean:


      @Name("carrito")
      @AutoCreate
      @Scope(ScopeType.SESSION)
      @Restrict("#{identity.loggedIn}")
      
      public @Stateful class CarritoBean implements Carrito {
      
      private Double importeTotal=0.0;
           
          @DataModel
          private List<Producto> productosAComprar;
           
          public Double getImporteTotal() {
                return importeTotal;
           }
      
           public void setImporteTotal(Double importeTotal) {
                this.importeTotal = importeTotal;
           }
          
          @Remove @Destroy
          public void destroy() {}
          
          public void addProductoAComprar(){
               try {
                    Long productoId = new Long(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("producto"));
                    if(productosAComprar==null){
                         productosAComprar = new LinkedList<Producto>();
                    }
                    InitialContext ic = new InitialContext();
                    ProductosService productosService = (ProductosService)ic.lookup("SGAF-ear/ProductosServiceBean/remote");
                    Producto producto = productosService.getProductoById(productoId);
                   productosAComprar.add(producto);
                   importeTotal+=producto.getImporte();
                } catch (Exception e) {
                     e.printStackTrace();
                }
          }
          
          public void removeProductoAComprar(){
               /*productosAComprar.remove(producto);
               importeTotal=importeTotal - producto.getImporte();*/
          }
           
      }
      


      The code in the xhtml:



      <rich:column styleClass="action">
           <f:facet name="header">Acciones</f:facet>
             <s:button value="Comprar"
                     id="comprar"
                  image="#{request.contextPath}/img/fam/basket_add.png"
                  title="Comprar"
                  action="#{carrito.addProductoAComprar()}"
                  reRender="importeTotal">
                <f:param name="producto" value="#{producto.productoId}"/>
           </s:button>
       </rich:column>


        • 1. Re: Button action fires on each page refresh
          benitojuarez

          hello,


          just send a redirect as response instead of the result page.


          grz
          BJ

          • 2. Re: Button action fires on each page refresh
            msantana

            Sorry but i´m new on this. But You are saying on the return of the method addProductoAComprar?
            Or where and how, i must do that.
            Sorry about my ignorance!, and thanx!!!

            • 3. Re: Button action fires on each page refresh
              marcioendo.marcioendo.gmail.com

                     <s:button value="Comprar"
                             id="comprar"
                          image="#{request.contextPath}/img/fam/basket_add.png"
                          title="Comprar"
                          action="#{carrito.addProductoAComprar()}"
                          reRender="importeTotal">
                        <f:param name="producto" value="#{producto.productoId}"/>
                   </s:button>
               




              Can you try to change your action from


              carrito.addProductoAComprar()



              to


              carrito.addProductoAComprar



              I think s:button is evaluating the method on the render phase.

              • 4. Re: Button action fires on each page refresh
                msantana

                Thank you very much!!! I will try that if i have news i let you know.

                • 5. Re: Button action fires on each page refresh
                  msantana

                  Nop, it doesn´t work. I changed:
                  carrito.addProductoAComprar() to carrito.addProductoAComprar, without success. I press the button, and then i refresh the page, and it fires the event again, adding the same product again.


                  Other ideas?

                  • 6. Re: Button action fires on each page refresh
                    benitojuarez

                    Hi,


                    one kind of redirect:
                    Type in pages.xml



                    <page view-id="/your_view.xhtml">
                            <navigation>
                                <rule if="#{carrito.addProductoAComprar}">
                                    <redirect view-id="/your_resulting_view.xhtml"/>
                                </rule>
                            </navigation>
                        </page>





                    Grz
                    BJ

                    • 7. Re: Button action fires on each page refresh
                      msantana

                      Hi,


                      one kind of redirect: Type in pages.xml


                      <page view-id="/your_view.xhtml">
                              <navigation>
                                  <rule if="#{carrito.addProductoAComprar}">
                                      <redirect view-id="/your_resulting_view.xhtml"/>
                                  </rule>
                              </navigation>
                          </page>



                      Grz BJ


                      ------------------


                      Yes i know. but i dont want to do that, i put a rerender on the button, so a inputText is rerender by ajax.
                      My problem is: I press the button, it fires the action {carrito.addProductoAComprar}. Then it makes all good. But after that if i refresh the page with Ctrl and F5, then the same product i have added, is added again.


                      Thanx for all your support, but anyone who has another tips?

                      • 8. Re: Button action fires on each page refresh
                        msantana

                        Yes i know. but i dont want to do that, i put a rerender on the button, so a inputText is rerender by ajax. My problem is: I press the button, it fires the action {carrito.addProductoAComprar}. Then it makes all good. But after that if i refresh the page with Ctrl and F5, then the same product i have added, is added again.


                        Thanx for all your support, but anyone who has another tips?

                        • 9. Re: Button action fires on each page refresh
                          benitojuarez

                          Hi,


                          if your're using s:button, the whole page will be new rendered. (isn't it?)
                          In this context, what is the sense of ajax rerendering?


                          BJ

                          • 10. Re: Button action fires on each page refresh
                            msantana

                            Ok! you´re right!, and that´s a good point i miss.
                            So correct me if i am wrong, but i have to put a link then, haven´t i?


                            Thanx for all your support man.

                            • 11. Re: Button action fires on each page refresh
                              benitojuarez

                              Matias  Santana wrote on Mar 26, 2009 16:30:


                              So correct me if i am wrong, but i have to put a link then, haven´t i?


                              no, a link has the same behaviour!
                              You got to decide: If you want to do ajax, use a4j:commandButton. But i would prefer s:link or s:button and redirect as mentioned.


                              Its generally a good idea to understand the meaning of the used tags. What is the result in HTML-Page on the browser? What data are transferred between client and server?


                              grz
                              BJ

                              • 12. Re: Button action fires on each page refresh
                                msantana

                                I have two grids where i click a button(which is an image) on one row of the first grid and it passes the row to the athoer grid.
                                So i made, that button on a column of the first row who passes a param which is the productId.
                                So on the server i, look for that product(passes on request) and added to the collection which is the dataModel of the other grid.
                                Then the datatable makes the render of the other grid when action is fire.
                                I don´t want to refresh the whole page each time i add a row.
                                Maybe you have better ideas of that?


                                Thanx!

                                • 13. Re: Button action fires on each page refresh
                                  msantana

                                  Thanxxxxxx!!!!!!
                                  I try with the a4j:commandButton and all gets right!!!
                                  The only thing is not working is that the a4j:commandbutton is not showing the image. i want the button were an image.
                                  But seams like something is wrong but the most important thig wa sloved, thank to you.


                                  You are the best guys!