Note that there are some explanatory texts on larger screens.

plurals
  1. PO@ManagedBean annotation doesnt work, but @Named works
    text
    copied!<p>I had some troubles. Class with @Named works:</p> <pre><code>@Named @SessionScoped public class UserBean { @Autowired UserBo userBo; public void setUserBo(UserBo userBo) { this.userBo = userBo; } public String printMsgFromSpring() { return userBo.getMessage(); } } </code></pre> <p>but this is doesnt work and generates error:</p> <p><em>javax.servlet.ServletException javax.faces.webapp.FacesServlet.service(FacesServlet.java:606) root cause java.lang.NullPointerException com.mkyong.UserBean.printMsgFromSpring(UserBean.java:24)</em></p> <pre><code>@ManagedBean @SessionScoped public class UserBean { @Autowired UserBo userBo; public void setUserBo(UserBo userBo) { this.userBo = userBo; } public String printMsgFromSpring() { return userBo.getMessage(); } } </code></pre> <p>xhtml page:</p> <pre><code> &lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html"&gt; &lt;h:body&gt; &lt;h1&gt;JSF 2.0 + Spring Example&lt;/h1&gt; #{userBean.printMsgFromSpring()} &lt;/h:body&gt; </code></pre> <p></p> <p>faces-config.xml :</p> <pre><code> &lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;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"&gt; &lt;application&gt; &lt;el-resolver&gt;org.springframework.web.jsf.el.SpringBeanFacesELResolver&lt;/el-resolver&gt; &lt;/application&gt; &lt;/faces-config&gt; </code></pre> <p>Service class:</p> <pre><code>@Named public class UserBoImpl implements UserBo{ public String getMessage() { return "JSF 2 + Spring Integration"; } } </code></pre> <p>web.xml:</p> <pre><code> &lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;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"&gt; &lt;display-name&gt;JavaServerFaces&lt;/display-name&gt; &lt;!-- Add Support for Spring --&gt; &lt;listener&gt; &lt;listener-class&gt; org.springframework.web.context.ContextLoaderListener &lt;/listener-class&gt; &lt;/listener&gt; &lt;listener&gt; &lt;listener-class&gt; org.springframework.web.context.request.RequestContextListener &lt;/listener-class&gt; &lt;/listener&gt; &lt;welcome-file-list&gt; &lt;welcome-file&gt;default.xhtml&lt;/welcome-file&gt; &lt;/welcome-file-list&gt; &lt;servlet&gt; &lt;servlet-name&gt;facesServlet&lt;/servlet-name&gt; &lt;servlet-class&gt;javax.faces.webapp.FacesServlet&lt;/servlet-class&gt; &lt;load-on-startup&gt;1&lt;/load-on-startup&gt; &lt;/servlet&gt; &lt;servlet-mapping&gt; &lt;servlet-name&gt;facesServlet&lt;/servlet-name&gt; &lt;url-pattern&gt;*.xhtml&lt;/url-pattern&gt; &lt;/servlet-mapping&gt; &lt;/web-app&gt; </code></pre> <p>applicationContext.xml :</p> <pre><code> &lt;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"&gt; &lt;context:component-scan base-package="com.mkyong" /&gt; &lt;/beans&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload