1

good afternoon every body, I'm stucked on the following problem...

I have to use jsf and cdi on a project, I've seen that when i use cdi i should be usign the @named instead of @ManagedBean, but when i change it the message is no longer shown on the browser.

it is just a test for now, so it has no complex code and stuff.

here is my index:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html">
<h:head>
    <title>Hello JSF!</title>
</h:head>
<h:body>
    <h:outputText value="#{teste.message}" />
</h:body>
</html>

here is my bean:

package jsfConnection;

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

@Named("teste")
@RequestScoped
public class Teste {
    public String getMessage() {
        return "ola";
    }

}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">

    <description>Patrimônio web.xml</description>
    <!-- Nome da sua aplicação -->
<display-name>Patrimônio</display-name>
<!-- Faces Servlet -->

<!-- Configuração do arquivo inicial quando a aplicação for inicializada -->
<welcome-file-list>
    <welcome-file>index.xhtml</welcome-file>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.jsf</welcome-file>
</welcome-file-list>

<!-- Configuração do Controlador Faces Servlet -->
<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>

when i use @ManagedBean it shows normally but when i switch to @Named it simply does not print anything...

  • Any error/exception on the terminal? What container do you use? – Tiny Apr 19 '15 at 18:35
  • it does not show anything – Thiago Ramires Apr 19 '15 at 18:40
  • Apache Tomcat is a bare-bones Servlet container. It is not a Java EE compliant container but I presumed that otherwise, those import statements `import javax.enterprise.context.RequestScoped;` and `import javax.inject.Named;` would have issued a compile-time error, if you had not already have CDI implementation which you are supposed to do on your own. Did you include and configure CDI in your real project on Tomcat? – Tiny Apr 19 '15 at 19:40

2 Answers2

1

This is a duplicate of "How to install and use CDI on Tomcat?". Please note that I attempted to make some detailed instructions on my Blog: "Enabling JSF 2.2 and CDI 1.2 on Tomcat 8". These instructions should work for Tomcat 7 too, although I did not test. I might also be good to note that you might be better off using a version of TomEE, which already comes packaged with an implementation of CDI and JSF.

See the Blog for more detailed information, but setting it all up on Tomcat requires 2 main steps:

  • Defining the right provided and runtime libraries as your maven dependencies
  • Configure and Bootstrap the CDI Implementation (Weld).

Setting up bootstrapping happens using the following 3 files:

  • WEB-INF/web.xml
  • WEB-INF/beans.xml
  • META-INF/context.xml - This META-INF directory can be found at the webapp root, or the same location where WEB-INF can be found.

Note also that as part of the web.xml file, you define how requests (URL's) are mapped to a physical location (path). The <servlet-mapping> for your Faces Servlet, which can be either as a path prefix, or as an extension: <url-pattern>/faces/*</url-pattern> vs <url-pattern>*.xhtml</url-pattern>.

Community
  • 1
  • 1
YoYo
  • 7,796
  • 8
  • 50
  • 67
  • The problem is that I already included weld to my pom, so it was suposed to work, wasnt it? – Thiago Ramires Apr 19 '15 at 20:03
  • just a question, where should be the META-INF folder? – Thiago Ramires Apr 19 '15 at 20:24
  • I got a new problem now, when i added the listener class on web.xml it now shows me a 404 on the hole project – Thiago Ramires Apr 19 '15 at 21:09
  • Consolidated several comments by updating the original answer posting. Most recently, in regards to the 401, just change the url pattern in the `web.xml` to how you had it originally: *.xhtml, and all should be fine again. – YoYo Apr 20 '15 at 06:58
0

Tomcat is not a Java EE server, so CDI doesn't work out of the box. To use CDI in it, you need to 1) include a CDI implementation and 2) configure it in web.xml.

See the documentation of your CDI implementation (Weld or OpenWebBeans) for details.

Harald Wellmann
  • 11,788
  • 2
  • 36
  • 56