【问题标题】:f:viewParam is not workingf:viewParam 不工作
【发布时间】:2013-11-27 19:48:52
【问题描述】:

我在 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。为什么&lt;f:viewParam&gt; 不做它的工作?

更新

我安装了 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

【问题讨论】:

  • 你从哪个包导入@RequestScoped?症状表明 JSF bean 范围已默认为 @NoneScoped,因为您从 CDI 的 javax.enterprise.cdi 包而不是 JSF 的 javax.faces.bean 包中导入了它。
  • @BalusC 没有可识别的范围,ManagedBean 不会默认为 RequestScoped 吗?
  • @BalusC Javadoc 说 RequestScoped 是 JSF ManagedBean 的默认值? docs.oracle.com/javaee/6/api/javax/faces/bean/ManagedBean.html
  • 我使用javax.faces.bean.RequestScoped
  • 用 outputText 替换 inputText 会发生什么? (带有或不带视图参数的 Ps,您需要一个表单,并且我永远不会将视图参数和组件绑定到同一个 bean 属性。您最好在这里使用不同的属性。在预渲染视图中初始化您将组件绑定到一次的那个活动,如果你必须)

标签: jsf jsf-2 viewparams


【解决方案1】:

问题解决了

以 *.xhtml 命名的页面。

在 web.xml 中是:

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

对:

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

【讨论】:

    猜你喜欢
    • 2023-04-03
    • 1970-01-01
    • 2011-12-09
    • 2011-08-04
    • 2013-12-18
    • 2012-03-22
    • 2013-10-27
    • 2013-05-20
    • 2014-02-11
    相关资源
    最近更新 更多