0

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 !!

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
A.Chakroun
  • 261
  • 3
  • 17
  • without the action being called it is hard to believe it is redirecting to Mur.xhtml. Does http://stackoverflow.com/questions/2118656/commandlink-commandbutton-ajax-backing-bean-action-listener-method-not-invoked apply to any of your scenario? – Mahendran Ayyarsamy Kandiar Dec 29 '15 at 20:32
  • 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 !!..the HTTP Status 500:java.lang.NumberFormatException: null is due to the abscence of request parameter "id_mur" which is strange since it's there in the redirection url !! – A.Chakroun Dec 29 '15 at 20:42
  • Is there any reason setting `prependId` to `false`? Put an `stdout` statement inside the `allerAuSecondMur()` method to see whether it is invoked properly or not. Check whether the only condition in that method is evaluated to a right boolean value or not and the presence of `ecole.getId()`. – Tiny Dec 29 '15 at 20:57
  • The reason for setting prepenId to false is for styling the h:graphicImage using the giving id (in other word avoiding the jsf generated id)...the ecole.getId() is there and i don't see how can i check if the method is invoked by using stdout !! could you clarify please.. – A.Chakroun Dec 29 '15 at 21:10
  • Can you add your filter code in the question, please? – Mahendran Ayyarsamy Kandiar Dec 29 '15 at 21:21
  • `public String allerAuSecondMur() { System.out.println("allerAuSecondMur() invoked." + " : ecole.getId() = " + ecole.getId() + " : (id_mur == 1) = " + (id_mur == 1)); ...}`. – Tiny Dec 29 '15 at 21:22
  • NumberFormatException could be there because paramidMur is null or empty or doesnt represent a number here --> `String paramIdMur = request.getParameter("id_mur"); int paramTdMurInteger = Integer.parseInt(paramIdMur);` Your filter is missing a lot of sanity checks. – Mahendran Ayyarsamy Kandiar Dec 29 '15 at 21:39
  • Yup and i want to figure out why as you can see when the commanLink is clicked i set the request parameter "id_mur" so why it's not there and if we assume that the action is not called again what is the matter specially that when i put off the filter everything work fine !!..Thanks and i hope we will find a solution – A.Chakroun Dec 29 '15 at 21:47
  • I do ***not*** see you passing request parameter in your commandLink. Read http://www.mkyong.com/jsf2/4-ways-to-pass-parameter-from-jsf-page-to-backing-bean/ and use step 2 to pass request parameters. Because you are not passing the request parameter properly in commandLink there is a NumberFormatException in the filter and so it works without the filter. – Mahendran Ayyarsamy Kandiar Dec 29 '15 at 21:56
  • return "/resources/restreint/Mur.xhtml?faces-redirect=true&id_mur=1"; >>>> the portion &id_mur=1 should pass a request parameter which name is "id_mur" and value 1 ?! – A.Chakroun Dec 29 '15 at 22:00
  • @Tiny where should i see the output with stdout ?! i'm using intellijidea 14 and can't see any output in the server console !..thank you for your help – A.Chakroun Dec 29 '15 at 22:11
  • It is displayed on the server terminal (server log) that you see, when you start the server you use. – Tiny Dec 29 '15 at 22:13
  • Ok i will check it out..thank you – A.Chakroun Dec 29 '15 at 22:14
  • OP: 'return "/resources/restreint/Mur.xhtml?faces-redirect=true&id_mur=1" is in action. the filter comes in before that and you yourself told the action is not called. @Tiny I want the OP to understand ***passing parameter*** and not take in the scopes used by Mr.Yong. – Mahendran Ayyarsamy Kandiar Dec 29 '15 at 22:17
  • @Mahendran Ayyarsamy Kandiar Wich url the filter is dealing with before calling the ManagedBean action ?! – A.Chakroun Dec 29 '15 at 22:23
  • 1
    @Mahendran Ayyarsamy Kandiar thank you very very much the problem is solved and yes i needed to pass the parameter in the url... But i steal have a question in fact the url of the view that contain the commandLink i .../resources/restreint/Mur.xhtml?id_mur=1 so is the filter deal with just http://localhost:8080/project_name/resources/restreint/Mur.xhtml ??? – A.Chakroun Dec 29 '15 at 22:32
  • @A.Chakroun when u click on a commandLink or commandButton action a POST request is sent and a filter is called. I will add this as solution. If you want you can click on green tick mark below down triangle – Mahendran Ayyarsamy Kandiar Dec 29 '15 at 22:34

1 Answers1

0

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.