outputText不会显示任何/无法访问类?

时间:2013-12-24 14:56:02

标签: java jsf javabeans

所以我对这个JSF的东西很新,并且认为我写了一些代码只是为了看它是如何工作的。其实我做了like here in this tutorial。但我甚至无法做最简单的事情。

为了清楚起见我刚刚上了一堂课:

@ManagedBean
@SessionScoped
public class Bean implements Serializable {

    private static final long serialVersionUID = 1L;

    public final String TEXT = "Bean";
}

我的index.xhtml:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:f="http://java.sun.com/jsf/core"
      xmlns:p="http://xmlns.jcp.org/jsf/passthrough">
<head>
<meta charset="UTF-8"/>
<title>JEE</title>
</head>
<body>
    <h3>Output1: <h:outputText value="#{Bean.TEXT}"/></h3>
    <h4>Output2: #{Bean.TEXT}</h4>
</body>
</html>

如果我在Eclipse上启动它,我就不会获得Bean.TEXT的任何输出,就好像我没有任何访问此类的权限一样:

enter image description here

因此在Output1和Output2之后应该有一些文本。我没有看到关于Bean类的任何异常。我该如何调试这样的东西?有什么建议吗?

PS:

这里是我的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">
  <display-name>JavaEE</display-name>
  <welcome-file-list>
    <welcome-file>index.xhtml</welcome-file>
  </welcome-file-list>
  <servlet>
    <servlet-name>Servlet Class</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>Servlet Class</servlet-name>
    <url-pattern>/faces/*</url-pattern>
  </servlet-mapping>
    <servlet-mapping>
    <servlet-name>Servlet Class</servlet-name>
    <url-pattern>*.xhtml</url-pattern>
  </servlet-mapping>
    <servlet-mapping>
    <servlet-name>Servlet Class</servlet-name>
    <url-pattern>*.faces</url-pattern>
  </servlet-mapping>
</web-app>

控制台输出:

Dec 24, 2013 3:36:18 PM org.apache.catalina.core.AprLifecycleListener init
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: /usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
Dec 24, 2013 3:36:18 PM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:JavaEE' did not find a matching property.
Dec 24, 2013 3:36:18 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-bio-8080"]
Dec 24, 2013 3:36:18 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-bio-8009"]
Dec 24, 2013 3:36:18 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 573 ms
Dec 24, 2013 3:36:18 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Dec 24, 2013 3:36:18 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/7.0.47
Dec 24, 2013 3:36:20 PM com.sun.faces.config.ConfigureListener contextInitialized
INFO: Initializing Mojarra 2.2.4 ( 20131003-1354 https://svn.java.net/svn/mojarra~svn/tags/2.2.4@12574) for context '/JavaEE'
Dec 24, 2013 3:36:20 PM com.sun.faces.spi.InjectionProviderFactory createInstance
INFO: JSF1048: PostConstruct/PreDestroy annotations present.  ManagedBeans methods marked with these annotations will have said annotations processed.
Dec 24, 2013 3:36:21 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
Dec 24, 2013 3:36:21 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-bio-8009"]
Dec 24, 2013 3:36:21 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 2284 ms

1 个答案:

答案 0 :(得分:2)

在EL上下文中,您可以通过以前通过注释或配置文件声明的名称来访问bean。如果您没有指定name注释的@ManagedBean元素,那么它将成为简单的类名,第一个字母为decapitalized(如果两个首字母不是大写)。在您的情况下,当您尝试以#{bean}访问它时,#{Bean}可以访问该bean,这是错误的(但如果您将其声明为@ManagedBean(name="Bean")则可能有效。)

另外,值得注意的是,通常只有一个面向servlet的映射,即*.xhtml,所以如果你从web中删除了三个指定的servlet映射,你会更好。 .xml并且只留下一个。