2

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>
Qadir Hussain
  • 1,263
  • 1
  • 10
  • 26
user3377708
  • 151
  • 6
  • 17

3 Answers3

2

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).

ChristophS
  • 598
  • 4
  • 23
1

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...");
    }
}
wittakarn
  • 3,124
  • 1
  • 18
  • 31
  • why declare the login method with a parameter of `ActionEvent event`? it should also work with no parameters. – tt_emrah Oct 29 '14 at 01:28
  • @tt_emrah actionListener attribute will accept with a ActionEvent parameter http://stackoverflow.com/questions/3909267/differences-between-action-and-actionlistener. – wittakarn Oct 29 '14 at 01:35
  • @wittakarn but it also works without using an ActionEvent parameter: http://stackoverflow.com/questions/14382466/jsf-actionlistener-tag-calls-method-which-doesnt-take-an-actionevent-paramete – tt_emrah Oct 29 '14 at 09:10
  • @tt_emrah you will see at the question, the owner did not use login with bracket. Thus, a method will accept with a ActionEvent parameter. – wittakarn Oct 29 '14 at 09:27
  • 1
    i missed that, sorry. i should go back and investigate how i managed to use it without the paranthesis. – tt_emrah Oct 29 '14 at 09:56
  • @wittakarn thank you for the tip. I was using https://stackoverflow.com/questions/21880017/javax-faces-interpret-empty-string-submitted-values-as-null-does-not-work-anymor to interpret empty strings as null but it prevented commandButton to call the method. Setting it to false did not work, either. I completely removed it from web.xml and now it works. – Ahmet Jan 18 '19 at 18:28
1

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...");
    }
}
Qadir Hussain
  • 1,263
  • 1
  • 10
  • 26