2

I am developing a web application using JSF on Netbeans 7.0. I have created 2 pages: one for entering persons name and other to display that name. I am using a java bean with get and set methods.

I get an error when I submit my form on first page the code.

This is my first page index.xhtml to accept name:

<h:form>
    Enter your Name : <h:inputText value="#{demoBean.name}" required="true"/>
    <br/> <h:commandButton value="Submit" action="welcome.xhtml"/>    
</h:form>

This is the other page welcome.xhtml to display the name:

<h:body>
    Hello #{demoBean.name}
</h:body>

This is the managed bean demoBean.java:

import javax.faces.bean.ManagedBean; 
import javax.faces.bean.SessionScoped; 

@ManagedBean()
@SessionScoped

public class demoBean {
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

This is the error which I got when I submit the fist page:

/index.xhtml @10,86 value="#{demoBean.name}": Target Unreachable, identifier 'demoBean'
resolved to null

prakash_d22
  • 1,153
  • 5
  • 21
  • 34

3 Answers3

4

The JSP framework expects your bean class name to conform to the Java naming conventions, i.e. be "CamelCased" (e.g. DemoBean), in which case it will assume the bean will be referenced in the JSP by the default name formed by converting the first character of the bean name to lower case (demoBean).

As you have deviated from the framework's expectations, you need to indicate the name by which you refer to your bean in the JSPs, either by specifying it in the @ManagedBean annotation or in the optional faces-config.xml file.

James McLeod
  • 2,381
  • 1
  • 17
  • 19
  • i cant find faces-config file.. where to find it.. – prakash_d22 Jan 30 '12 at 12:05
  • i was using ManagedBean which was using default class name then i used ManagedBean(name="mybean") and it worked thanks @James McLeod and why -1.? – prakash_d22 Jan 30 '12 at 12:25
  • 1
    Because this isn't a solution. If you don't specify the name, JSF will default to the classname with 1st character lowercased. OP's concrete problem is that he ignored Java Naming Conventions and the way how tutorial/example code is written. – BalusC Jan 30 '12 at 14:52
  • I have updated my answer (hopefully improved it) to address BalusC's comment, the contents of which I was previously unaware of. – James McLeod Feb 01 '12 at 00:27
2

I had a similar issue. I added an extra annotation. { @Named("demobean") }

Dhawan Gayash
  • 463
  • 1
  • 4
  • 17
0

Another thing to check is to make sure you have all of the required jars in your path(or use maven:) ). I started getting this issue in Glassfish with all of my beans properly set because I changed the directory that stores my Primefaces3.5 jar.

Chow
  • 21
  • 4