I have a stateless login page as BalusC suggests in this answer.
However, when I include either f:viewParam
or f:viewAction
, my action method is not invoked.
I already read this answer to debug my code, but I could not identify the problem.
Here is some example code reproducing the issue. When clicking the button, "Logged in" is printed to the console. But when I uncomment either f:viewParam
or f:viewAction
in login.xhtml, the method is not invoked anymore.
login.xhtml
<ui:composition template="./WEB-INF/templates/template.xhtml"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<ui:define name="metadata" >
<f:metadata>
<!-- uncomment one of these to reproduce the issue
<f:viewParam name="test" required="false"/>
<f:viewAction action="#{AuthenticationBean.sayHello()}"/>
-->
</f:metadata>
</ui:define>
<ui:define name="main_content">
<f:view transient="true">
<h:form>
<h:commandButton value="Login"
action="#{AuthenticationBean.login()}" />
</h:form>
</f:view>
</ui:define>
</ui:composition>
template.xhtml
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<f:view locale="en" contentType="text/html" >
<ui:insert name="metadata"/>
<h:head>
</h:head>
<h:body id="body">
<h:messages/>
<ui:insert name="main_content" />
</h:body>
</f:view>
</html>
AuthenticationBean.java
import javax.enterprise.context.RequestScoped;
import javax.inject.Named;
@Named("AuthenticationBean")
@RequestScoped
public class AuthenticationBean {
public String login() {
System.out.println("Logged in");
return "";
}
public void sayHello() {
System.out.println("Hello");
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</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>/test/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>test/login.xhtml</welcome-file>
</welcome-file-list>
<listener>
<listener-class>
org.jboss.weld.environment.servlet.Listener
</listener-class>
</listener>
</web-app>
Could someone explain me what is going wrong? And how to fix it?
I'm using Mojarra 2.2.13 running on Tomcat 8.