@ManagedBean注释不起作用,但@Named起作用

我遇到了麻烦。具有@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>

回答:

JSF不会处理@Autowired@ManagedProperty在下面的示例中,应将其替换为:

    @ManagedProperty(value="#{userBoSpringName}")

UserBo userBo;

这里userBoSpringName应该与您的spring上下文文件中的bean名称相对应。此选项仅在JSF托管Bean中可用。

从JSF 2.2开始,您还可以@Inject在JSF托管Bean中使用它来执行资源注入

以上是 @ManagedBean注释不起作用,但@Named起作用 的全部内容, 来源链接: utcz.com/qa/406040.html

回到顶部