Platform I am using:
I am implementing example
and I am trying to manage to work the login interface. My problem is that the login.xhtml webpage gets correctly the username and password, but the method
public void login(ActionEvent actionEvent)
of bean LoginController.java does not react correctly to the webpage login button. In particular, the webpage does not say anything about the correctness of the username & password. To see what it should happen, see the interactive example at the previous link. Here instead, a short video of what happens on my computer.
To find out what is going on, I putted some console outputs inside the Java bean, and I found out that the getter/setter methods are working, the only method that does not work is
public void login(ActionEvent actionEvent)
Indeed the print
System.out.println("Entering in public void login(ActionEvent actionEvent)");
does not appear in console.
Login.xml
<ui:composition template="/templates/layout.xhtml"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui"
>
<ui:define name="content">
<h:form styleClass="loginPanelStyle">
<p:growl id="msgs" showDetail="true" sticky="false" />
<p:panelGrid columns="2">
<f:facet name="header">
Login Panel
</f:facet>
<h:outputText value="Username : "></h:outputText>
<p:inputText id="username" value="#{loginController.username}" required="true" requiredMessage="Please Enter Username!" message="fc">
<f:validateLength minimum="1" />
</p:inputText>
<h:outputText value="Password : "></h:outputText>
<p:password id="password" value="#{loginController.password}" required="true" requiredMessage="Please Enter password!">
<f:validateLength minimum="1" />
</p:password>
<f:facet name="footer">
<p:commandButton value="Submit" update="msgs" action="#{loginController.login}"
icon="ui-icon-check" style="margin:0" />
</f:facet>
</p:panelGrid>
</h:form>
</ui:define>
</ui:composition>
I have also tried to replace
<p:commandButton value="Submit" update="msgs" action="#{loginController.login}" icon="ui-icon-check" style="margin:0" />
with
<p:commandButton value="Submit" update="msgs" action="#{loginController.login}" icon="ui-icon-check" style="margin:0" />
but the result is the same.
LoginController.java
package controller;
import util.DateUtility;
import java.io.IOException;
import java.io.Serializable;
import java.security.Principal;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.enterprise.context.SessionScoped;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.inject.Inject;
import javax.inject.Named;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
/**
* Login Controller class allows only authenticated users to log in to the web
* application.
*
* @author Emre Simtay <emre@simtay.com>
*/
@Named
@SessionScoped
public class LoginController implements Serializable {
@Inject
private transient Logger logger;
private String username;
private String password;
/**
* Creates a new instance of LoginController
*/
public LoginController() {
System.out.println("LoginController instantiated");
}
// Getters and Setters
/**
* @return username
*/
public String getUsername() {
System.out.println("getUsername: " + username);
return username;
}
/**
*
* @param username
*/
public void setUsername(String username) {
this.username = username;
System.out.println("setUsername sets username: " + this.username);
}
/**
*
* @return password
*/
public String getPassword() {
System.out.println("getPassword is: " + password);
return password;
}
/**
*
* @param password
*/
public void setPassword(String password) {
this.password = password;
System.out.println("setPassword sets password: " + this.password);
}
/**
* Listen for button clicks on the #{loginController.login} action,
* validates the username and password entered by the user and navigates to
* the appropriate page.
*
* @param actionEvent
*/
public void login(ActionEvent actionEvent) {
System.out.println("Entering in public void login(ActionEvent actionEvent)");
FacesContext context = FacesContext.getCurrentInstance();
HttpServletRequest request = (HttpServletRequest) context.getExternalContext().getRequest();
try {
String navigateString = "";
// Checks if username and password are valid if not throws a ServletException
request.login(username, password);
// gets the user principle and navigates to the appropriate page
Principal principal = request.getUserPrincipal();
if (request.isUserInRole("Administrator")) {
navigateString = "/admin/AdminHome.xhtml";
} else if (request.isUserInRole("Manager")) {
navigateString = "/manager/ManagerHome.xhtml";
} else if (request.isUserInRole("User")) {
navigateString = "/user/UserHome.xhtml";
}
try {
logger.log(Level.INFO, "User ({0}) loging in #" + DateUtility.getCurrentDateTime(), request.getUserPrincipal().getName());
context.getExternalContext().redirect(request.getContextPath() + navigateString);
} catch (IOException ex) {
logger.log(Level.SEVERE, "IOException, Login Controller" + "Username : " + principal.getName(), ex);
context.addMessage(null, new FacesMessage("Error!", "Exception occured"));
System.out.println("(DEBUG) CONSOLE OUTPUT: Error!, Exception occured");
}
} catch (ServletException e) {
logger.log(Level.SEVERE, e.toString());
context.addMessage(null, new FacesMessage("Error!", "The username or password you provided does not match our records."));
System.out.println("(DEBUG) CONSOLE OUTPUT: Error!, The username or password you provided does not match our records.");
}
}
/**
* Listen for logout button clicks on the #{loginController.logout} action
* and navigates to login screen.
*/
public void logout() {
HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
logger.log(Level.INFO, "User ({0}) loging out #" + DateUtility.getCurrentDateTime(), request.getUserPrincipal().getName());
if (session != null) {
session.invalidate();
}
FacesContext.getCurrentInstance().getApplication().getNavigationHandler().handleNavigation(FacesContext.getCurrentInstance(), null, "/Login.xhtml?faces-redirect=true");
}
}
persistence.xml
web.xml
Project sources:
You can find the whole "Login interface" project sources HERE
TomEE configuration
TomEE is using directories:
What is inside apache-tomee-plus-1.6.0-JAAS/config/ (1):
.
├── catalina.policy
├── catalina.properties
├── context.xml
├── groups.properties
├── logging.properties
├── login.config
├── server.xml
├── server.xml.original
├── system.properties
├── tomcat-users.xml
├── tomcat-users.xml.original
├── tomee.xml
├── users.properties
└── web.xml
0 directories, 14 files
What is inside (2):
.
├── catalina.policy
├── catalina.properties
├── context.xml
├── server.xml
├── tomcat-users.xml
└── web.xml
I uploaded both conf dirs HERE so you can consult them.
To configure TomEE to operate with JAAS, I asked the following question: So I configured TomEE in Eclipse in this way
TomEE console:
Changing the attribute of the commandButton's tag to actionListener
instead of action
should resolve the problem.