0

I try to made simple JSF example and have several filers. I use Maven, and have stored in META-INF flolder faces-confid.xml.

In output when try to execute I see :

Welcome to JSF. 

But it must be:

Welcome to JSF. test

Here they are:

Bean file

import java.io.Serializable;
import javax.enterprise.context.RequestScoped;
import javax.inject.Named;


@Named("test")
@RequestScoped
public class TestBean implements Serializable{
    private String test = "test";

    public String getTest() {
        return test;
    }

    public void setTest(String test) {
        this.test = test;
    }    
}

XHTML file:

<?xml version="1.0" encoding="UTF-8"?>
<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html">
    <h:head>
        <title>Welcome</title>
    </h:head>
    <h:body>
        <h3>Welcome to JSF. #{test.test}</h3>
    </h:body>
</html>
Andrew
  • 266
  • 6
  • 18
  • possible duplicate of [JSF not see my annotated Bean](http://stackoverflow.com/questions/14525377/jsf-not-see-my-annotated-bean) – Ravi Kadaboina Jan 25 '13 at 17:32
  • @Ravi: not exactly. In the initial question, the `#{test.test}` appeared plain vanilla. In the current question, it appears as an empty string. – BalusC Jan 25 '13 at 21:20

2 Answers2

2

In a Java EE / JSF web application, there are basically 2 APIs available to manage your beans.

Those, as in your initial question,

import javax.enterprise.context.RequestScoped;
import javax.inject.Named;

@Named("test")
@RequestScoped

are from CDI (Context and Dependency Injection) which is a Java EE standardized way of managing beans (and designed to standardize/supplant among others Spring DI and AOP). This is only available in containers supporting Java EE web profile such as Glassfish, JBoss AS, etc, but not in barebones servlet containers such as Tomcat and Jetty (you can however install it separately; note, Weld is the reference implementation of CDI, like as Mojarra is the reference implementation of JSF).

In order to get CDI to run, you need to have a physical /WEB-INF/beans.xml file in your webapp. The file itself can just be kept empty. Your initial problem suggests that you didn't have one. When this file is absent, then CDI won't be initialized for the webapp, speeding up the server startup time.

Another way, as you figured in your own answer,

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

@ManagedBean("test")
@RequestScoped

are from JSF itself. This is particularly helpful if you intend to keep your web application portable across various containers, including barebones JSP/Servlet containers such as Tomcat and Jetty. They do not require any additional configuration files in order to get to run.

Noted should be that eager=true is unnecessary on a request scoped bean. Even more, it would be ignored anyway. It's only accepted on an application scoped bean. See also e.g. How can I initialize a Java FacesServlet.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • I change dependency in my project to jsf 2.1.0 from jsf 2.2.x and now it don't work again and I can understand why. – Andrew Jan 26 '13 at 07:20
  • Reboot my laptop and it works again, I cant understand anything. – Andrew Jan 26 '13 at 07:53
  • It was apparently not properly been built or deployed. This is a tooling issue, not a JSF issue. – BalusC Jan 26 '13 at 11:51
0

Fixed by using other anotation

@ManagedBean(name = "test", eager = true)
@RequestScoped
Andrew
  • 266
  • 6
  • 18