无法用JSF解析Spring属性值



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

兽疥癣Bean

@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();
   }
}

<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使用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(注意会话范围)中:

<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输出。

相关内容

  • 没有找到相关文章

最新更新