p:button 示例没有读取示例中传递的参数

p:button example doesn't read the parameter passed as in the example

p:button 将参数传递到下一页的在线示例有效

https://www.primefaces.org/showcase/ui/button/button.xhtml

但是当我 运行 这个相同的例子在本地给了我输出网页:

You've been redirected to this bookmarkable page with button.ViewParameter you've passed is "" 

传递的参数未被读取

这是我的代码:

pom.xml:

<!DOCTYPE xml>
<project xmlns="https://maven.apache.org/xsd" 
    xmlns:xsi="http://www.w3.org/TR/xmlschema-1"
    xsi:schemaLocation="https://maven.apache.org/xsd 
    https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.mkyong.core</groupId>
    <artifactId>primefaces</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>primefaces Maven Webapp</name>
    <url>http://maven.apache.org</url>

    <repositories>
        <repository>
            <id>prime-repo</id>
            <name>Prime Repo</name>
            <url>http://repository.primefaces.org</url>
        </repository>
    </repositories>

    <dependencies>

        <dependency>
            <groupId>org.primefaces</groupId>
            <artifactId>primefaces</artifactId>
            <version>8.0.RC2</version>
        </dependency>

        <dependency>
            <groupId>com.sun.faces</groupId>
            <artifactId>jsf-api</artifactId>
            <version>2.2.20</version>
        </dependency>

        <dependency>
            <groupId>com.sun.faces</groupId>
            <artifactId>jsf-impl</artifactId>
            <version>2.2.20</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>4.0.1</version>
        </dependency>

        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>javax.servlet.jsp-api</artifactId>
            <version>2.3.3</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.glassfish.web</groupId>
            <artifactId>el-impl</artifactId>
            <version>2.2</version>
        </dependency>

        <dependency>
            <groupId>javax.enterprise</groupId>
            <artifactId>cdi-api</artifactId>
            <version>2.0.SP1</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.10</version>
            <scope>provided</scope>
        </dependency>

    </dependencies>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <encoding>UTF-8</encoding>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.2.3</version>
                <configuration>
                    <filteringDeploymentDescriptors>true</filteringDeploymentDescriptors>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>
<web-app xmlns:xsi="http://www.w3.org/TR/xmlschema-1"
    xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
    xmlns:web="http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
    http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
    id="WebApp_ID" version="4.0">

  <display-name>PrimeFaces Web Application</display-name>

    <!-- Change from "Development" to "Production" when you are ready to deploy -->
    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>

    <welcome-file-list>
        <welcome-file>faces/index.xhtml</welcome-file>
    </welcome-file-list>

    <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>*.jsf</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.faces</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>Faces Servlet</servlet-name>
        <url-pattern>*.xhtml</url-pattern>
    </servlet-mapping>

</web-app>

index.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>
    </h:head>

    <h:body>
        <p:button outcome="productDetail" value="Bookmark" icon="pi pi-star" style="margin-right:20px;">
            <f:param name="productId" value="10" />
        </p:button>

        <p:button outcome="productDetail" value="With Icon" icon="pi pi-star" style="margin-right:20px;">
            <f:param name="productId" value="20" />
        </p:button>

        <p:button outcome="productDetail" icon="pi pi-star" style="margin-right:20px;" title="Icon Only">
            <f:param name="productId" value="30" />
        </p:button>

        <p:button outcome="productDetail" value="Bookmark" icon="pi pi-star" disabled="true" style="margin-right:20px;">
            <f:param name="productId" value="40" />
        </p:button>
    </h:body>
</html>

ProductDetail.xhtml:

<p>You've been redirected to this bookmarkable page with button.
        ViewParameter you've passed is <strong>"#{editor.productId}"</strong>
</p>

EditorBean.java:

package com.jsf.editor;

import javax.enterprise.context.RequestScoped;
import javax.inject.Named;

import lombok.Data;

@Data
@Named("editor")
@RequestScoped
public class EditorBean {

    private String productId;

}

目录:

按钮有效,但未读取传递的参数 "productId"。
link 上的示例没有 POM 或 web.xml 的示例,我不得不自己创建它们,没有 primefaces 经验。

您的 productDetail.xhtml 缺少一个重要部分。如果您查看 github 中 productDetail.xhtml 完整 来源,您会发现您的页面中缺少一段重要的代码:

<f:metadata>
    <f:viewParam name="productId" value="#{productDetailView.productId}" />
</f:metadata> 

这就是在 url 中获取参数并将其放入对象中的方法。

另见

  • What can <f:metadata>, <f:viewParam> and <f:viewAction> be used for?