I am using Netbeans 8.0.2. I created a very simple (what is intended to be a JSF) web application by using File -> New Project -> Java Web : Web Application.
I am trying to print a @Named bean's instance variable in my index.xhtml page but its not working as expected. I am deploying the application with the green "Run Project" button in Netbeans, which packages, deploys and launches the browser automatically.
Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<welcome-file-list>
<welcome-file>index.xhtml</welcome-file>
</welcome-file-list>
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<context-param>
<param-name>MyContext</param-name>
<param-value>null</param-value>
</context-param>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
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 lang="en"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html">
<h:head>
<title>Facelets Hello Greeting</title>
</h:head>
<h:body>
<!-- I am expecting the beans name to be printed here... -->
The managed bean name is: #{myFirstBean.name}
</h:body>
</html>
Managed Bean
package my.first.jfs;
import java.io.Serializable;
import javax.faces.bean.SessionScoped;
import javax.inject.Named;
@Named
@SessionScoped
public class MyFirstBean implements Serializable {
public String name = "Insert your Name here...";
public MyFirstBean() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Below is a screenshot of my browser after launching the app.
Please let me know if any additional info is required. Thanks!
If you only want to use JSF framwork, you should replace @Named annotation with @ManagedBean. I think it will solve your problem in this case.
For more details, read this topic: Difference between @Named and @ManagedBean annotations in JSF2.0 Tomcat7
and this "sub-one": ManagedProperty in CDI @Named bean returns null