【问题标题】:JSF 2 + Spring 3 can't access beanJSF 2 + Spring 3 无法访问 bean
【发布时间】:2013-10-26 20:56:14
【问题描述】:

我正在尝试将 Spring 3 与 JSF 2 一起使用,但我已经在执行以下操作时遇到了麻烦:

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

}

我使用 Mkyong 的“JSF 2 + Spring 3 Integration”进行项目配置 (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>

【问题讨论】:

  • 记得使用&lt;h:head&gt;&lt;h:body&gt;标签,而不是&lt;head&gt;&lt;body&gt;。不确定这是否能解决您的问题。
  • 谢谢,我进行了相应的编辑。遗憾的是它并没有解决问题。
  • 尝试将@ManagedBean 注释添加到您的SampleController 类中。
  • 不幸的是,也没有运气。我也按照 3.3 @mkyong 中的建议尝试了@Named,但这也没有用。
  • 我已经能够让它在另一台 PC 上运行。它实际上完全一样(我从这个线程中复制了所有内容)但它有效...... owell。

标签: java spring jsf


【解决方案1】:

删除 servlet-mapping jsf,这是针对 1.2 版本的。没有 servlet-mapping 工作!

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

【讨论】:

    猜你喜欢
    • 2011-12-18
    • 2012-08-06
    • 2014-07-06
    • 1970-01-01
    • 1970-01-01
    • 2014-07-02
    • 2011-07-01
    • 2014-12-11
    • 1970-01-01
    相关资源
    最近更新 更多