f:viewParam不起作用

时间:2013-11-14 17:01:52

标签: jsf jsf-2 viewparams

我在GlassFish 3.1.2.2上使用JSF 2.2.4。

我有这个支持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;
    }
}

此视图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>

当我打开/test.html?id=123时,ID会显示为0而不是123。为什么<f:viewParam>没有做好自己的工作?

更新

我安装了GlassFish 4.0。

JSF的Maven依赖项:

<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>

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>

但ID显示为0

Maven测试项目: https://www.dropbox.com/s/qbc05vysspvt46l/jsf-spring-mybatis-master.zip

1 个答案:

答案 0 :(得分:0)

问题解决了

名为* .xhtml的页面。

在web.xml中是:

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

右:

<url-pattern>*.xhtml</url-pattern>