0

I have JSF:

<p:graphicImage value="#{chatView.userPhoto}">
    <f:param name="userId" value="#{user.id}"/>
</p:graphicImage>

user.id is valid I have checked.

And bean:

@ManagedBean
@ViewScoped
public class ChatView extends BaseView {

    /*
     * Injecting managed beans in each other
     * http://balusc.blogspot.com/2011/09/communication-in-jsf-20.html#InjectingManagedBeansInEachOther
     */
    @ManagedProperty("#{chatUsers}")
    private ChatUsers users;

    public StreamedContent getUserPhoto() {         
        // there output is {}            
        System.out.println(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap()); 

        return new DefaultStreamedContent();
    }
}

But I have empty map in debug output for requestParameterMap. Any idea why?

UPDATED:

I created a simple project with such pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>grim</groupId>
    <artifactId>grim</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

        <version.jboss.bom>9.0.0.CR1</version.jboss.bom>

        <version.primefaces>5.2</version.primefaces>
        <version.atmosphere>2.3.1</version.atmosphere>
        <version.jboss.logging>3.2.1.Final</version.jboss.logging>
    </properties>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.wildfly.bom</groupId>
                <artifactId>jboss-javaee-7.0-wildfly-with-tools</artifactId>
                <version>${version.jboss.bom}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <!-- First declare the APIs we depend on and need for compilation. All
                    of them are provided by JBoss WildFly -->

        <!-- Import the CDI API, we use provided scope as the API is included in
            JBoss WildFly -->
        <dependency>
            <groupId>javax.enterprise</groupId>
            <artifactId>cdi-api</artifactId>
            <scope>provided</scope>
        </dependency>

        <!-- Import the Common Annotations API (JSR-250), we use provided scope
            as the API is included in JBoss WildFly -->
        <dependency>
            <groupId>org.jboss.spec.javax.annotation</groupId>
            <artifactId>jboss-annotations-api_1.2_spec</artifactId>
            <scope>provided</scope>
        </dependency>

        <!-- Import the JAX-RS API, we use provided scope as the API is included
            in JBoss WildFly -->
        <dependency>
            <groupId>org.jboss.resteasy</groupId>
            <artifactId>jaxrs-api</artifactId>
            <scope>provided</scope>
        </dependency>

        <!--Hibernate-->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <scope>provided</scope>
        </dependency>

        <!-- Import the JPA API, we use provided scope as the API is included in
            JBoss WildFly -->
        <dependency>
            <groupId>org.hibernate.javax.persistence</groupId>
            <artifactId>hibernate-jpa-2.1-api</artifactId>
            <scope>provided</scope>
        </dependency>

        <!-- Import the EJB API, we use provided scope as the API is included in
            JBoss WildFly -->
        <dependency>
            <groupId>org.jboss.spec.javax.ejb</groupId>
            <artifactId>jboss-ejb-api_3.2_spec</artifactId>
            <scope>provided</scope>
        </dependency>

        <!-- JSR-303 (Bean Validation) Implementation -->
        <!-- Provides portable constraints such as @Email -->
        <!-- Hibernate Validator is shipped in JBoss WildFly -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <scope>provided</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-api</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <!-- Import the JSF API, we use provided scope as the API is included in
            JBoss WildFly -->
        <dependency>
            <groupId>org.jboss.spec.javax.faces</groupId>
            <artifactId>jboss-jsf-api_2.2_spec</artifactId>
            <scope>provided</scope>
        </dependency>

        <!-- Now we declare any tools needed -->

        <!-- Annotation processor to generate the JPA 2.0 metamodel classes for
            typesafe criteria queries -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-jpamodelgen</artifactId>
            <scope>provided</scope>
        </dependency>

        <!-- Annotation processor that raising compilation errors whenever constraint
            annotations are incorrectly used. -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator-annotation-processor</artifactId>
            <scope>provided</scope>
        </dependency>

        <!-- Needed for running tests (you may also use TestNG) -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- Optional, but highly recommended -->
        <!-- Arquillian allows you to test enterprise code such as EJBs and Transactional(JTA)
            JPA from JUnit/TestNG -->
        <dependency>
            <groupId>org.jboss.arquillian.junit</groupId>
            <artifactId>arquillian-junit-container</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.jboss.arquillian.protocol</groupId>
            <artifactId>arquillian-protocol-servlet</artifactId>
            <scope>test</scope>
        </dependency>

        <!-- Import the Servlet API, we use provided scope as the API is included in JBoss WildFly. -->
        <dependency>
            <groupId>org.jboss.spec.javax.servlet</groupId>
            <artifactId>jboss-servlet-api_3.1_spec</artifactId>
            <scope>provided</scope>
        </dependency>

        <!--PrimeFaces-->
        <dependency>
            <groupId>org.primefaces</groupId>
            <artifactId>primefaces</artifactId>
            <version>${version.primefaces}</version>
        </dependency>

        <!--Atmosphere-->
        <dependency>
            <groupId>org.atmosphere</groupId>
            <artifactId>atmosphere-runtime</artifactId>
            <version>${version.atmosphere}</version>
            <exclusions>
                <exclusion>
                    <artifactId>slf4j-api</artifactId>
                    <groupId>org.slf4j</groupId>
                </exclusion>
            </exclusions>
        </dependency>

        <!--Jboss Logging-->
        <dependency>
            <groupId>org.jboss.logging</groupId>
            <artifactId>jboss-logging</artifactId>
            <version>${version.jboss.logging}</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

