1
votes

I am doing a simple navigation example in jsf as i am a beginner. i am always getting null when accessing the f:param value in the managedBean using ManagedProperty

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

How is this caused and how can I solve it? I am using jsf2.2 Mojarra 2.0.3.there are other sample page1.xhtml and page2.xhtml just for navigation with me Thanks in advance

1

1 Answers

3
votes

Look closer at the XML namespace prefix and the URI and compare with whatever is shown in a decent 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">

Yes, the XML namespace URI for the f: prefix is wrong. You declared it to be the one of 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>.

Fix the taglib URI. It needs to be http://java.sun.com/jsf/core. Here's the complete set:

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

See also:


Unrelated to the concrete problem, Mojarra 2.0.3 is not JSF 2.2. It's JSF 2.0. And a rather old implementation too, over 5 years already. You can get latest Mojarra 2.2 (currently 2.2.11) at http://javaserverfaces.java.net. After that, you can change the domain in taglib URIs from java.sun.com to 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">