【问题标题】:Unable to resolve the Spring property value with JSF无法使用 JSF 解析 Spring 属性值
【发布时间】:2023-04-10 02:53:01
【问题描述】:

我尝试将 JSF 与 Spring 集成,所以我的代码非常简单:

豆角

@ManagedBean(name = "userData")
@SessionScoped
public class UserData implements Serializable {

   private static final long serialVersionUID = 1L;

private String message;

   public String getMessage() {
      return message;
   }

   public void setMessage(String message) {
      this.message = message;
   }

   public String getGreetingMessage(){
      return getMessage();
   }
}

ApplicationContext.xml

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

<bean id="messageService"   class="test.UserData">
      <property name="message" value="Hello World" />        
   </bean>
</beans>

Web.xml

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

faces-config.xml

<?xml version="1.0"?>
<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_0.xsd"
              version="2.0">
    <application>
        <!-- SpringBeanFacesELResolver -->
        <el-resolver>
            org.springframework.web.jsf.el.SpringBeanFacesELResolver
        </el-resolver>
    </application>
</faces-config>

home.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://java.sun.com/jsf/html">
   <h:head>
      <title>Spring with JSF</title>
   </h:head>
   <h:body>
    #{userData.greetingMessage}
   </h:body>
</html> 

问题,当我运行 home.xhtml 时,我没有得到按摩的价值!! 它只显示空白页面。似乎无法从 ApplicationContext.xml 中获取 message 属性的值。

我的方法有什么问题以及如何解决?

【问题讨论】:

    标签: spring jsf


    【解决方案1】:

    由于您让 Spring 使用 SpringFacesELResolver 管理 JSF bean,因此不需要 JSF 注释。

    public class UserData implements Serializable {
    
        private static final long serialVersionUID = 1L;
    
        private String message;
    
        public String getMessage() {
          return message;
        }
    
       public void setMessage(String message) {
          this.message = message;
       }
    
    }
    

    并且,在您的 applicationContext.xml 中(注意 session scope):

    <bean id="userData" class="test.UserData" scope="session">
      <property name="message" value="Hello World" />        
    </bean>
    

    以这种方式访问​​它:

    <?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://java.sun.com/jsf/html">
       <h:head>
          <title>Spring with JSF</title>
       </h:head>
       <h:body>
        #{userData.message}
       </h:body>
    </html> 
    

    如果正确配置了 JSF servlet,应该给出一个 Hello World HTML 输出。

    【讨论】:

    • 感谢您的回答。只有在 applicationContext.xml 中而不是 JSF bean 类中使用 sessionCope 时才修复?或者你的意思是我也应该直接使用#{userData.getMessage}(getter)??
    • @R.Almoued getter 是一个错字,我已经修复了。我的意思是你必须参考spring bean,而不是JSF。你让 spring 管理 bean,而不是 JSF。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-10
    • 2013-07-25
    相关资源
    最近更新 更多