tags:

views:

31

answers:

1

;Hi,

I am using JSF 2.0 Mojarra ver. 2.0.2-SNAPSHOT (obtain using maven).

I am trying to use Annotation to setup JSF bean, as such:

@ManagedBean
@RequestScoped
public class HelloBean {
    @ManagedProperty(value="test")
    private String name;

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

The annotation does not work, meaning the HelloBean class is not configured as JSF bean. To illustrate this, i have the following 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"&gt;
<html xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"&gt;
<head>
    <title>Hello World</title>

</head>
<body>
<f:view>
    Hello #{helloBean.name}
</f:view>
</body>

</html>

The name "test" should be printed on the page. But when i run on Tomcat 6.20 and JDK 1.6, the name does not show.

If i use faces-config.xml to configure HelloBean, the name appears correctly on the page.

What have I missed?

+1  A: 

If you have any faces-config.xml file, it needs to be declared as JSF 2.0, not as JSF 1.2 or lower.

<faces-config
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
    version="2.0">

By the way, the f:view is unnecessary in Facelets.

BalusC
Thank you Balus. After modifying the faces-config.xml declaration, the annotation works correctly.
Gama Ogi Prayogo
You're welcome.
BalusC