2013-10-18 62 views
0

我試圖與JSF 2一起使用Spring 3,但我已經遇到問題執行以下操作:JSF 2 +彈簧3不能訪問bean

<h:outputText value="#{sampleController.hello}" /> 

使用一個靜態值只是工作很好,但從Java獲得的價值不(它不顯示任何東西),例如

<body> 
    Test <h:outputText value="#{sampleController.hello}" /> <h:outputText value="hello" /> 
</body> 

會顯示爲

測試你好

SampleController.java:

@Component 
@Scope(value = "session", proxyMode = ScopedProxyMode.TARGET_CLASS) 
public class SampleController { 

    private String hello = "Hello!"; 

    public String getHello() { 
     return hello; 
    } 

    public void setHello(String hello) { 
     this.hello = hello; 
    } 

} 

我用 「JSF 2 +彈簧3集成」 從Mkyong爲項目配置(http://www.mkyong.com/jsf2/jsf-2-0-spring-integration-example/

的applicationContext.xml

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.1.xsd 
    http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.1.xsd"> 

    <context:component-scan base-package="eu.shishigami" /> 

    <context:annotation-config /> 

</beans> 

的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>Sample</display-name> 

    <!-- Add Support for Spring --> 
    <listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 
    <listener> 
     <listener-class> 
      org.springframework.web.context.request.RequestContextListener 
     </listener-class> 
    </listener> 
    <listener> 
     <listener-class>org.springframework.web.util.WebAppRootListener</listener-class> 
    </listener> 

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

    <!-- JSF Mapping --> 
    <servlet> 
     <servlet-name>facesServlet</servlet-name> 
     <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>facesServlet</servlet-name> 
     <url-pattern>*.jsf</url-pattern> 
    </servlet-mapping> 
    <servlet-mapping> 
     <servlet-name>facesServlet</servlet-name> 
     <url-pattern>*.xhtml</url-pattern> 
    </servlet-mapping> 

</web-app> 

faces-config.xml中

<?xml version="1.0" encoding="UTF-8"?> 
<faces-config xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-facesconfig_2_1.xsd" 
    version="2.1"> 

    <application> 
     <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver> 
    </application> 

</faces-config> 
+0

記住使用',而不是''和'''和''標籤。不知道這是否會解決您的問題。 –

+0

謝謝,我相應地編輯了它。可悲的是,這並沒有解決問題。 – Shishigami

+0

嘗試向您的'SampleController'類中添加'@ ManagedBean'註釋。 –

回答

0

卸下Servlet映射JSF,這是爲版本1.2。沒有servlet映射的作品!

<servlet-mapping> 
    <servlet-name>facesServlet</servlet-name> 
    <url-pattern>*.jsf</url-pattern> 
</servlet-mapping>