Reputation: 1431
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!
Upvotes: 0
Views: 6966
Reputation: 922
You are mixing CDI and JSF based annotations on your class. If you are making use of Java EE7 and an EE7 compliant container, you may be running into the larger issue of EE7's default "bean-discovery-mode=annotated". By default, CDI beans are managed when annotated with an explicit scope (@RequestScoped
, @SessionScoped
, etc).
However, when mixing CDI/JSF like this your bean is actually @DependentScoped and because the EE7 default discovery mode doesn't pick up the scope since it isn't explicit, your bean isn't being directly managed as expected.
This can be changed by setting the bean-discovery-mode in your beans.xml file or by ensuring you have a CDI scope explicitly defined on your bean. Unless you have a specific need to make use of JSF managed bean scope, you should swap out
import javax.faces.beans.SessionScoped
with
import javax.enterprise.context.SessionScoped
JSF's annotations have been ported over to CDI so in general you should be able to stick with plain CDI.
I recommend as well making your name
field private
since you've provided the getter/setter.
Hope this helps. Additionally, here are a couple of links regarding CDI activation and scoping which may be beneficial.
Upvotes: 5
Reputation: 188
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
Upvotes: 3