3 Replies Latest reply on May 13, 2009 9:22 PM by Alejandro Alves

    commandlink passing parameters

    Alejandro Alves Newbie

      Hi,
      I am trying to pass parameters from one page to another, first I used

      <s:link>

      but that did not send the form, then I used h:commandbutton but that did not send the pararmeters, and then I used h:commandLink, and it does not pass the parameters either,
      I should get the uri with the parameters
      myurl?param=param

      , but that is not happening. what can I do?


      This is my code:



       <div class="tableControl">
      
              <h:commandLink action="#{asignarPrecio.continuar}"
                  rendered="#{asignarPrecio.previousExists}"
                     value="#{messages.left}#{messages.left} First Page"
                        id="firstPage">
                <f:param name="firstResult" value="0"/>
              </h:commandLink>
      
              <h:commandLink action="#{asignarPrecio.continuar}"
                  rendered="#{asignarPrecio.previousExists}"
                     value="#{messages.left} Previous Page"
                        id="previousPage">
                  <f:param name="firstResult"
                          value="#{asignarPrecio.previousFirstResult}"/>
              </h:commandLink>
      
              <h:commandLink action="#{asignarPrecio.continuar}"
                  rendered="#{asignarPrecio.nextExists}"
                     value="Next Page #{messages.right}"
                        id="nextPage">
                  <f:param name="firstResult"
                          value="#{asignarPrecio.nextFirstResult}"/>
              </h:commandLink>
      
              <h:commandLink action="#{asignarPrecio.continuar}" rendered="#{asignarPrecio.nextExists}"
                     value="Last Page #{messages.right}#{messages.right}"
                        id="lastPage">
                  <f:param name="firstResult"
                          value="#{asignarPrecio.lastFirstResult}"/>
              </h:commandLink>
      
          </div>



      when I get to the action I do not get the params needed to do the pagination.
      What I am trying to do its to paginate a list of items into which I have to insert information.
      Regards

        • 1. Re: commandlink passing parameters
          Bob Corcoran Newbie

          Since f:params are request parameters, you could add the requestParameter annotation to your backing bean for each param you want to pass:


          @RequestParameter
          int firstResult;


          If you wanted to keep the firstResult value around across several requests, you'd have to then assign the value to a field in the (stateful or conversational) bean. Of course, if that is the case, you can just use hidden fields in your form.


          • 2. Re: commandlink passing parameters
            Alejandro Alves Newbie

            Hi,
            Thanks for the reply, the thing is that in the url the parameters do not appear, and the backing bean does not recover the values.
            Whith s:link it worked, but with h:commandLink it doesnt.

            • 3. Re: commandlink passing parameters
              Alejandro Alves Newbie

              Hi,
              I think that I found the problem , the thing is that the parameters that I pass always have the same value, which is two.
              Here is my code:


              Backing object:


              package es.jda.epsa.reserva.session;
              
              import java.text.DateFormat;
              import java.text.SimpleDateFormat;
              import java.util.ArrayList;
              import java.util.Date;
              import java.util.Iterator;
              import java.util.List;
              import java.util.Set;
              
              
              import org.hibernate.annotations.Parameter;
              import org.jboss.seam.ScopeType;
              import org.jboss.seam.annotations.Name;
              import org.jboss.seam.annotations.In;
              import org.jboss.seam.annotations.Logger;
              import org.jboss.seam.annotations.Scope;
              import org.jboss.seam.annotations.security.Restrict;
              import org.jboss.seam.annotations.web.RequestParameter;
              import org.jboss.seam.contexts.Contexts;
              import org.jboss.seam.log.Log;
              import org.jboss.seam.international.StatusMessages;
              
              import es.jda.epsa.reserva.bean.DiasRecurso;
              import es.jda.epsa.reserva.bean.HorariosRecurso;
              import es.jda.epsa.reserva.bean.RecursoPrecio;
              import es.jda.epsa.reserva.entity.Actividad;
              import es.jda.epsa.reserva.entity.Dia;
              import es.jda.epsa.reserva.entity.Horario;
              import es.jda.epsa.reserva.entity.Periodo;
              import es.jda.epsa.reserva.entity.Recurso;
              import es.jda.epsa.reserva.entity.Tipodia;
              import es.jda.epsa.reserva.utils.DateUtils;
              
              @Name("asignarPrecio")
              @Scope(ScopeType.PAGE)
              public class AsignarPrecio
              {
                  @Logger private Log log;
              
                  @In StatusMessages statusMessages;
                  
                  private List<Tipodia> tipoDias;
                  
                  private List<DiasRecurso> dias;
                  
                  private List<DiasRecurso> diasAux;
                  
                  private boolean rendered;
                  
                  private boolean previousExists;
              
                  private boolean nextExists;
                  
                  private int previousFirstResult;
              
                  private int nextFirstResult;
              
                  private int lastFirstResult;
                  
                  private int eltosPag = 2;
                  
                  @RequestParameter
                  private Integer firstResult;
                    
                  
                  public AsignarPrecio() 
                  {
                       rendered = false;
                       if(firstResult==null)
                            firstResult=0;              
                       Integer id = 19;//(Integer)Contexts.getSessionContext().get("idPeriodo");
                       PeriodoService periodoService = new PeriodoService();
                       TipodiaList tipodiaList = new TipodiaList();
                       tipoDias = tipodiaList.getResultList();
                       Periodo periodo = periodoService.find(id);
                       Actividad actividad = periodo.getActividad();
                       diasAux = new ArrayList<DiasRecurso>();
                       Set<Dia> diasSet = periodo.getDias();
                       //List<Dia> diasSet = periodoService.getDias(id);
                       List<Recurso> recursos = new ArrayList<Recurso>(actividad.getRecursos());
                       for(Dia dia : diasSet)
                       {
                            List<Horario> horarios = new ArrayList<Horario>(dia.getHorarios());
                            System.out.println(parseDay(dia.getDia()));
                            for(Horario h : horarios)
                            {     
                                 System.out.println(h.getInicio() + " " + h.getFin());
                            }
                       }
                       for(Dia dia : diasSet)
                       {
                            List<Horario> horarios = new ArrayList<Horario>(dia.getHorarios());
                            List<HorariosRecurso> horariosRecurso = new ArrayList<HorariosRecurso>();
                            for(Horario horario : horarios)
                            {
                                 List<RecursoPrecio> recursosPrecio = new ArrayList<RecursoPrecio>();
                                 for(Recurso rec : recursos)
                                 {
                                      recursosPrecio.add(new RecursoPrecio(rec));
                                 }
                                  HorariosRecurso hRecurso = new HorariosRecurso(horario, recursosPrecio);
                                  horariosRecurso.add(hRecurso);
              
                            }
                            DiasRecurso diasRecurso = new DiasRecurso(parseDay(dia.getDia()).toUpperCase(),horariosRecurso);
                            diasAux.add(diasRecurso);
                       }
                       dias=diasAux;
                       for(DiasRecurso dia : diasAux)
                       {
                            System.out.println(dia.getDia());
                            List<HorariosRecurso> hRec = dia.getHorariosRecurso();
                            for(HorariosRecurso hr : hRec)
                            {
                                 System.out.println(hr.getHorario().getInicio() + " " + hr.getHorario().getFin());
                                 List<RecursoPrecio> recPre = hr.getRecursos();
                                 for(RecursoPrecio r : recPre)
                                 {
                                      System.out.println(r.getRecurso().getNombre());
                                 }
                            }
                       }
                   }
                     
                  public void continuar()
                  {
                        if(firstResult==null)
                        {
                             firstResult=0;
                        }
                        for(DiasRecurso dia : dias)
                        {
                             List<HorariosRecurso> horariosRecursosAux = dia.getHorariosRecurso();
                             Iterator<DiasRecurso> itDias = diasAux.iterator();
                             boolean enc=false;
                             while (itDias.hasNext() && !enc)
                             {
                                  DiasRecurso d = itDias.next();
                                  if(d.getDia()==dia.getDia())
                                  {
                                       enc=true;
                                       d.setHorariosRecurso(horariosRecursosAux);
                                  }
                             }
                        }
                       dias = new ArrayList<DiasRecurso>(diasAux.subList(firstResult, firstResult+eltosPag));
                       rendered = true;
                  }
              
                  @Restrict("#{s:hasRole('admin')}")
                  public String asignarPrecio()
                  {
                       for(DiasRecurso dia : dias)
                       {
                            
                       }
                       return "";
                  }
              
                  public void siguiente()
                  {
                        if(firstResult==null)
                        {
                             firstResult=0;
                        }
                       dias=diasAux.subList(firstResult, firstResult+eltosPag);
                  }
                  // add additional action methods
                 
                  public void setDias(List<DiasRecurso> dias)
                  {
                       this.dias=dias;
                  }
                  
                  public List<DiasRecurso> getDias()
                  {
                       return this.dias;
                  }
              
                   public List<Tipodia> getTipoDias() {
                        return tipoDias;
                   }
              
                   public void setTipoDias(List<Tipodia> tipoDias) {
                        this.tipoDias = tipoDias;
                   }
                  
                   public String parseDay(Date day)
                   {
                        DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
                        String dateStr=dateFormat.format(day);
                        dateStr = DateUtils.getDay(day) + " " + dateStr;
                        return dateStr;
                   }
                   
                  public void setRendered(boolean rendered)
                  {
                       this.rendered=rendered;
                  }
                  
                  public boolean getRendered()
                  {
                       return rendered;
                  }
              
                   public boolean getPreviousExists() {
                        if(firstResult==null)
                        {
                             firstResult=0;
                        }
                        if(firstResult>0)
                             return true;
                        return false;
                   }
              
                   public void setPreviousExists(boolean previousExists) {
                        this.previousExists = previousExists;
                   }
              
                   public boolean isNextExists() {
                        if(firstResult==null)
                        {
                             firstResult=0;
                        }
                        if((firstResult+eltosPag)<diasAux.size())
                             return true;
                        return false;
                   }
              
                   public void setNextExists(boolean nextExists) {
                        this.nextExists = nextExists;
                   }
              
                   public int getPreviousFirstResult() {
                        if(firstResult==null)
                        {
                             firstResult=0;
                        }
                        return firstResult-eltosPag;
                   }
              
                   public void setPreviousFirstResult(int previousFirstResult) {
                        this.previousFirstResult = previousFirstResult;
                   }
              
                   public int getNextFirstResult() {
                        if(firstResult==null)
                        {
                             firstResult=0;
                        }
                        return firstResult+eltosPag;
                   }
              
                   public void setNextFirstResult(int nextFirstResult) {
                        this.nextFirstResult = nextFirstResult;
                   }
              
                   public int getLastFirstResult() {
                        return diasAux.size()-eltosPag;
                   }
              
                   public void setLastFirstResult(int lastFirstResult) {
                        this.lastFirstResult = lastFirstResult;
                   }
              
                   public List<DiasRecurso> getDiasAux() {
                        return diasAux;
                   }
              
                   public void setDiasAux(List<DiasRecurso> diasAux) {
                        this.diasAux = diasAux;
                   }
              
                   public int getEltosPag() {
                        return eltosPag;
                   }
              
                   public void setEltosPag(int eltosPag) {
                        this.eltosPag = eltosPag;
                   }
              
                   public int getFirstResult() {
                        return firstResult;
                   }
              
                   public void setFirstResult(int firstResult) {
                        this.firstResult = firstResult;
                   }
                   
              }
              



              The xthml page:



              <!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
                  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
              <ui:composition xmlns="http://www.w3.org/1999/xhtml"
                  xmlns:s="http://jboss.com/products/seam/taglib"
                  xmlns:ui="http://java.sun.com/jsf/facelets"
                  xmlns:f="http://java.sun.com/jsf/core"
                  xmlns:h="http://java.sun.com/jsf/html"
                  xmlns:rich="http://richfaces.org/rich"
                  xmlns:a="http://richfaces.org/a4j"
                  xmlns:c="http://java.sun.com/jstl/core"
                  template="layout/template.xhtml">
              
              <ui:define name="body">
              
                    <h:form id="asignarPrecioForm1" rendered="#{!asignarPrecio.rendered}">
              
                      <rich:panel>
                          <f:facet name="header">asignarPrecio</f:facet>
              
                         <ui:repeat id="dias"  value="#{asignarPrecio.dias}" var="diasRecurso">
                                   <div class="prop">
                                        <s:label styleClass="name"><i>#{diasRecurso.dia}</i></s:label>
                                   </div>
                                   <s:decorate template="layout/edit.xhtml" >
                                       <ui:define name="label">Tipo Dia</ui:define>
                                       <h:selectOneMenu value="#{diasRecurso.tipodia}"  required="true" >
                                                 <s:selectItems value="#{asignarPrecio.tipoDias}" var="tipo" label="#{tipo.tipo}"/>
                                                 <s:convertEntity/>
                                     </h:selectOneMenu>                         
                                   </s:decorate>
                                   <s:decorate id="grupo" template="layout/edit.xhtml">
                                        <ui:define name="label">Todas las horas tienen el mismo precio</ui:define>
                                         <h:selectBooleanCheckbox value="#{diasRecurso.mismoPrecio}" />
                                </s:decorate>  
                              </ui:repeat>
              
                          <div style="clear:both"/>
              
                      </rich:panel>
              
                      <div class="actionButtons">
                          <h:commandButton id="continuar" value="Continuar" action="#{asignarPrecio.continuar()}" />
                      </div>
              
                  </h:form>
                  
                  <h:form id="asignarPrecioForm2" rendered="#{asignarPrecio.rendered}">
              
                      <rich:panel>
                          <f:facet name="header">asignarPrecio</f:facet>
              
                         <ui:repeat id="dias"  value="#{asignarPrecio.dias}" var="diasRecurso">
                                        <div class="prop">
                                             <s:label styleClass="name"><i>#{diasRecurso.dia} #{diasRecurso.mismoPrecio}</i></s:label>
                                        </div>
                                        <s:div rendered="#{diasRecurso.mismoPrecio}">
                                                  <ui:repeat value="#{horariosRecursos.get(0).recursos}" var="recurso">
                                                            <div class="prop">     
                                                                 <s:label styleClass="name">#{recurso.recurso.nombre}</s:label><br/>
                                                            </div>
                                                            <s:decorate template="layout/edit.xhtml" >
                                                                  <ui:define name="label">Precio</ui:define>
                                                                  <h:inputText id="precio" required="true" value="#{recurso.precio}"/><bt/>
                                                            </s:decorate>
                                                 </ui:repeat>
                                        </s:div>
                                        <s:div rendered="#{!diasRecurso.mismoPrecio}">
                                                   <ui:repeat value="#{diasRecurso.horariosRecurso}" var="horariosRecurso">
                                                         <div class="prop">
                                                              <s:label styleClass="name">De #{horariosRecurso.horario.inicio} a #{horariosRecurso.horario.fin}</s:label><br/>
                                                         </div>
                                                         <ui:repeat value="#{horariosRecurso.recursos}" var="recurso">
                                                                 <div class="prop">     
                                                                      <s:label styleClass="name">#{recurso.recurso.nombre}</s:label><br/>
                                                                 </div>
                                                                 <s:decorate template="layout/edit.xhtml" >
                                                                       <ui:define name="label">Precio</ui:define>
                                                                       <h:inputText id="precio" required="true" value="#{recurso.precio}"/><bt/>
                                                                 </s:decorate>
                                                         </ui:repeat>
                                                  </ui:repeat>
                                       </s:div>
                              </ui:repeat>
              
                          <div style="clear:both"/>
              
                      </rich:panel>
                      
                      
                  <div class="tableControl">
              
                      <h:commandLink action="#{asignarPrecio.continuar}"
                          rendered="#{asignarPrecio.previousExists}"
                             value="#{messages.left}#{messages.left} First Page"
                                id="firstPage">
                        <f:param name="firstResult" value="0"/>
                      </h:commandLink>
              
                      <h:commandLink action="#{asignarPrecio.continuar}"
                          rendered="#{asignarPrecio.previousExists}"
                             value="#{messages.left} Previous Page"
                                id="previousPage">
                          <f:param name="firstResult"
                                  value="#{asignarPrecio.previousFirstResult}"/>
                      </h:commandLink>
              
                      <h:commandLink action="#{asignarPrecio.continuar}"
                          rendered="#{asignarPrecio.nextExists}"
                             value="Next Page #{messages.right}"
                                id="nextPage">
                          <f:param name="firstResult"
                                  value="#{asignarPrecio.nextFirstResult}"/>
                      </h:commandLink>
              
                      <h:commandLink action="#{asignarPrecio.continuar}" rendered="#{asignarPrecio.nextExists}"
                             value="Last Page #{messages.right}#{messages.right}"
                                id="lastPage">
                          <f:param name="firstResult"
                                  value="#{asignarPrecio.lastFirstResult}"/>
                      </h:commandLink>
              
                  </div>
              
                      <div class="actionButtons">
                          <h:commandButton id="asignarPrecio" value="asignarPrecio" action="#{asignarPrecio.asignarPrecio}" />
                      </div>
              
                  </h:form>
              
              </ui:define>
              
              </ui:composition>