-1

I have written the following, very simple JSF page:

<?xml version='1.0' encoding='UTF-8' ?>
<!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://xmlns.jcp.org/jsf/html"
      xmlns:f="http://xmlns.jcp.org/jsf/core">
    <f:metadata>
        <f:viewParam name="name" value="#{nameController.name}" />
    </f:metadata>

    <h:head>
        <title>Hello, <h:outputText value="#{nameController.name}" /></title>
    </h:head>
    <h:body>
        Hello, <h:outputText value="#{nameController.name}" />
    </h:body>
</html>

The name property of nameController is just an instance variable with a getter and a setter.

However, when I go to the page http://localhost:8080/NameThing/name.xhtml?name=tbodt (and that is the correct path to the page) I get this result:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"><head id="j_idt3">
        <title>Hello, </title></head><body>
        Hello, </body>
</html>

What am I doing wrong?

user207421
  • 305,947
  • 44
  • 307
  • 483
tbodt
  • 16,609
  • 6
  • 58
  • 83
  • 2
    Place a break point in the getter, and check whether name is null? If so, the problem is not in the code you show us ... – meriton Jan 20 '14 at 00:18
  • What is the expected value of nameController.name? – user207421 Jan 25 '14 at 00:37
  • @EJP As you can tell by looking at the code, it is what is passed as the GET parameter "name". – tbodt Jan 25 '14 at 01:48
  • 4
    Why did you rollback my edit to a snarky version? The bounty is nice enough to post an answer anyway irrespective of your initial attitude, but the rollback is stopping me from posting it. – BalusC Jan 25 '14 at 10:23
  • 1
    #{nameController.name} return an empty string (or null). Are you sure the setter is called? – elbuild Jan 25 '14 at 18:21
  • 1
    Fixed your title again. You won't get anywhere by just restating your assumptions in your title, and a more precise title will lead to more precise answers. @BalusC please note. – user207421 Jan 26 '14 at 00:51
  • I may have figured it out. I will try something soon. – tbodt Jan 31 '14 at 20:06

6 Answers6

0

You are trying to pass "name" by url

   http://localhost:8080/NameThing/name.xhtml?name=tbodt.

This dont set your name property.

If you need to set your "name" property by url you have a lot of WA you can do.

1) Use javascript to set your property like BlusC describe in him blog. Pass variable from client side to server side

2) using <h:outputText value="#{param['name']}" />

Like this post: https://stackoverflow.com/a/550718/3056912

Community
  • 1
  • 1
xild
  • 187
  • 8
0

Figured it out. It turns out to be a bug in JSF 2.2. <f:viewParam> doesn't work with the new namespace. As a workaround, I changed the namespaces to this:

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

Then I use <oldf:metadata> and <oldf:viewParam>.

tbodt
  • 16,609
  • 6
  • 58
  • 83
-1

How have you declared your NameController class?

When you write

<h:outputText value="#{nameController.name}" />

it's supposed to bind to the property name of a bean NameController present in the scope of the page.

So NameController should be a managed bean.

@ManagedBean
@SessionScoped // or RequestScoped, ApplicationScoped, etc.. (depending 
               // on how you want your bean to bean managed
public class NameController{
 // contructors
 // properties
 // getter and setter    
}
  • My bean is exactly like that, except with `@Named` instead of `@ManagedBean`, and `@SessionScope` also is from CDI. – tbodt Jan 30 '14 at 18:13
  • So the problem might be that you don't put any data in the NameController instance. Try to set some dumb values in the constructor and you should be able to see sth. – user3173787 Jan 31 '14 at 10:52
-1

If you are using JBoss Weld for CDI , make sure that you added the @Model annotation or better yet a specific @Named annotation.

To narrow down the issue and distinguish between a page parameter problem and a naming problem, add a fixed string to your bean (with getters) and write it to the page.

If the string shows up its a page parameter issues, if it does not show up, its a bean naming/binding issue

ShayM
  • 106
  • 5
-1

check your Managed bean whether the class variable 'name' start with uppercase or lowercase. If it is uppercase make it lowercase and create your getter and setters again and try it again !!!

Mehdi
  • 3,795
  • 3
  • 36
  • 65
-2

You are using JSF 2.2 xml namespaces and obviously don't have an environment (probably an application server) which supports JSF 2.2.

If you want to use "the old" JSF 2 you have to use these namespace declarations:

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

If you want to use JSF 2.2 you have to update your application server to support JSF 2.2.

unwichtich
  • 13,712
  • 4
  • 53
  • 66
  • 2
    If he didn't have JSF 2.2, he would have seen unparsed JSF tags in the generated HTML output. However, he got `` and `` instead of `` and `` and the `` is correctly removed, it's just `#{nameController.name}` which didn't return anything. – BalusC Jan 20 '14 at 08:18
  • I am using GlassFish 4. There is no way this could be. – tbodt Jan 20 '14 at 23:33
  • Changing the namespace to what @unwichtich suggested solves the problem! Just tried it with no other changes! So the setup of tbodt must be wrong! – Martin Höller Jan 31 '14 at 14:11
  • @MartinHöller It's a JSF bug. – tbodt Mar 10 '14 at 23:44