2012-09-18 168 views
3

我正在试验JSF和Primefaces(JSF 2.0.2,PrimeFaces 3.0.5,Spring 3.0.0)。看来我无法访问托管bean从XHTML页面,如@Scope(“请求”)不起作用

<h:inputText id="lastName" value="#{personalBean.personal_Basic.firstName}" label="Last Name" required="true" /> 

请求从命令链接的调用到bean方法,服务启动,并返回页面。我可以在服务器控制台Bean中看到服务方法被执行。当我尝试使用上面的bean属性时,我没有看到任何东西。不过,我能够访问用于会话管理用户的会话范围的bean。

对不起,如果这个问题太天真了。任何解决问题的输入都非常感谢。

下面是我的代码片段。这就是我所说的bean:

<h:form> 
    <p:commandLink id="ajax" actionListener="#{personalBean.getPersonalInfo}" style="margin-left:5px;"> 
     <h:outputText value="Personal Info" /> <br/> 
    </p:commandLink> 
</h:form> 

下面是请求作用域管理bean。

package com.test.model; 
@ManagedBean 
@Scope("request") 
public class PersonalBean implements Serializable { 

private static final long serialVersionUID = 199L; 
private Personal_Basic personal_Basic; 
private IPersonalService personalService; 

@Inject 
public PersonalBean(final IPersonalService personalService) { 
    this.personalService = personalService; 
} 
public String getPersonalInfo() { 
    System.out.println("\n\nPersonalBean#getPersonalInfo called...**"); 
    User user = null; 
    Personal_Basic personal_Basic = personalService.getPersonalBasic(); 
    this.setPersonal_Basic(personal_Basic); 
    System.out.println("personal_Basic data:"+personal_Basic); 
    System.out.println("PersonalBean#getPersonalInfo ended..."); 
    return "/page/personal.html"; 
} 
// Getters/setters for class members followed 
} 

PersonalService实现用@Service注释。

下面是spring configuration xml的摘录。

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:p="http://www.springframework.org/schema/p" 
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:tx="http://www.springframework.org/schema/tx" 
    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.0.xsd 
     http://www.springframework.org/schema/tx 
     http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
     http://www.springframework.org/schema/aop 
     http://www.springframework.org/schema/aop/spring-aop-3.0.xsd 
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 
    <context:annotation-config/> 
    <context:component-scan base-package="com.test"/> 
................... 
................... 
</beans> 

下面是从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> 
    <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver> 
     <resource-bundle> 
      <base-name>Messages</base-name> 
      <var>msg</var> 
     </resource-bundle> 
     <locale-config> 
      <default-locale>en</default-locale> 
     </locale-config> 
    </application> 
........................... 
</faces-config> 
+0

任何错误消息? –

+0

服务器(Tomcat 6.0)控制台,日志中没有错误消息。 :( – bkrish

+2

@范围与JSF没有任何关系,它是Spring的一部分,所以为了得到能够回答你问题的正确的人的关注,适当地标记问题是很重要的。缺少'[spring]'标签给你的问题 – BalusC

回答

4

<context:component-scan base-package="com.test"/>元件扫描的摘录和登记由默认与@Component,@Repository,@Service或@Controller的所有豆。

Spring还提供对javax.annotation.ManagedBean(而不是javax.faces.bean.ManagedBean)和JSR-330标准注释的支持,并且这些标注也由Spring扫描,但您需要将以下依赖项添加到您的项目中。

<dependency> 
    <groupId>javax.inject</groupId> 
    <artifactId>javax.inject</artifactId> 
    <version>1</version> 
</dependency> 

你可以看到所有的等效注解Spring注解here,你可以看到@Component的等效@Named和范围使用温泉@Scope注解,而不是javax.inject.scope提到。 所以,如果你想春天来管理应用程序中的所有豆,您可以使用@Component,@Namedjavax.annotation.ManagedBean注释并注入它们使用@Autowired或@Inject。

对于你的问题,为什么用javax.faces.bean.ManagedBean注释的bean似乎被初始化,但不起作用是因为@BalusC提到的@Inject在JSF中不受支持,你必须使用@ManagedProperty注释。

在这一切是如何与Spring和JSF工作的一些背景:

其实有两个IOC容器在你的应用程序现在使用JSF和弹簧,当应用程序启动。 首先,在faces-config.xml中配置的org.springframework.web.jsf.el.SpringBeanFacesELResolver将委派给Spring根WebApplicationContext,首先解析对Spring定义的Bean的名称引用,然后解析底层JSF实现的默认解析器。

现在,当JSF的bean是第一次看到了它的构造,然后托管属性通过setter方法设置,因此您可以使用@ManagedProperty注解这样注入一个Spring bean:

package com.test.model; 
@ManagedBean 
@RequestScoped 
public class PersonalBean implements Serializable { 
@ManagedProperty(value="#{personalService}") 
private IPersonalService personalService; 
public IPersonalService getPersonalService() { 
    return personalService; 
} 

public void setPersonalService(IPersonalService personalService) { 
    this.personalService= personalService; 
} 

或您也可以手动获取使用Spring bean的

org.springframework.web.context.support.WebApplicationContextUtils这样的:

private Object getSpringBean(String name){ 
     WebApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(
       (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext()); 
     return ctx.getBean(name); 
} 

,并使用它像这样:

Personal_Basic personal_Basic = ((IPersonalService) getSpringBean("personalService")).getPersonalBasic(); 

而不是使用两个集装箱在应用程序中你可以使用Spring容器通过注释JSF豆@Component来管理所有的咖啡豆,但和有这样没有意义,我知道的,应该是完全可以使用Spring注释。

参见:

+0

完美!从spring xml的ManagedBean和annotation-config。组件扫描就像一个魅力。非常感谢!:) – bkrish

+0

我做到了。但是我仍然想知道为什么@ManagedBean不起作用?我想我们可以将请求范围设置为Component。但它有什么影响? – bkrish

+0

@bkrish,请参阅更新的答案以获得更详细的解释! – Ravi

0

你不能用弹簧范围混合@ManagedBean,你有@ManagedBean和之间做出选择@ RequestScope或@Componenet和@Scope(“会话”)