spring - @Scope ("request") 不工作

标签 spring jsf-2 scope request

我正在试验 JSF 和 Primefaces(JSF 2.0.2、PrimeFaces 3.0.5、Spring 3.0.0)。看来我无法从 xhtml 页面访问托管 bean,例如

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

请求从命令链接调用bean 方法、服务开始并返回页面。我可以在服务器控制台 Bean 中看到,服务方法已执行。当我尝试使用上面的 bean 属性时,我什么也没看到。但是,我能够访问用于用户 session 管理的 session 作用域 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 配置 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>

最佳答案

<context:component-scan base-package="com.test"/>默认情况下,element 会扫描并注册所有使用@Component、@Repository、@Service 或@Controller 注解的 bean。

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 并且对于范围使用 Springs @Scope 注释而不是 javax.inject.scope如前所述。 因此,如果您希望 Spring 管理应用程序中的所有 beans,您可以使用 @Component , @Namedjavax.annotation.ManagedBean注释并使用@Autowired 或@Inject 注入(inject)它们。

关于你的问题,为什么 bean 用 javax.faces.bean.ManagedBean 注释似乎已初始化但不起作用是因为正如@BalusC 所述,JSF 不支持@Inject,您必须使用@ManagedProperty 注释。

有关它如何与 Spring 和 JSF 一起工作的一些背景知识:

实际上,当应用程序启动时,您的应用程序中现在有两个 IOC 容器,分别是 JSF 和 Spring。 首先,在您的 faces-config.xml 中配置的 org.springframework.web.jsf.el.SpringBeanFacesELResolver 委托(delegate)给 Spring 根 WebApplicationContext 首先解析对 Spring 定义的 bean 的名称引用,然后解析底层 JSF 实现的默认解析器。

现在,当第一次查找 JSF bean 时,它会被构造,然后通过 setter 方法设置托管属性,因此您可以使用 @ManagedProperty 注释注入(inject) 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 容器通过使用 @Component 注释 JSF bean 来管理您的所有 bean,并且没有任何影响,我知道并且应该完全可以使用 Spring 注解。

另见:

关于spring - @Scope ("request") 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12470512/

相关文章:

java - 无法将 JSON 映射到 Java bean

Spring Boot JSP 未找到

java - 链接触发方法多个编号。次

java - Joda Time - Hibernate 将昨天的日期插入数据库

java - 如何解决 spring-boot-devtools 依赖问题?

validation - primefaces tabView 在选项卡更改时跳过表单验证

spring - 如何在 Spring 应用程序中使用 Maven 将 JSF 2.2 更新到 JSF 2.3

java - 我该如何解决这个简单的范围界定错误?

javascript - AngularJS 取消绑定(bind)等效变量

Groovy 范围 - 如何在方法中访问脚本变量