3

The online example of a p:button passing a parameter to next page works

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

But when I run this same example locally gives at me the output web page:

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

The passed parameter is not read.

This is my code:

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;

}

Directories:

enter image description here

The buttons works but the parameter "productId" passed isn't read.
The example on link doesn't have an example of POM or web.xml, I had to create them myself with no primefaces experience.

Kukeltje
  • 12,223
  • 4
  • 24
  • 47
Mario Palumbo
  • 693
  • 8
  • 32
  • You mean your versions results in this error? The showcase works – Kukeltje Jan 23 '20 at 14:42
  • The parameter isn't read like in example – Mario Palumbo Jan 23 '20 at 14:43
  • Ok, but please try without lombok. Not saying it makes it work, but it is less dependencies (and only two a small number of lines of code that will be added) – Kukeltje Jan 23 '20 at 14:52
  • I now have retryed without lombok, the result is the same. – Mario Palumbo Jan 23 '20 at 14:57
  • 1
    Btw, most of your pom is not needed when running a java-ee server (instead of a servlet engine) and your web.xml can be cleaner to with new jsf versions (like 2.2 and up) – Kukeltje Jan 23 '20 at 15:07
  • Where is your CDI implementation? What are you running? Tomcat? Wildfly? either way, your pom is 'wrong' and either contains too little or too much and also wrong things – Kukeltje Jan 28 '20 at 12:20
  • Sorry for the gaps of information. I use Tomcat 9.0.30 – Mario Palumbo Jan 28 '20 at 12:35
  • My only row for CDI is in the POM: – Mario Palumbo Jan 28 '20 at 12:35
  • javax.enterprise cdi-api 2.0.SP1 provided – Mario Palumbo Jan 28 '20 at 12:36
  • What means SP1? – Mario Palumbo Jan 28 '20 at 12:39
  • I don't know how to change the pom. I am completely ignorant of this. I don't know what to put on and what isn't necessary. – Mario Palumbo Jan 28 '20 at 12:43
  • Uhhhh... May I strongly (very strongly) suggest to take a step back and to start learning the basics of each technology you are using. And each question has an existing Q/A in stackoverflow (I'm 200% sure). Start by "What does SP mean in a maven dependency" then "How to use maven" then "How to install CDI in tomcat or use it in a web project"... But really, really better would be to start using a java-ee server like TomEE or Wildfly or... Cheers. – Kukeltje Jan 28 '20 at 13:33
  • The original question is NOT a duplicate of what it is marked as. The initial problem is solved by my answer (see the upvotes before OP chameleonized the question, see the number 9 edit!!! ) – Kukeltje Jan 28 '20 at 14:39
  • OP Also changed images, title etc. I rolled the question back to the #9 edit, OP should accept my answer and create a new question for his, well, new question – Kukeltje Jan 28 '20 at 14:41
  • I have configured POM correctly, i want show at all the only three (four with lombok) dependencies required. – Mario Palumbo Jan 28 '20 at 14:50

1 Answers1

4

Your productDetail.xhtml is missing an important part. If you check the full source of productDetail.xhtml in github, you'll see an important piece of code that is missing in your page:

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

This is what picks up the param in the url and puts it in the object.

See also

Kukeltje
  • 12,223
  • 4
  • 24
  • 47
  • Why didn't the primefaces example report all the code? (which I now feel) – Mario Palumbo Jan 23 '20 at 15:02
  • I not have "/WEB-INF/template.xhtml" – Mario Palumbo Jan 23 '20 at 15:02
  • No idea... The showcase is sort of 'dynamic' (which is really cool since the actual source of what is executed is what you see) But it might be that the metadata needs to be in a specific place in the html and therefor is (unfortunately) not visible. And the template is not needed. All can work without. It is just a (great) thing in full applications and showcase websites like this – Kukeltje Jan 23 '20 at 15:04
  • After applied that xhtml, i obtain an error: productDetail.xhtml @13,64 value="#{editor.productId}": Target Unreachable, identifier [editor] resolved to null – Mario Palumbo Jan 23 '20 at 16:50
  • in the web.xml is not present id. – Mario Palumbo Jan 23 '20 at 17:09
  • I don't understand. What is the region where i use "id" instead of "productId"? – Mario Palumbo Jan 23 '20 at 17:10
  • @MarioPalumbo sorry! i removed my previous comment. i read the `id` in the web.xml in the linked answer and not here. it is my fault. if you use p:button the page redirected and all the query params lost. it just alive the redirection if you use the flash parameters feature. this is what kukeltje answer do. to make flash alive you have to enable param redirection at the navigation rule in in faces-config.xml. look at the answer here : https://stackoverflow.com/questions/27149787/passing-redirect-param-in-faces-config-in-jsf-2-2 – The Bitman Jan 23 '20 at 17:10
  • @TheBitman: redirection does not play a role here, nor does flash scope. At least I do not see anything of this in the showcase – Kukeltje Jan 23 '20 at 20:11
  • Sorry, i don't understand that topic, i have need some help, please. I have try and retry but nothing. – Mario Palumbo Jan 28 '20 at 10:49
  • I have edited all question. Review. – Mario Palumbo Jan 28 '20 at 11:30
  • Where is your CDI implementation? What are you running? Tomcat? Wildfly? In both cases your pom is 'wrong' – Kukeltje Jan 28 '20 at 12:20