1

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

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Jason
  • 129
  • 1
  • 5
  • 19

1 Answers1

3

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">
Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thank you @Balus C such a silly mistake. but i wanted to ask you a thing like cant we change the prefixes like `xmlns:yash="http://java.sun.com/jsf/core" xmlns:gui="http://java.sun.com/jsf/facelets"` such a way ? – Jason May 08 '15 at 12:16
  • Sure you can. You should then only use `` and such. It only makes the code much less obvious and self-documenting (and thus harder to read/maintain/explain/bugfix) to other JSF developers who respect the standards. In other words, it's kind of disrespectful when doing that in public domain. You'd better not do that if you want to earn respect :) – BalusC May 08 '15 at 12:17
  • and with 'modern' EL, you can do ` so loose the param at all – Kukeltje May 08 '15 at 12:20
  • @Kukeltje: server side EL method parameters and client side HTTP request parameters have quite different semantics (a.o. moment of resolving and ab/presence in HTTP payload) and are usually not to be mindlessly interchanged. So this should be done with care and understanding. – BalusC May 08 '15 at 12:21
  • @BalusC: If the intention is to actually use them as request params, also in URL's, I agree, but due to the nature of the question, I doubt this is intended. – Kukeltje May 08 '15 at 12:29
  • @Kukeltje: OP's "just" learning, but agreed, OP's doing it wrong as to navigation. Yashoon, when you're ready playing around in the sandbox and the time allows it, carefully read http://stackoverflow.com/q/15521451 and http://stackoverflow.com/q/4317684 and replace all those command links by normal links. – BalusC May 08 '15 at 12:34