@ManagedBean annotation doesnt work, but @Named works

匆匆过客 提交于 2019-12-12 09:19:45

问题


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) 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 page:

  <?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>

Service class:

@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>

回答1:


JSF won't process the @Autowired. You should replace that with @ManagedProperty as in the following example:

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

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.

As of JSF 2.2, you can also use @Inject in a JSF managed bean to perform resource injections




回答2:


You've made some assumptions that I believe are wrong. Spring can read @Named and Inject annotations, but I doubt your JSF DI container can read Autowired annotations. In this case

@Named
@SessionScoped
public class UserBean {

    @Autowired
    UserBo userBo;

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

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

I believe Spring is creating a UserBean instance (because of @Named) and injecting a UserBo bean (that also comes from a @Named class). Spring ignores SessionScoped.

In this case, however,

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

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

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

I believe your JSF container is creating this bean, but has no idea what @Autowired is so doesn't process it, it ignores it.

My answer would be more clear if you showed us your Spring context configuration and where you expect the above beans to come from.



来源:https://stackoverflow.com/questions/19370574/managedbean-annotation-doesnt-work-but-named-works

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!