JSF ajax render message and redirect

1.4k views Asked by At

I have a JSF application where users login in a login form inserting their email and password. The ManagedBean has the following 2 methods.

The method checkIdentity1() returns an URL ("" if the validation is incorrect in order to stay in the same page, or /useraccount.xhtml if inserted data is ok in order to go to next page).

The method checkIdentity2() returns a boolean value (false if the validation is wrong in order to show a message, true if it is ok).

loginManagedBean.java

public String checkIdentity1()
{
    String strResponse="";

    //check id

    if(email.equals("[email protected]") && password.equals("1234"))
    {
        strResponse="/useraccount.xhtml?faces-redirect=true";
    } else {

    }
    //
        return strResponse;
}

public boolean checkIdentity2()
{
    boolean bResponse=false;
    //check id

    if(email.equals("[email protected]") && password.equals("1234"))
    {
       //setpassIncorrecta(false); 

    } else {
        bResponse=true;
    }

    //

    return bResponse;
}

What I am trying to do is to mix ajax and JSF to show "Email and/or password incorrect" when I click Login button and validation fails and go to account.xhtml when validation is ok. But when I insert incorrect email and password no message is displayed, and when I insert them correctly the page is no redirected to account.xhtml. What am I doing wrong?

This is my Facelet

 <h:form> 
    <!--Email-->
    <h:inputText id="email" label="email" required="true" size="32" maxlength="32"
         value="#{loginManagedBean.email}"
         requiredMessage="Insert your email">              
    </h:inputText>
    <h:message for="email" />


    <!--Password-->
    <h:inputSecret id="password" label="password" required="true" size="32" maxlength="40"
        value="#{loginManagedBean.password}" 
        requiredMessage="Insert your password">            
    </h:inputSecret>
    <h:message for="password" />


    <!--Button-->
    <h:commandButton value="Login" action="#{loginManagedBean.checkIdentity1()}">
    <f:ajax execute="@form" render=":loginErrorMessage" />
    </h:commandButton>

</h:form>


<h:panelGroup id="loginErrorMessage">
    <h:outputText value="Email and/or password incorrect" rendered="#{!loginManagedBean.checkIdentity2()}" />
</h:panelGroup>
1

There are 1 answers

0
kolossus On

Like I explained in the comment, that's just the way JSF works: if a request fails validation, the action method will not be executed. That's defined the JSF request processing lifecycle (which I won't get into here). For the purpose of this answer, all you need to know is that the validation of request parameters happens before the action methods are considered. That being said, if the validation fails, the request processing is short-circuited there and then. To achieve what you're looking for, you should consider the following:

  1. To conditionally render that component, you can examine the validation status in your page:

    <h:outputText value="Email and/or password incorrect" rendered="#{facesContext.validationFailed}" />
    

    Ideally, you should just use the <h:messages/> component, where you don't need to manage message display yourself

JSF will stay on the same page by default, if the validation fails, so you needn't take any special steps to that effect