2016-08-12 73 views
0

我尝试JSF与Spring集成,所以我有很简单的代码来解决春节属性值:无法与JSF

Manged豆

@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中获取消息属性的值。

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

回答

0

由于您让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> 

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

+0

感谢您的回答。它只有在applicationContext.xml而不是JSF bean类中使用sessionCope时才被修复?或者你的意思是我也应该直接使用#{userData.getMessage}总是(getter)? –

+0

@ R.Almoued吸气剂是一个错字,我已经修好了。我的意思是你必须引用spring bean,而不是JSF。你让spring管理bean,而不是JSF。 –

+1

非常感谢您的解决方案。 –