<p:commandButton> doesn't navigate when actionListener=“#{bean.method}” is declared
Asked Answered
S

1

5

I am trying to create a page which allows a user to logon to the system and then navigates to the homepage. I have managed to get it to do one or the other but cannot work out how to get it to do both. I have crawled through all the sites and cannot find a suitable answer. Please help. My code is as follows: XHTML:

<h:form>
    <p:growl id="growl" showDetail="true" life="3000" />
    <h:panelGrid columns="2" cellpadding="5">
        <h:outputLabel for="username" value="Username: " />
        <p:inputText value="#{login.username}" id="username" required="true"
            label="username" />
        <h:outputLabel for="password" value="Password: " />
        <h:inputSecret value="#{login.password}" id="password" required="true"
            label="password" />
        <p:commandButton ajax="false" id="loginButton" value="Login"
            update="growl" actionListener="#{login.login}" />
    </h:panelGrid>
</h:form>

Java Class:

@ViewScoped
@ManagedBean
@SessionScoped
public class Login implements Serializable
{
private static final long serialVersionUID = 1L;
private String username;
private String password;

public String login(ActionEvent actionEvent)
{
    RequestContext context = RequestContext.getCurrentInstance();
    FacesMessage msg = null;
    boolean loggedIn = false;

    if(username != null && username.equals("admin") && password != null && password.equals("admin"))
        {
            loggedIn = true;
            msg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Welcome", username);
        }
    else
    {
        loggedIn = false;
        msg = new FacesMessage(FacesMessage.SEVERITY_WARN, "Login Error", "Invalid Credentials");
    }
    FacesContext.getCurrentInstance().addMessage(null, msg);
    context.addCallbackParam("loggedIn", loggedIn);

    FacesContext context2 = FacesContext.getCurrentInstance();
    context2.getExternalContext().getFlash().setKeepMessages(true);

    return "ProEJT?faces-redirect=true";
}

As Requested here is my faces-config.xml:

<faces-config xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
version="2.0">
<navigation-rule>
    <from-view-id>/login.xhtml</from-view-id>
    <navigation-case>
        <from-action>#{login.login}</from-action>
        <from-outcome>loggedin</from-outcome>
        <to-view-id>/ProEJT.xhtml</to-view-id>
    </navigation-case>
</navigation-rule>

When I attempted this I altered the return from my login method to "loggedin".

Thanks for any help in advance!!

Serious answered 8/10, 2013 at 14:18 Comment(6)
What is jsf and primefaces version ?Cyclonite
And did you check out this site mkyong.com/jsf2/implicit-navigation-in-jsf-2-0Cyclonite
Hi, I am using JSF 2.1 and PrimeFaces 3.4. I have seen a similar suggestion to that however, it did not work for me. I will edit my question with my attempted faces-config.xmlSerious
I forgot to mention I am also using TomCat 7. ThanksSerious
Some tips: Use action instead of actionListener when using non-ajax command buttons. Don't use implicit navigation (return "ProEJT?faces-redirect=true") and faces-config navigation rules at same time, they are redundant. You have your managed bean declared as @ViewScoped and @SessionScoped, what's supposed to be?Fachini
Changing actionListener to action solved my issue. Thank you for your help!!Serious
L
10

Action listeners are not supposed to do business logic and navigation. They are supposed to listen on action events. Business logic and navigation should be done in a true action method.

Replace

<p:commandButton ... actionListener="#{login.login}" />
public String login(ActionEvent actionEvent) {}

by

<p:commandButton ... action="#{login.login}" />
public String login() {}

and all should be well.

See also:

Lecialecithin answered 30/10, 2013 at 10:21 Comment(1)
Thank you BalusC. You have all the answers!! I had managed to work it out but had no idea why it worked. Now I do. I have another problem regarding primefaces at this [#19677788 if you have time to look at it. Thanks again BalusC!!!!Serious

© 2022 - 2024 — McMap. All rights reserved.