f:param commandButton 和 commandLink 的值都返回 null

f:param value is returning null both with commandButton and also commandLink

我是初学者,正在用 jsf 做一个简单的导航示例。使用 ManagedProperty

访问 managedBean 中的 f:param 值时,我总是得到 null

home.xhtml

    <!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://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/facelets">
<head>
<title>JSF Tutorial!</title>
</head>
<body>
    <h3>Using JSF outcome</h3>
    <h:form>
        <h:commandButton action="#{navigation.show}" value="Page1">
            <f:param name="pageId" value="1" />
        </h:commandButton>
        <h:commandLink action="#{navigation.show}" value="Page2">
            <f:param name="pageId" value="2" />
        </h:commandLink>
        <h:commandLink action="#{navigation.show}" value="Home">
            <f:param name="pageId" value="3" />
        </h:commandLink>
    </h:form>

Navigation.java

    package com.jason.jsf;

import java.util.Map;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;

@ManagedBean(name = "navigation", eager = true)
@RequestScoped
public class Navigation {

    @ManagedProperty(value = "#{param.pageId}")
    private String pageId;

    public String show() {

        System.out.println("page id" + value);
        if (pageId == null) {
            return "home";
        }
        if (pageId.equals("1")) {
            return "page1";
        } else if (pageId.equals("2")) {
            return "page2";
        } else {
            return "home";
        }
    }

    public String getPageId() {
        return pageId;
    }

    public void setPageId(String pageId) {
        System.out.println("page id set" + pageId);

        this.pageId = pageId;
    }
}

这是怎么引起的,我该如何解决?我正在使用 jsf2.2 Mojarra 2.0。3.there 是其他样本 page1.xhtml 和 page2.xhtml 只是为了和我一起导航 提前致谢

仔细查看 XML 名称空间前缀和 URI,并与体面的 JSF book/tutorial/resource:

中显示的内容进行比较
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/facelets">

是的,f: 前缀的 XML 命名空间 URI 是错误的。您声明它是 Facelets tags which have usually ui: prefix. This basically causes those tags to not be properly interpreted at all. It's being misinterpreted as an <ui:param> which has an entirely different meaning than the real <f:param>.

之一

修复标签库 URI。它需要是 http://java.sun.com/jsf/core。这是完整的集合:

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:ui="http://java.sun.com/jsf/facelets">

另请参阅:


与具体问题无关,Mojarra 2.0.3 不是 JSF 2.2。它是 JSF 2.0。还有一个相当老的实现,已经超过 5 年了。您可以在 http://javaserverfaces.java.net 获取最新的 Mojarra 2.2(当前为 2.2.11)。之后,您可以将 taglib URI 中的域从 java.sun.com 更改为 xmlns.jcp.org:

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