我有一些麻煩。 類具有@Named工作:@ManagedBean註釋不起作用,但@Named作品
@Named
@SessionScoped
public class UserBean {
@Autowired
UserBo userBo;
public void setUserBo(UserBo userBo) {
this.userBo = userBo;
}
public String printMsgFromSpring() {
return userBo.getMessage();
}
}
但是這是行不通的,併產生錯誤:
javax.servlet.ServletException javax.faces.webapp.FacesServlet.service(FacesServlet.java:606) 根源 顯示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中:
<?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:
<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>
請發表您的Spring上下文。 –
Thx,我將applicationContext.xml添加到我的文章 – MeetJoeBlack