0

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.

Community
  • 1
  • 1
Christophe Weis
  • 2,518
  • 4
  • 28
  • 32

1 Answers1

1

The f:viewParam is a stateful component, it stores the values in the view state. In the source code of the UIViewParameter we can see this.

@Override
public Object getSubmittedValue() {
    return getStateHelper().get(PropertyKeys.submittedValue);
}

@Override
public void setSubmittedValue(Object submittedValue) {
    getStateHelper().put(PropertyKeys.submittedValue, submittedValue);
}

See the article Stateless vs Stateful JSF view parameters for more details.

For the f:viewAction, I'm not sure if it is really stateful, but in the source code of UIViewAction we can see that many properties are stored in the state helper.

As workaround for the f:viewAction in a stateless view, we can use the preRenderView event.

login.xhtml

<f:metadata>
    <f:event type="preRenderView" listener="#{AuthenticationBean.sayHello()}"/>
</f:metadata>

We can also grab request parameters inside the sayHello method.

AuthenticationBean.java

public void sayHello() {
    System.out.println("Hello");
    FacesContext facesContext = FacesContext.getCurrentInstance();
    ExternalContext externalContext = facesContext.getExternalContext();
    Map<String, String> requestParameterMap =
                                externalContext.getRequestParameterMap();
    if (requestParameterMap.containsKey("test")) {
        String test = requestParameterMap.get("test");
        System.out.println("Test parameter: " + test);
    }
}

And even perform navigation.

AuthenticationBean.java

public void sayHello() {
    System.out.println("Hello");
    FacesContext facesContext = FacesContext.getCurrentInstance();
    facesContext.getApplication().getNavigationHandler()
                        .handleNavigation(facesContext, null, "mypage");
}
Christophe Weis
  • 2,518
  • 4
  • 28
  • 32