Search code examples
jsfparameter-passingcommandlinkhttp-request-parameters

How to pass request parameter via h:commandLink


I have two forms in my xhtml page, the first includes a commandLink which calls a ManagedBean method :

<h:form prependId="false">
    <h:commandLink action="#{gestionSessionBean.allerAuSecondMur}">
        <p><h:graphicImage library="default" name="#{gestionSessionBean.logoSecondMur}" id="LogoEcole" /></p>
        <p>Mur <h:outputText value="#{gestionSessionBean.nomSecondMur}"/></p>
    </h:commandLink>
</h:form>

Here is the method:

public String allerAuSecondMur() {

    if(id_mur == 1) {
        return "/resources/restreint/Mur.xhtml?faces-redirect=true&id_mur=" + ecole.getId();
    } else {
        return "/resources/restreint/Mur.xhtml?faces-redirect=true&id_mur=1";
    }
}

the second has a commandButton :

 <h:commandButton class="btn btn-primary btn-sm col-xs-offset-3 col-xs-1" value="Partager" id="BoutonPartager"  action="#{gestionSessionBean.sauvgarderSujet}" />

for the method:

public void sauvgarderSujet() {

   // business stuff
    FacesMessage message = new FacesMessage("Fine");
    FacesContext.getCurrentInstance().addMessage(null,message);
}

when i click the button it works fine ( i have a global message with an info class and the word Fine show after clicking the button) but when i click the commanLink the action is not called in fact it redirect me to the page: http://localhost:8080/project_name/resources/restreint/Mur.xhtml without the request parameter.

Then i face a HTTP Status 500:java.lang.NumberFormatException: null due to a filter in my application that required a request parameter and where the filter is not there everything work fine !!

Here is the only filter, note that ConnexionUtilisateurBean is a SessionScoped Bean

  public static final String CONNEXION_UTILISATEUR_BEAN =   "connexionUtilisateurBean";
  public static final String PAGE_ACCUEIL = "/resources/accueil.xhtml";
  public static final String MUR = "/resources/restreint/Mur.xhtml?id_mur=";

 @Override
   public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

    HttpServletRequest request = (HttpServletRequest) servletRequest;
    HttpServletResponse response = (HttpServletResponse) servletResponse;

    HttpSession session = request.getSession();
    UtilisateurDao utilisateurDao = new UtilisateurDao();

    ConnexionUtilisateurBean connexionUtilisateurBean = (ConnexionUtilisateurBean) session.getAttribute(CONNEXION_UTILISATEUR_BEAN);

    Utilisateur utilisateur = connexionUtilisateurBean.getUtilisateur();

    String paramIdMur = request.getParameter("id_mur");
    int paramTdMurInteger = Integer.parseInt(paramIdMur);
    int idEcoleUtilisateur = utilisateur.getEcole().getId();

    boolean problemeAuNiveauIdentifiantMur = (paramTdMurInteger != 1) && paramTdMurInteger != idEcoleUtilisateur;

    if (utilisateurDao.evaluerExistenceUtilisateur(utilisateur.getEmail()) == null) {
        response.sendRedirect(request.getContextPath() + PAGE_ACCUEIL);
    } else if (problemeAuNiveauIdentifiantMur) {
        response.sendRedirect(request.getContextPath() + MUR + 1);
    } else {
        filterChain.doFilter(request, response);
    }
}

Thanks in advance

EDIT: FROM OP's comments:

I think the filter block the request as i mentioned when the filter is not there everything work fine but i don't know why !!


Solution

  • OP had issues whenever his filter was enabled. A java.lang.NumberFormatException exception was thrown whenever he did so.

    Closer inspection of the filter revealed a possible NumberFormatException when request param was null or empty or was not numeric.

    FILTER:

    String paramIdMur = request.getParameter("id_mur");
    int paramTdMurInteger = Integer.parseInt(paramIdMur);
    int idEcoleUtilisateur = utilisateur.getEcole().getId();
    

    OP assumed that this param could not be null as he is passing parameters in action

    ACTION:

    public String allerAuSecondMur() {
    
    if(id_mur == 1) {
        return "/resources/restreint/Mur.xhtml?faces-redirect=true&id_mur=" + ecole.getId();
    } else {
        return "/resources/restreint/Mur.xhtml?faces-redirect=true&id_mur=1";
    }
    

    }

    But every action is a POST request and the filter kicks in for his urlPatterns.

    So OP needed to pass the request param as

    BEGIN EDIT:

    <f:param name="id_mur" value="{gestionSessionBean.id_mur}" />
    

    END EDIT in his offending commandLink for the Filter to receive this parameter.