0

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.

index.xhtml

Please let me know if any additional info is required. Thanks!

BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
Jason
  • 1,311
  • 4
  • 20
  • 41
  • `javax.faces.bean.SessionScoped`? The managed bean enjoys the default `@Dependent` pseudo scope. It is not a session scoped bean as you are likely to believe unless it is arriving from `javax.enterprise.context.SessionScoped` or `@Named` is replaced by `@ManagedBean`. – Tiny Aug 22 '15 at 02:45
  • This question handles only the problem when such a bean is involved in a form submit, not in a pure presentation: http://stackoverflow.com/questions/30128395/identifying-and-solving-javax-el-propertynotfoundexception-target-unreachable Is this regardless acceptable as dupe? As you can probably see after reading the answer over there, the exact cause cannot be pinpointed based on your information provided so far. Configuration detail about CDI and the server make/version is completely missing in your question. – BalusC Aug 24 '15 at 09:35

2 Answers2

5

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.

Jens Piegsa
  • 6,630
  • 5
  • 47
  • 95
whitlaaa
  • 822
  • 8
  • 14
  • @BalusC Correct, this is only addressing the incorrect scoping caused by mixing CDI/JSF, but more importantly is caused by the implicit bean discovery mode (assuming EE7 is being used). I will update my answer to hopefully clarify this. – whitlaaa Aug 24 '15 at 13:56
  • Sorry for long delay on getting back to this. I've never heard of CDI, thanks for the information I will be sure to read up on it. My project doesn't have any dependencies for javax.enterprise.context so I cannot change the import based on your suggestion. – Jason Aug 29 '15 at 15:44
3

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

Community
  • 1
  • 1
nori
  • 189
  • 5
  • 1
    This will only have as consequence that the bean scope is wrong while submitting the form, not that the bean is not found while merely printing out its properties. – BalusC Aug 24 '15 at 09:33
  • Not sure why someone gave a negative point for this posting, it solved my problem! – Jason Aug 29 '15 at 15:53
  • My answer was correct depending of what you want to use: JSF or JSF/CDI. BalusC calrified the behavior when using CDI and JSF and whitlaaa gave the way to use both. If you want a full exemple of a JSF/CDI implementation, look at: [link](https://musingsinjava.wordpress.com/2014/11/02/enabling-jsf-2-2-and-cdi-1-2-on-tomcat-8/) – nori Sep 02 '15 at 10:33