2013-10-06 76 views
2

我對一個非常簡單的JSF頁面有點麻煩,我嘗試了不同的解決方案,我在這裏找到但沒有一個工作。你能幫我發現一下,爲什麼我不能做出這個JSF工作的簡單例子?沒有找到JSF屬性

這是pom.xml中

<project xmlns="http://maven.apache.org/POM/4.0.0" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
    http://maven.apache.org/maven-v4_0_0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 
    <groupId>com.mkyong.core</groupId> 
    <artifactId>primefaces</artifactId> 
    <packaging>war</packaging> 
    <version>1.0-SNAPSHOT</version> 
    <name>primefaces Maven Webapp</name> 
    <url>http://maven.apache.org</url> 

    <repositories> 
     <repository> 
      <id>prime-repo</id> 
      <name>PrimeFaces Maven Repository</name> 
      <url>http://repository.primefaces.org</url> 
      <layout>default</layout> 
     </repository> 
    </repositories> 

    <dependencies> 

     <!-- PrimeFaces --> 
     <dependency> 
      <groupId>org.primefaces</groupId> 
      <artifactId>primefaces</artifactId> 
      <version>4.0</version> 
     </dependency> 

     <!-- JSF 2 --> 
     <dependency> 
      <groupId>com.sun.faces</groupId> 
      <artifactId>jsf-api</artifactId> 
      <version>2.2.4</version> 
     </dependency> 

     <dependency> 
      <groupId>com.sun.faces</groupId> 
      <artifactId>jsf-impl</artifactId> 
      <version>2.2.4</version> 
     </dependency> 

     <dependency> 
      <groupId>javax.servlet</groupId> 
      <artifactId>jstl</artifactId> 
      <version>1.2</version> 
     </dependency> 

     <!--Servlet api already provided by tomcat--> 
     <!--<dependency>--> 
     <!--<groupId>javax.servlet</groupId>--> 
     <!--<artifactId>servlet-api</artifactId>--> 
     <!--<version>2.5</version>--> 
     <!--</dependency>--> 

     <dependency> 
      <groupId>javax.servlet.jsp</groupId> 
      <artifactId>jsp-api</artifactId> 
      <version>2.1</version> 
     </dependency> 

     <!-- EL --> 
     <dependency> 
      <groupId>org.glassfish.web</groupId> 
      <artifactId>el-impl</artifactId> 
      <version>2.2</version> 
     </dependency> 

     <!-- Tomcat 6 need this --> 
     <dependency> 
      <groupId>com.sun.el</groupId> 
      <artifactId>el-ri</artifactId> 
      <version>1.0</version> 
     </dependency> 


    </dependencies> 
    <build> 
     <plugins> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-compiler-plugin</artifactId> 
       <version>2.3.2</version> 
       <configuration> 
        <source>1.6</source> 
        <target>1.6</target> 
       </configuration> 
      </plugin> 
     </plugins> 
    </build> 
</project> 

這是在web.xml