</project>

View:

@ManagedBean
@ViewScoped
public class TestView {

    public StreamedContent getImg() {
    FacesContext context = FacesContext.getCurrentInstance();
    if (context.getCurrentPhaseId() == PhaseId.RENDER_RESPONSE) {
        // So, we're rendering the HTML. Return a stub StreamedContent so that it will generate right URL.
        return new DefaultStreamedContent();
    }
    else {
        // So, browser is requesting the image. Return a real StreamedContent with the image bytes.
        String studentId = context.getExternalContext().getRequestParameterMap().get("shashistId");
        Shashist student = shashistService.find(Long.valueOf(studentId));
        return new DefaultStreamedContent(new ByteArrayInputStream(student.getPhoto()));
    }
    }
}

And xhtml:

<?xml version="1.0" encoding="UTF-8"?>
<!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:h="http://xmlns.jcp.org/jsf/html"
      xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
      xmlns:p="http://primefaces.org/ui"
      xmlns:f="http://xmlns.jcp.org/jsf/core">
<f:view>
    <h:outputLabel value="Hello, world"/>
    <p:graphicImage value="#{testView.img}">
        <f:param value="1" name="s"/>
    </p:graphicImage>
</f:view>
</html>

But anyway I get empty request parameter map in debug output.

Alex Po
  • 1,297
  • 1
  • 16
  • 26
  • 1
    That method will be called twice. In the second call, it's there, right? Or is that method really called only once? – BalusC May 14 '15 at 22:20
  • Yes, it is called twice. But I see this in log [0m[0m21:46:30,131 INFO [stdout] (default task-54) {} [0m[0m21:46:30,135 INFO [stdout] (default task-54) {} And when I add this in bean System.out.println(FacesContext.getCurrentInstance().getCurrentPhaseId()); it returns such output [0m[0m08:05:13,765 INFO [stdout] (default task-7) RENDER_RESPONSE 6 – Alex Po May 15 '15 at 05:07
  • It should be there in the second request. Apparently the `` isn't being parsed. Did you properly declare the `xmlns:f` XML namespace? – BalusC May 15 '15 at 10:32
  • @BalusC, I updated question with my pom.xml and my xhtml. – Alex Po May 15 '15 at 17:03

1 Answers1

0

Thank to this answer https://stackoverflow.com/a/25425111/836701 I found the solution. The problem was in @ViewScope it should be @SessionScope for getting parameters from f:param.

Community
  • 1
  • 1
Alex Po
  • 1,297
  • 1
  • 16
  • 26
  • 1
    This confirms my initial comment on your question which you apparently didn't understood. Making the bean session scoped is the wrong solution. – BalusC Jul 03 '15 at 14:10
  • 1
    I updated getImg method content. Really, it calls twice, but it didn't visit 'else' branch. What do you suggest instead SessionScope bean? – Alex Po Jul 03 '15 at 15:36