未在瞬态视图中调用操作方法,包括 viewParam 或 viewAction

Action method not called in transient view including viewParam or viewAction

我有一个无状态登录页面,正如 BalusC 在 this answer 中建议的那样。

但是,当我包含 f:viewParamf:viewAction 时,不会调用我的操作方法。

我已经阅读 this answer 来调试我的代码,但我无法确定问题所在。

这是重现该问题的一些示例代码。单击按钮时,“Logged in”会打印到控制台。但是当我在 login.xhtml 中取消注释 f:viewParamf:viewAction 时,该方法不再被调用。

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>

有人可以向我解释发生了什么问题吗?以及如何修复它?

我在 Tomcat 8.[=20= 上使用 Mojarra 2.2.13 运行 ]

f:viewParam 是有状态组件,它在视图状态中存储值。在UIViewParameter的源代码中我们可以看到这一点。

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

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

有关详细信息,请参阅文章 Stateless vs Stateful JSF view parameters

对于f:viewAction,我不确定它是否真的是有状态的,但是在UIViewAction的源代码中我们可以看到很多属性都存储在state helper中。

作为无状态视图中 f:viewAction 的解决方法,我们可以使用 preRenderView 事件。

login.xhtml

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

我们也可以在sayHello方法中获取请求参数。

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);
    }
}

甚至执行导航。

AuthenticationBean.java

public void sayHello() {
    System.out.println("Hello");
    FacesContext facesContext = FacesContext.getCurrentInstance();
    facesContext.getApplication().getNavigationHandler()
                        .handleNavigation(facesContext, null, "mypage");
}