1

I am using JSF 2.2.4 on GlassFish 3.1.2.2.

I have this backing bean:

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import java.io.Serializable;

@ManagedBean(name="testMB")
@RequestScoped
public class TestMB implements Serializable {

    public long id;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }
}

And this view test.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">    

 <f:metadata>
    <f:viewParam name="id" value="#{testMB.id}"/>
 </f:metadata>

<h:body>
    <h:inputText value="#{testMB.id}" />
</h:body>
</html>

When I open /test.html?id=123, then the ID shows up as 0 instead of 123. Why didn't <f:viewParam> do its job?

Updated

I installed GlassFish 4.0.

Maven dependencies for JSF:

<dependency>
    <groupId>javax.faces</groupId>
    <artifactId>javax.faces-api</artifactId>
    <version>2.2</version>
</dependency>

faces-config.xml:

<?xml version='1.0' encoding='UTF-8'?>
<faces-config 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-facesconfig_2_2.xsd"
              version="2.2">

    <!-- JSF and Spring are integrated -->
    <application>
        <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
    </application>

</faces-config>

And test.xhtml:

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:f="http://xmlns.jcp.org/jsf/core"
      xmlns:h="http://xmlns.jcp.org/jsf/html">

<f:metadata>
    <f:viewParam name="id" value="#{testMB.id}"/>
</f:metadata>

<h:body>
    <h:inputText value="#{testMB.id}" />
</h:body>
</html>

But the ID shows up as 0.

Maven project for test: https://www.dropbox.com/s/qbc05vysspvt46l/jsf-spring-mybatis-master.zip

z3r9
  • 167
  • 1
  • 4
  • 15
  • What package did you import `@RequestScoped` from? Symptoms suggests that the JSF bean scope has defaulted to `@NoneScoped` because you imported it from CDI's `javax.enterprise.cdi` package instead of form JSF's `javax.faces.bean` package. – BalusC Nov 14 '13 at 18:18
  • @BalusC without a recognised scope, wouldn't ManagedBean default to RequestScoped? – Mike Braun Nov 14 '13 at 19:06
  • @BalusC Javadoc says that RequestScoped is default for JSF ManagedBean? http://docs.oracle.com/javaee/6/api/javax/faces/bean/ManagedBean.html – Mike Braun Nov 14 '13 at 19:18
  • I use `javax.faces.bean.RequestScoped`. – z3r9 Nov 14 '13 at 19:26
  • What happens when you replace inputText with outputText? (Ps with or without view param you would need a form AND I would never bind a view param and component to the same bean property. You'd better use different properties here. Init the one you bind the component to once in a prerender view event if you must) – Mike Braun Nov 14 '13 at 19:26
  • If I use ``, the ID shows up `0`. – z3r9 Nov 14 '13 at 19:42
  • It could be the name space. Mojarra 2.2.x can be picky about the old and new ones. Can you try the code on a plain GlassFish 3.1? (don't update Mojarra, just leave the default 2.1.x in) – Mike Braun Nov 14 '13 at 20:03
  • Or alternatively try "http://xmlns.jcp.org/jsf/core" for JSF 2.2. See: http://jsflive.wordpress.com/2013/05/16/jsf22-namespaces/ – Mike Braun Nov 14 '13 at 20:22
  • I installed GlassFish 4.0 and updated namespace, but it is not working. I added in main post section Updated. – z3r9 Nov 15 '13 at 15:56
  • I added a test project in main post. – z3r9 Nov 17 '13 at 20:11
  • Thank you all for help! Problem solved. – z3r9 Nov 18 '13 at 20:12

1 Answers1

0

Problem solved

Pages named as *.xhtml.

In web.xml was:

<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.html</url-pattern>
</servlet-mapping> 

Right:

<url-pattern>*.xhtml</url-pattern>
z3r9
  • 167
  • 1
  • 4
  • 15