<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_3_0.xsd" 
     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"> 

    <!-- Change to "Production" when you are ready to deploy --> 
    <context-param> 
     <param-name>javax.faces.PROJECT_STAGE</param-name> 
     <param-value>Development</param-value> 
    </context-param> 

    <!-- Welcome page --> 
    <welcome-file-list> 
     <welcome-file>faces/index.xhtml</welcome-file> 
    </welcome-file-list> 

    <!-- JSF mapping --> 
    <servlet> 
     <servlet-name>Faces Servlet</servlet-name> 
     <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 

    <!-- Map these files with JSF --> 
    <servlet-mapping> 
     <servlet-name>Faces Servlet</servlet-name> 
     <url-pattern>/faces/*</url-pattern> 
    </servlet-mapping> 
    <servlet-mapping> 
     <servlet-name>Faces Servlet</servlet-name> 
     <url-pattern>*.jsf</url-pattern> 
    </servlet-mapping> 
    <servlet-mapping> 
     <servlet-name>Faces Servlet</servlet-name> 
     <url-pattern>*.faces</url-pattern> 
    </servlet-mapping> 
    <servlet-mapping> 
     <servlet-name>Faces Servlet</servlet-name> 
     <url-pattern>*.xhtml</url-pattern> 
    </servlet-mapping> 

</web-app> 

這是在faces-config.xml

<?xml version='1.0' encoding='UTF-8'?> 
<faces-config version="2.2" 
       xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
       xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd"> 
</faces-config> 

這是視圖頁面index.xhtml

<?xml version="1.0" encoding="UTF-8"?> 
<!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:h="http://xmlns.jcp.org/jsf/html" 
     xmlns:f="http://xmlns.jcp.org/jsf/core" 
     xmlns:ui="http://xmlns.jcp.org/jsf/facelets" 
     xmlns:p="http://primefaces.org/ui"> 
<h:head> 
</h:head> 
<h:body> 
    <h:form> 
     Enter your name 
     <h:inputText value="#{helloBean.input}" /> 
     <h:commandButton value="submit" action="#{helloBean.submit}" /> 
    </h:form> 
    <h:outputText value="#{helloBean.output}" /> 


</h:body> 
</html> 

這是支持bean EditorBean

package com.mykong.editor; 

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

@ManagedBean 
@RequestScoped 
public class HelloBean { 

    private String input; 
    private String output; 

    public void submit() { 
     output = String.format("Hello %s!", input); 
    } 

    public String getInput() { 
     return input; 
    } 

    public String getOutput() { 
     return output; 
    } 

    public void setInput(String input) { 
     this.input = input; 
    } 

} 

編譯順利,而我部署使用命令mvn tomcat:run,但是當我在球場上鍵入一些內容並點擊提交按鈕,我看到的卻是一個例外:

javax.el.PropertyNotFoundException: /index.xhtml @15,51 value="#{helloBean.input}": Target Unreachable, identifier 'helloBean' resolved to null 
    at com.sun.faces.facelets.el.TagValueExpression.getType(TagValueExpression.java:100) 
    at com.sun.faces.renderkit.html_basic.HtmlBasicInputRenderer.getConvertedValue(HtmlBasicInputRenderer.java:95) 
    at javax.faces.component.UIInput.getConvertedValue(UIInput.java:1046) 
    at javax.faces.component.UIInput.validate(UIInput.java:976) 
    at javax.faces.component.UIInput.executeValidate(UIInput.java:1249) 
    at javax.faces.component.UIInput.processValidators(UIInput.java:712) 
    at javax.faces.component.UIForm.processValidators(UIForm.java:253) 
    at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1258) 
    at javax.faces.component.UIComponentBase.processValidators(UIComponentBase.java:1258) 
    at javax.faces.component.UIViewRoot.processValidators(UIViewRoot.java:1195) 
    at com.sun.faces.lifecycle.ProcessValidationsPhase.execute(ProcessValidationsPhase.java:76) 
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101) 
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:198) 
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:646) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) 
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233) 
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) 
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) 
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) 
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) 
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298) 
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857) 
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588) 
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489) 
    at java.lang.Thread.run(Thread.java:724) 
Caused by: javax.el.PropertyNotFoundException: Target Unreachable, identifier 'helloBean' resolved to null 
    at com.sun.el.parser.AstValue.getTarget(AstValue.java:131) 
    at com.sun.el.parser.AstValue.getType(AstValue.java:76) 
    at com.sun.el.ValueExpressionImpl.getType(ValueExpressionImpl.java:195) 
    at com.sun.faces.facelets.el.TagValueExpression.getType(TagValueExpression.java:98) 
    ... 25 more 
+0

'這是支持bean EditorBean'。你解釋的內容與給定的代碼不符,這是你的真實代碼嗎? –

回答

3

從您的pom.xml - 您使用的是JSF 2.2。而您的.xhtml文件使用http://java.sun.com/...名稱空間。問題在於JSF 2.2引入了以http://xmlns.jcp.org/...開頭的新命名空間。

╔════════════════════════════════════════╦═════════════════════════════════════════╗ 
║    Old namespace    ║   JSF 2.2 namespace   ║ 
╠════════════════════════════════════════╬═════════════════════════════════════════╣ 
║ http://java.sun.com/jsf/core   ║ http://xmlns.jcp.org/jsf/core   ║ 
║ http://java.sun.com/jsf/html   ║ http://xmlns.jcp.org/jsf/html   ║ 
║ http://java.sun.com/jsf/facelets  ║ http://xmlns.jcp.org/jsf/facelets  ║ 
║ http://java.sun.com/jsf/composite  ║ http://xmlns.jcp.org/jsf/composite  ║ 
║ http://java.sun.com/jsp/jstl/core  ║ http://xmlns.jcp.org/jsp/jstl/core  ║ 
║ http://java.sun.com/jsp/jstl/functions ║ http://xmlns.jcp.org/jsp/jstl/functions ║ 
╚════════════════════════════════════════╩═════════════════════════════════════════╝ 

編輯:

而且faces-config.xml應爲2.2:

<?xml version='1.0' encoding='UTF-8'?> 
<faces-config version="2.2" 
    xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facesconfig_2_2.xsd"> 

</faces-config> 

更新:

代碼在GlassFish 4.0測試和工程只是罰款,必須有EL2.2和Tomcat 6的問題,請參閱here for d etails。

+0

我更改了.xhtml文件中的xmlns。但我仍然有同樣的問題。 這是我目前有:'' – sfrj

+0

@sfrj嘗試改變'面孔,config.xml' 2.2爲好,看到我的編輯。 – rdcrng

+0

你必須有一個老編輯,我的確可以工作。還要注意'faces-config.xml'的命名空間。但是,我不確定這是否確實是問題,但嘗試將所有內容都設置爲該庫的版本是有意義的。 – rdcrng

1

我目前使用EL 2.2與Tomcat 6服務器。所有我需要的是:

<dependency> 
    <groupId>javax.el</groupId> 
    <artifactId>el-api</artifactId> 
    <version>2.2</version> 
</dependency> 

<dependency> 
    <groupId>org.glassfish.web</groupId> 
    <artifactId>el-impl</artifactId> 
    <version>2.2</version> 
</dependency> 

你不必在你的POM文件的第一個。除此之外,你還包括el-ri,這可能會在你的el庫之間產生一個衝突,就像@rdcrng點。

此外,您的Web應用程序可配置爲servlet 3.0。這就是你在web.xml文件version="3.0"中所說的。作爲一個servlet 2.5容器的Tomcat是not compatible

UPDATE

這對我使用的測試目的在Tomcat中6(Primefaces,鑽嘴魚科和EL 2項目的Maven配置。2):

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 

    <groupId>com.mycompany</groupId> 
    <artifactId>basic-jsf</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 
    <packaging>war</packaging> 

    <name>basic-jsf</name> 
    <url>http://maven.apache.org</url> 

    <repositories> 
     <repository> 
      <id>prime-repo</id> 
      <name>PrimeFaces Maven Repository</name> 
      <url>http://repository.primefaces.org</url> 
      <layout>default</layout> 
     </repository> 
    </repositories> 

    <properties> 
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
    </properties> 

    <build> 
     <plugins> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-compiler-plugin</artifactId> 
       <version>3.1</version> 
       <configuration> 
        <source>1.6</source> 
        <target>1.6</target> 
       </configuration> 
      </plugin> 
     </plugins> 
    </build> 

    <dependencies> 
     <dependency> 
      <groupId>org.primefaces</groupId> 
      <artifactId>primefaces</artifactId> 
      <version>3.5</version> 
     </dependency> 
     <dependency> 
      <groupId>com.sun.faces</groupId> 
      <artifactId>jsf-api</artifactId> 
      <version>2.1.25</version> 
      <scope>compile</scope> 
     </dependency> 
     <dependency> 
      <groupId>com.sun.faces</groupId> 
      <artifactId>jsf-impl</artifactId> 
      <version>2.1.25</version> 
      <scope>compile</scope> 
     </dependency> 
     <dependency> 
      <groupId>javax.servlet</groupId> 
      <artifactId>jstl</artifactId> 
      <version>1.2</version> 
     </dependency> 
     <dependency> 
      <groupId>javax.servlet</groupId> 
      <artifactId>servlet-api</artifactId> 
      <version>2.5</version> 
     </dependency> 
     <dependency> 
      <groupId>javax.el</groupId> 
      <artifactId>el-api</artifactId> 
      <version>2.2</version> 
     </dependency> 

     <dependency> 
      <groupId>org.glassfish.web</groupId> 
      <artifactId>el-impl</artifactId> 
      <version>2.2</version> 
     </dependency> 
    </dependencies> 
</project> 

我的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" 
    id="WebApp_ID" version="2.5"> 
    <display-name>jsf2testproject</display-name> 

    <welcome-file-list> 
     <welcome-file>index.xhtml</welcome-file> 
    </welcome-file-list> 
    <context-param> 
     <param-name>com.sun.faces.expressionFactory</param-name> 
     <param-value>com.sun.el.ExpressionFactoryImpl</param-value> 
    </context-param> 

    <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> 

    <context-param> 
     <param-name>javax.faces.PROJECT_STAGE</param-name> 
     <param-value>Development</param-value> 
    </context-param> 
</web-app> 
+0

我知道上面的代碼完全在Glassfish的4,我可以證實,但它不會在Tomcat中6能否請您粘貼完整的pom.xml?我不知道,但因爲某些原因添加這2個依賴不能解決問題 – sfrj