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

时间:2013-10-14 22:55:37

标签: java-ee spring-mvc jsf-2 cdi

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

2 个答案:

答案 0 :(得分:1)

JSF不会处理@Autowired。您应该将其替换为@ManagedProperty,如以下示例所示:

    @ManagedProperty(value="#{userBoSpringName}")
    UserBo userBo;

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

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

答案 1 :(得分:0)

你做了一些我认为错误的假设。 Spring可以读取@NamedInject注释,但我怀疑你的JSF DI容器可以读取Autowired注释。在这种情况下

@Named
@SessionScoped
public class UserBean {

    @Autowired
    UserBo userBo;

    public void setUserBo(UserBo userBo) {
        this.userBo = userBo;
    }

    public String printMsgFromSpring() {
        return userBo.getMessage();
    }
}

我相信Spring正在创建一个UserBean实例(因为@Named)并注入一个UserBo bean(也来自@Named类)。 Spring忽略SessionScoped

然而,在这种情况下,

@ManagedBean
@SessionScoped
public class UserBean {
    @Autowired
    UserBo userBo;

    public void setUserBo(UserBo userBo) {
      this.userBo = userBo;
    }

    public String printMsgFromSpring() {
      return userBo.getMessage();
    }
}

我相信你的JSF容器正在创建这个bean,但不知道@Autowired是什么,所以不处理它,它忽略它。

如果您向我们展示了您的Spring上下文配置以及您希望上述bean来自何处,我的回答会更清楚。