春天-@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属性时,我什么也没看到.但是,我能够访问用于用户会话管理的会话作用域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” />元素默认扫描并注册所有带有@ 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管理应用程序中的所有bean,则可以使用@ Component,@ Named和javax.annotation.ManagedBean批注,并使用@Autowired或@Inject注入它们.
对于您的问题,为什么用javax.faces.bean.ManagedBean注释的bean似乎已初始化但不起作用,是因为如@BalusC所述,JSF不支持@Inject,因此必须使用@ManagedProperty注释.
关于Spring和JSF的全部工作方式的一些背景知识:
实际上,当应用程序启动时,您的应用程序中现在有了JSF和Spring两个IOC容器.
首先,在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容器通过使用@Component注释JSF Bean来管理所有bean,并且我没有意识到任何暗示,并且使用Spring注释应该是完全可以的.
也可以看看:
> Difference between <context:annotation-config> vs <context:component-scan>
> JSR-330 support for component detection is inconsistent
> Spring Documentation – JSF Integration
> Using Spring to Manage JSF beans
标签:scope,request,jsf-2,spring 来源: https://codeday.me/bug/20191031/1979095.html