2

am new to Java EE and trying to get a simple JSF 2 tutorial example to work.

I am using the dynamic web project in eclipse and publishing to a Glassfish 3 server (run -> run on server). The first index.xhtml page loads correctly, but when I have to access a managed bean, the following error displays:

/index.xhtml @14,48 value="#{helloBean.name}": Target Unreachable, identifier 'helloBean' resolved to null

I've had a look at the various other discussions on this topic, however the solutions never seem to work for me (e.g. adding beans.xml, giving the managed bean a name etc, following naming conventions).

Any help would be appreciated, as I have been stuck with this for a while.

Here is the code I am currently working with:

Index.xhtml :

<?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:f="http://java.sun.com/jsf/core"      
      xmlns:h="http://java.sun.com/jsf/html">

    <h:head>
        <title>JSF 2.0 Hello World</title>
    </h:head>
    <h:body>
        <h3>JSF 2.0 Hello World Example - hello.xhtml</h3>
        <h:form>
           <h:inputText value="#{helloBean.name}"></h:inputText>
           <h:commandButton value="Welcome Me" action="response"></h:commandButton>
        </h:form>
    </h:body>
</html>

response.xhtml :

    <?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://java.sun.com/jsf/html">

        <h:head>
            <title>JSF 2.0 Hello World</title>
        </h:head>
        <h:body bgcolor="white">
            <h3>JSF 2.0 Hello World Example - welcome.xhtml</h3>
            <h4>Welcome #{helloBean.name}</h4>
        </h:body>
    </html>

Managed bean :

 package java.hello1;
    import javax.faces.bean.ManagedBean;
    import javax.faces.bean.SessionScoped;
    import java.io.Serializable;


        @ManagedBean
        @SessionScoped
        public class HelloBean implements Serializable {

            private static final long serialVersionUID = 1L;

            private String name = "Ricardo";

            public String getName() {
                return name;
            }
            public void setName(String name) {
                this.name = name;
            }
        }
Omar
  • 1,352
  • 1
  • 12
  • 30
Vikki
  • 279
  • 1
  • 3
  • 13
  • 1
    The code seems to be correct. Try to clean & build it, then run it again. – Omar Jan 18 '14 at 14:35
  • If using Maven, clean the project first. Also, try adding at least the 'beans' tag with xmlns to your beans.xml - Google it. – GGrec Mar 04 '14 at 23:39

2 Answers2

0

Change the "@ManagedBean" by "@Named"

brunocrt
  • 557
  • 6
  • 10
0

In case anyone stumbled on this old thread, I got the code to work by replacing .name with .getName() // the variable is private while getName() is public

mc88
  • 1
  • 2