@ManagedBean批注不起作用,但@Named起作用 [英] @ManagedBean annotation doesnt work, but @Named works
问题描述
我遇到了一些麻烦. 具有@Named作品的类:
I had some troubles. Class with @Named works:
@Named
@SessionScoped
public class UserBean {
@Autowired
UserBo userBo;
public void setUserBo(UserBo userBo) {
this.userBo = userBo;
}
public String printMsgFromSpring() {
return userBo.getMessage();
}
}
但这是行不通的,并且会产生错误:
but this is doesnt work and generates error:
javax.servlet.ServletException javax.faces.webapp.FacesServlet.service(FacesServlet.java:606) 根本原因 java.lang.NullPointerException com.mkyong.UserBean.printMsgFromSpring(UserBean.java:24)
javax.servlet.ServletException javax.faces.webapp.FacesServlet.service(FacesServlet.java:606) root cause java.lang.NullPointerException com.mkyong.UserBean.printMsgFromSpring(UserBean.java:24)
@ManagedBean
@SessionScoped
public class UserBean {
@Autowired
UserBo userBo;
public void setUserBo(UserBo userBo) {
this.userBo = userBo;
}
public String printMsgFromSpring() {
return userBo.getMessage();
}
}
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:body>
<h1>JSF 2.0 + Spring Example</h1>
#{userBean.printMsgFromSpring()}
</h:body>
faces-config.xml:
faces-config.xml :
<?xml version="1.0" encoding="UTF-8"?>
<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_1.xsd"
version="2.1">
<application>
<el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
</application>
</faces-config>
服务类别:
@Named
public class UserBoImpl implements UserBo{
public String getMessage() {
return "JSF 2 + Spring Integration";
}
}
web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>JavaServerFaces</display-name>
<!-- 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>
<welcome-file-list>
<welcome-file>default.xhtml</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>facesServlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>facesServlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
</web-app>
applicationContext.xml:
applicationContext.xml :
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<context:component-scan base-package="com.mkyong" />
</beans>
推荐答案
JSF不会处理@Autowired
.如以下示例所示,应将其替换为@ManagedProperty
:
JSF won't process the @Autowired
. You should replace that with @ManagedProperty
as in the following example:
@ManagedProperty(value="#{userBoSpringName}")
UserBo userBo;
此处userBoSpringName
应该与您的spring上下文文件中的bean的名称相对应.
此选项仅在JSF托管Bean中可用.
Here userBoSpringName
should correspond to the name of the bean in your spring context file.
This option is usable in a JSF managed bean only.
从JSF 2.2开始,您还可以在JSF托管bean中使用@Inject
来执行资源注入
As of JSF 2.2, you can also use @Inject
in a JSF managed bean to perform resource injections
这篇关于@ManagedBean批注不起作用,但@Named起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!