user3377708
user3377708

Reputation: 161

Why commandButton actionListener doesn't work?

I have the following code in the login.xhtml:

<html xmlns="http://www.w3.org/1999/xhtml"
 xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
     xmlns:p="http://primefaces.org/ui">

<h:head><title>Login</title></h:head>
<h:body>
    <h:form>
        <p:commandButton id="loginBtn"  value="Login" type="submit" actionListener="#{userMB.login}"/>
    </h:form>
</h:body>
</html>

And I have I have the following bean:

@ManagedBean(name="userMB")
@RequestScoped
public class UsersManagedBean implements Serializable {
    public void login(ActionEvent event) {
        System.out.println("print here...");
    }
}

I don't get the message printed because the login() method hasn't been invoked. Is there anything wrong?

The web.xml file is:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns="http://java.sun.com/xml/ns/javaee"
   xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
   id="WebApp_ID"
   version="2.5">    
<context-param>
    <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
    <param-value>client</param-value>
</context-param>
 <servlet>
     <servlet-name>Faces Servlet</servlet-name>
     <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
     <load-on-startup>1</load-on-startup>
   </servlet>
 <servlet-mapping>
     <servlet-name>Faces Servlet</servlet-name>
     <url-pattern> *.xhtml</url-pattern>
   </servlet-mapping>
</web-app>

Upvotes: 2

Views: 6403

Answers (3)

ChristophS
ChristophS

Reputation: 623

I've copied your complete example login.xhtml page to my testproject and it works fine. The managed bean I've created myself.

Did you try to avoid primefaces? Use the standard JSF implementation and try again:

<h:commandButton id="loginBtn"  value="Login" type="submit" actionListener="#{userMB.login}"/>

Notice the <h: instead of <p: for the commandButton.

By the way: type="sumbit" is the default in PrimeFaces and it should not make any difference to omit or use this attribute with the value submit (PrimeFaces User Guide 5.1, page 108).

Upvotes: 2

Qadir Hussain
Qadir Hussain

Reputation: 1263

Try this after removing type="submit"

<html xmlns="http://www.w3.org/1999/xhtml"
 xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
     xmlns:p="http://primefaces.org/ui">

<h:head><title>Login</title></h:head>
<h:body>
    <h:form>
        <p:commandButton id="loginBtn" value="Login" action="#{userMB.login}"/>
    </h:form>
</h:body>
</html>

And your ManagedBean should be like this.

import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.event.ActionEvent;

@ManagedBean(name = "userMB")
@RequestScoped
public class UsersManagedBean implements Serializable {

    public void login() {
        System.out.println("print here...");
    }
}

Upvotes: 1

wittakarn
wittakarn

Reputation: 3162

Make sure you use javax.faces.event.ActionEvent.

xhtml

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:p="http://primefaces.org/ui">

    <h:head><title>Login</title></h:head>
    <h:body>
        <h:form>
            <p:commandButton id="loginBtn" 
                             value="Login" 
                             type="submit" 
                             actionListener="#{userMB.login}"/>
        </h:form>
    </h:body>
</html>

managedbean

import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.event.ActionEvent;

@ManagedBean(name = "userMB")
@RequestScoped
public class UsersManagedBean implements Serializable {

    public void login(ActionEvent event) {
        System.out.println("print here...");
    }
}

Upvotes: 1

Related Questions