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. #{test.test}

But I think it must be somthing else.

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>

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"
   xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
      http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
   version="2.5">
   <servlet>
      <servlet-name>Faces Servlet</servlet-name>
      <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
   </servlet>
   <servlet-mapping>
      <servlet-name>Faces Servlet</servlet-name>
      <url-pattern>/faces/*</url-pattern>
   </servlet-mapping>
   <welcome-file-list>
      <welcome-file>new.xhtml</welcome-file>
   </welcome-file-list>
   <context-param>
      <param-name>javax.faces.PROJECT_STAGE</param-name>
      <param-value>Development</param-value>
   </context-param>
</web-app>
Andrew
  • 266
  • 6
  • 18

2 Answers2

4

If JSF did not find the bean, you would not have seen #{test.test}, but simply an empty string in place of that EL expression. That the EL expression is not been evaluated has a different cause: the current HTTP request did not invoke the FacesServlet at all and hence it was not able to perform all the JSF works for you. In order to invoke it, you need to make sure that the request URL as you see in browser's address bar matches the URL pattern of the FacesServlet as definied in web.xml.

Another evidence can be found by looking at the HTML source in webbrowser by rightclick, View Source. You would have noticed that all those <h:xxx> tags are not been parsed at all. The FacesServlet is also the one responsible for that job.

Imagine that your webapp runs on http://example.com/context/ and you have a Facelet file /page.xhtml then there are following scenarios depending on the URL pattern of the FacesServlet:

  • If it's *.jsf, then you need to open by http://example.com/context/page.jsf
  • If it's *.faces, then you need to open by http://example.com/context/page.faces
  • If it's /faces/*, then you need to open by http://example.com/context/faces/page.xhtml

An alternative is to just map it on an URL pattern of *.xhtml. This way you never need to worry about virtual URLs.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Yes tags not parsed, how to fix it, I add my web.xml file. – Andrew Jan 25 '13 at 16:06
  • That's already answered. I recite: *"you need to make sure that the request URL as you see in browser's address bar matches the URL pattern of the FacesServlet as definied in web.xml."* An alternative is to change whatever URL pattern you've definied in `web.xml` to `*.xhtml`, so that you never need to worry about virtual URLs. – BalusC Jan 25 '13 at 16:07
  • Not sure, but isn't it required to map *.xhtml ? Or is /faces/* enough? – xoned Jan 25 '13 at 16:08
  • 1
    @Timo: Both will work as good. That's a choice which the developer has to make itself based on business requirements. See also e.g. http://stackoverflow.com/questions/3008395/jsf-facelets-sometimes-i-see-the-url-is-jsf-and-sometimes-xhtml-why/3008504#3008504 But in this particular case, it's likely just complete ignorance. – BalusC Jan 25 '13 at 16:08
  • Andrew, I expanded the answer somewhat to explain that part more clearly. – BalusC Jan 25 '13 at 16:12
  • is your page under /faces/ directory or not? – roel Jan 25 '13 at 16:13
  • Because in his web.xml it states Faces Servlet /faces/* Or am I mistaken that only the url pattern will be used by facelets? – roel Jan 28 '13 at 09:03
0

You need to store faces-config.xml and web.xml inside WEB-INF folder. The xhtml files should be inside WebContent.

Rafael Companhoni
  • 1,780
  • 1
  • 15
  • 31