JSF not see my annotated Bean

徘徊边缘 提交于 2019-12-25 02:42:06

问题


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>

回答1:


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:

  • JSF Facelets: Sometimes I see the URL is .jsf and sometimes .xhtml. Why?



回答2:


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



来源:https://stackoverflow.com/questions/14525377/jsf-not-see-my-annotated-bean

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!