JSF2:从Spring向managedbean注入服务对象? [英] JSF2 : inject service objects to managedbean from Spring?

查看:96
本文介绍了JSF2:从Spring向managedbean注入服务对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经测试了这个,尝试将服务对象注入@ManagedBean,但它失败了,因为userService为null。



我目前正在使用nullpointerexception失败。使用Tomcat 7,JSF 2,这里有一些我的pom.xml

 < properties> 
< java-version> 1.6< / java-version>
< org.springframework-version> 3.0.3.RELEASE< /org.springframework-version>
< org.hibernate-version> 3.6.0.Final< /org.hibernate-version>
....
< / properties>

以下是异常追踪:

  javax.faces.el.E​​valuationException:java.lang.NullPointerException 
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:102)
at com。 sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
at javax.faces.component.UICommand.broadcast(UICommand.java:315)
at javax.faces.component.UIViewRoot。 broadcastEvents(UIViewRoot.java:775)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1267)
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java: 82)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:312)
at org.apache.catalina.core.ApplicationFilte rChain.internalDoFilter(ApplicationFilterChain.java:306)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke( StandardWrapperValve.java:240)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java: 164)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:108)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:558)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:379)
at org .apache.coyote.http11.Http11Processor.process(Http11Processor.java:243)
at org.apache.coyote.http11.Http11Protocol $ Http11ConnectionHandler.process(Http11Protocol.java:259)
at org.apache.tomcat.util.net.JIoEndpoint $ SocketProcessor.run(JIoEndpoint.java:281)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java .util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:636)
引起:java.lang.NullPointerException
at org.albertkam.ui.LoginBean.login(LoginBean.java:38)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl。 java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:616)
at org .apache.el.parser.AstValue.invoke(AstValue.java:262)
at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:278)
at com.sun.faces.facelets .el.TagMethodExpression.invoke(TagMethodExpression.java:98)
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
... 23更多

这是我的ManagedBean:

  @ManagedBean 
@RequestScoped
public class LoginBean {
private MstUser entityMstUser;

@Autowired
private UserService userService;

@PostConstruct
private void init(){
this.entityMstUser = new MstUser();
}

这是我的服务bean ..

  @Service 
公共类UserService {

@Autowired
private UserDAO userDao;

public void login(MstUser entityMstUser){

这是我的applicationContext。 xml ..

 <?xml version =1.0encoding =UTF-8?> 
< beans xmlns =http://www.springframework.org/schema/beans
xmlns:xsi =http://www.w3.org/2001/XMLSchema-instancexmlns :aop =http://www.springframework.org/schema/aop
xmlns:context =http://www.springframework.org/schema/context
xmlns:jee = http://www.springframework.org/schema/jeexmlns:lang =http://www.springframework.org/schema/lang
xmlns:tx =http://www.springframework。 org / schema / txxmlns:util =http://www.springframework.org/schema/util
xsi:schemaLocation =http://www.springframework.org/schema/beans http:/ /www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring -aop-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http: //www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.0.xsd
http://www.springframework.org/schema/ tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema /util/spring-util-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
default-autowire =byName>

< context:component-scan base-package =org.albertkam/>
< context:annotation-config />
< tx:annotation-driven />

< / beans>

我的web.xml

 <?xml version =1.0encoding =UTF-8?> 
< web-app version =2.5xmlns =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- app_2_5.xsd>

< display-name> albert simpleWebapp< / display-name>

< context-param>
< param-name> contextConfigLocation< / param-name>
< param-value>
/WEB-INF/applicationContext.xml
/WEB-INF/datasourceContext.xml
< / param-value>
< / context-param>

< listener>
< listener-class> org.springframework.web.context.ContextLoaderListener< / listener-class>
< / listener>

< servlet>
< servlet-name> facesServlet< / servlet-name>
< servlet-class> javax.faces.webapp.FacesServlet< / servlet-class>
< / servlet>
< servlet-mapping>
< servlet-name> facesServlet< / servlet-name>
< url-pattern> / faces / *< / url-pattern>
< / servlet-mapping>
< welcome-file-list>
< welcome-file> faces / index.xhtml< / welcome-file>
< / welcome-file-list>
< context-param>
< param-name> javax.faces.PROJECT_STAGE< / param-name>
< param-value>开发< / param-value>
< / context-param>

< / web-app>

我的faces-config.xml,虽然这里没有任何相关内容..

 <?xml version =1.0encoding =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_0.xsd
version =2.0>

< application>
< locale-config> < / default-locale>中的
< default-locale>
< supported-locale> zh< / supported-locale>
< / locale-config>
< resource-bundle>
< base-name> org.albertkam.common.messages< / base-name>
< var> msgs< / var>
< / resource-bundle>
< resource-bundle>
< base-name> org.albertkam.common.errors< / base-name>
< var> errs< / var>
< / resource-bundle>

<! - < message-bundle> id.co.sofcograha.override_messages< / message-bundle> - >
< / application>

< factory>
< exception-handler-factory> org.albertkam.common.BusinessExceptionHandlerFactory< / exception-handler-factory>
< / factory>
< / faces-config>

我想知道是否有可能做这样的事情,将一个Spring托管bean注入@ManagedBean? / p>

请分享您的经验..



谢谢!



***********添加了一些新的实验结果****************

 <听者GT; 
< listener-class> org.springframework.web.context.request.RequestContextListener< / listener-class>
< / listener>
< listener>
< listener-class> org.jboss.weld.environment.servlet.Listener< / listener-class>
< / listener>你好,

我测试了这个,尝试注入服务对象是@ManagedBean,但它失败了nullpointerexception,因为userService是null。



我目前正在使用Tomcat 7,JSF 2,这里有一些我的pom.xml

 < properties> 
< java-version> 1.6< / java-version>
< org.springframework-version> 3.0.3.RELEASE< /org.springframework-version>
< org.hibernate-version> 3.6.0.Final< /org.hibernate-version>
....
< / properties>

以下是异常追踪:

  javax.faces.el.E​​valuationException:java.lang.NullPointerException 
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:102)
at com。 sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
at javax.faces.component.UICommand.broadcast(UICommand.java:315)
at javax.faces.component.UIViewRoot。 broadcastEvents(UIViewRoot.java:775)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1267)
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java: 82)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:312)
at org.apache.catalina.core.ApplicationFilte rChain.internalDoFilter(ApplicationFilterChain.java:306)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke( StandardWrapperValve.java:240)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java: 164)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:108)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:558)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:379)
at org .apache.coyote.http11.Http11Processor.process(Http11Processor.java:243)
at org.apache.coyote.http11.Http11Protocol $ Http11ConnectionHandler.process(Http11Protocol.java:259)
at org.apache.tomcat.util.net.JIoEndpoint $ SocketProcessor.run(JIoEndpoint.java:281)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java .util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:636)
引起:java.lang.NullPointerException
at org.albertkam.ui.LoginBean.login(LoginBean.java:38)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl。 java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:616)
at org .apache.el.parser.AstValue.invoke(AstValue.java:262)
at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:278)
at com.sun.faces.facelets .el.TagMethodExpression.invoke(TagMethodExpression.java:98)
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
... 23更多

这是我的ManagedBean:

  @ManagedBean 
@RequestScoped
public class LoginBean {
private MstUser entityMstUser;

@Autowired
private UserService userService;

@PostConstruct
private void init(){
this.entityMstUser = new MstUser();
}

这是我的服务bean ..

  @Service 
公共类UserService {

@Autowired
private UserDAO userDao;

public void login(MstUser entityMstUser){

这是我的applicationContext。 xml ..

 <?xml version =1.0encoding =UTF-8?> 
< beans xmlns =http://www.springframework.org/schema/beans
xmlns:xsi =http://www.w3.org/2001/XMLSchema-instancexmlns :aop =http://www.springframework.org/schema/aop
xmlns:context =http://www.springframework.org/schema/context
xmlns:jee = http://www.springframework.org/schema/jeexmlns:lang =http://www.springframework.org/schema/lang
xmlns:tx =http://www.springframework。 org / schema / txxmlns:util =http://www.springframework.org/schema/util
xsi:schemaLocation =http://www.springframework.org/schema/beans http:/ /www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring -aop-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http: //www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.0.xsd
http://www.springframework.org/schema/ tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema /util/spring-util-3.0.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
default-autowire =byName>

< context:component-scan base-package =org.albertkam/>
< context:annotation-config />
< tx:annotation-driven />

< / beans>

我的web.xml

 <?xml version =1.0encoding =UTF-8?> 
< web-app version =2.5xmlns =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- app_2_5.xsd>

< display-name> albert simpleWebapp< / display-name>

< context-param>
< param-name> contextConfigLocation< / param-name>
< param-value>
/WEB-INF/applicationContext.xml
/WEB-INF/datasourceContext.xml
< / param-value>
< / context-param>

< listener>
< listener-class> org.springframework.web.context.ContextLoaderListener< / listener-class>
< / listener>

< servlet>
< servlet-name> facesServlet< / servlet-name>
< servlet-class> javax.faces.webapp.FacesServlet< / servlet-class>
< / servlet>
< servlet-mapping>
< servlet-name> facesServlet< / servlet-name>
< url-pattern> / faces / *< / url-pattern>
< / servlet-mapping>
< welcome-file-list>
< welcome-file> faces / index.xhtml< / welcome-file>
< / welcome-file-list>
< context-param>
< param-name> javax.faces.PROJECT_STAGE< / param-name>
< param-value>开发< / param-value>
< / context-param>

< / web-app>

我的faces-config.xml,虽然这里没有任何相关内容..

 <?xml version =1.0encoding =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_0.xsd
version =2.0>

< application>
< locale-config> < / default-locale>中的
< default-locale>
< supported-locale> zh< / supported-locale>
< / locale-config>
< resource-bundle>
< base-name> org.albertkam.common.messages< / base-name>
< var> msgs< / var>
< / resource-bundle>
< resource-bundle>
< base-name> org.albertkam.common.errors< / base-name>
< var> errs< / var>
< / resource-bundle>

<! - < message-bundle> id.co.sofcograha.override_messages< / message-bundle> - >
< / application>

< factory>
< exception-handler-factory> org.albertkam.common.BusinessExceptionHandlerFactory< / exception-handler-factory>
< / factory>
< / faces-config>

我想知道是否有可能做这样的事情,将一个Spring托管bean注入@ManagedBean? / p>

请分享您的经验..



谢谢!



***********添加了一些新的实验结果****************



<刚刚我尝试使用@Named替换@ManagedBean和@Inject来替换@Autowired,但它确实有效,但我还是不明白原因。



所以这里是修改后的托管bean:

  @Named(userBean)
@RequestScoped
公共类LoginBean {
private MstUser entityMstUser;

@Inject
private UserService userService;

@PostConstruct
private void init(){
this.entityMstUser = new MstUser();
}

这很有效,服务对象被注入,尽管userService由Spring管理?这怎么可能?



这里还有一些我在路上添加的东西..



我添加了这个在web.xml中

 < listener> 
< listener-class> org.springframework.web.context.request.RequestContextListener< / listener-class>
< / listener>
< listener>
< listener-class> org.jboss.weld.environment.servlet.Listener< / listener-class>
< / listener>

我还添加了一个空的WEB-INF / beans.xml



这是在faces-config.xml内部
org.springframework.web.jsf.el.SpringBeanFacesELResolver



我有点担心这里使用@Named,因为我有一个糟糕的体验与ViewScoped一起使用它。当我让@ManagedBean能够通过spring框架注入服务对象时,我会感觉更安全。



我想知道你们通常做什么来连接ManagedBean与您的业务服务对象..使用CDI,纯Spring,或他们的组合?

解决方案

faces-config.xml中缺少变量解析器

 < application> 
< variable-resolver>
org.springframework.web.jsf.DelegatingVariableResolver
< / variable-resolver>
< / application>

缺少 web.xml中的RequestContextListener侦听器

 < listener> 
< listener-class>
org.springframework.web.context.request.RequestContextListener
< / listener-class>
< / listener>


I have tested this, trying to inject an service object to a @ManagedBean, but it failed with a nullpointerexception, being the userService is null.

I am currently using Tomcat 7, JSF 2, and here are some of my pom.xml

<properties>
    <java-version>1.6</java-version>
    <org.springframework-version>3.0.3.RELEASE</org.springframework-version>
    <org.hibernate-version>3.6.0.Final</org.hibernate-version>
    ....
</properties>

Here is the exception trace :

javax.faces.el.EvaluationException: java.lang.NullPointerException
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:102)
    at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
    at javax.faces.component.UICommand.broadcast(UICommand.java:315)
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:775)
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1267)
    at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:82)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:312)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:306)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:108)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:558)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:379)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:243)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:259)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:281)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at java.lang.Thread.run(Thread.java:636)
Caused by: java.lang.NullPointerException
    at org.albertkam.ui.LoginBean.login(LoginBean.java:38)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:616)
    at org.apache.el.parser.AstValue.invoke(AstValue.java:262)
    at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:278)
    at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:98)
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
    ... 23 more

Here's my ManagedBean :

@ManagedBean
@RequestScoped
public class LoginBean {
    private MstUser entityMstUser;

    @Autowired
    private UserService userService;

    @PostConstruct
    private void init() {
        this.entityMstUser = new MstUser();
    }

And here's my service bean ..

@Service
public class UserService {

    @Autowired
    private UserDAO userDao;

    public void login(MstUser entityMstUser) {

And here's my applicationContext.xml ..

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
        http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"
    default-autowire="byName">

    <context:component-scan base-package="org.albertkam" />
    <context:annotation-config />
    <tx:annotation-driven />

</beans>

And my web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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-app_2_5.xsd">

    <display-name>albert simpleWebapp</display-name>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/applicationContext.xml
            /WEB-INF/datasourceContext.xml
        </param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>facesServlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>facesServlet</servlet-name>
        <url-pattern>/faces/*</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>faces/index.xhtml</welcome-file>
    </welcome-file-list>
    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>

</web-app>

And my faces-config.xml, although there's really nothing related in here ..

<?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_0.xsd"
    version="2.0">

    <application>
        <locale-config>
            <default-locale>in</default-locale>
            <supported-locale>en</supported-locale>
        </locale-config>
      <resource-bundle>
         <base-name>org.albertkam.common.messages</base-name>
         <var>msgs</var>
      </resource-bundle>
      <resource-bundle>
         <base-name>org.albertkam.common.errors</base-name>
         <var>errs</var>
      </resource-bundle>

      <!-- <message-bundle>id.co.sofcograha.override_messages</message-bundle>  -->
    </application>

    <factory>
        <exception-handler-factory>org.albertkam.common.BusinessExceptionHandlerFactory</exception-handler-factory>
    </factory>
</faces-config>

I wonder if it's possible to do such thing, injecting a Spring managed bean to a @ManagedBean ?

Please share your experience in this ..

Thank you !

*********** added some new experiment results ****************

<listener>
    <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<listener>
    <listener-class>org.jboss.weld.environment.servlet.Listener</listener-class>
</listener>Hello,

I have tested this, trying to inject an service object to a @ManagedBean, but it failed with a nullpointerexception, being the userService is null.

I am currently using Tomcat 7, JSF 2, and here are some of my pom.xml

<properties>
    <java-version>1.6</java-version>
    <org.springframework-version>3.0.3.RELEASE</org.springframework-version>
    <org.hibernate-version>3.6.0.Final</org.hibernate-version>
    ....
</properties>

Here is the exception trace :

javax.faces.el.EvaluationException: java.lang.NullPointerException
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:102)
    at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
    at javax.faces.component.UICommand.broadcast(UICommand.java:315)
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:775)
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1267)
    at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:82)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:312)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:306)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:108)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:558)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:379)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:243)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:259)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:281)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
    at java.lang.Thread.run(Thread.java:636)
Caused by: java.lang.NullPointerException
    at org.albertkam.ui.LoginBean.login(LoginBean.java:38)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:616)
    at org.apache.el.parser.AstValue.invoke(AstValue.java:262)
    at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:278)
    at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:98)
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
    ... 23 more

Here's my ManagedBean :

@ManagedBean
@RequestScoped
public class LoginBean {
    private MstUser entityMstUser;

    @Autowired
    private UserService userService;

    @PostConstruct
    private void init() {
        this.entityMstUser = new MstUser();
    }

And here's my service bean ..

@Service
public class UserService {

    @Autowired
    private UserDAO userDao;

    public void login(MstUser entityMstUser) {

And here's my applicationContext.xml ..

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
        http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"
    default-autowire="byName">

    <context:component-scan base-package="org.albertkam" />
    <context:annotation-config />
    <tx:annotation-driven />

</beans>

And my web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 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-app_2_5.xsd">

    <display-name>albert simpleWebapp</display-name>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/applicationContext.xml
            /WEB-INF/datasourceContext.xml
        </param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>facesServlet</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>facesServlet</servlet-name>
        <url-pattern>/faces/*</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
        <welcome-file>faces/index.xhtml</welcome-file>
    </welcome-file-list>
    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Development</param-value>
    </context-param>

</web-app>

And my faces-config.xml, although there's really nothing related in here ..

<?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_0.xsd"
    version="2.0">

    <application>
        <locale-config>
            <default-locale>in</default-locale>
            <supported-locale>en</supported-locale>
        </locale-config>
      <resource-bundle>
         <base-name>org.albertkam.common.messages</base-name>
         <var>msgs</var>
      </resource-bundle>
      <resource-bundle>
         <base-name>org.albertkam.common.errors</base-name>
         <var>errs</var>
      </resource-bundle>

      <!-- <message-bundle>id.co.sofcograha.override_messages</message-bundle>  -->
    </application>

    <factory>
        <exception-handler-factory>org.albertkam.common.BusinessExceptionHandlerFactory</exception-handler-factory>
    </factory>
</faces-config>

I wonder if it's possible to do such thing, injecting a Spring managed bean to a @ManagedBean ?

Please share your experience in this ..

Thank you !

*********** added some new experiment results ****************

Just now i tried using @Named to replace @ManagedBean and @Inject to replace @Autowired, and it worked, although i still dont really understand the reason why.

So here's the modifed managed bean :

@Named("userBean")
@RequestScoped
public class LoginBean {
    private MstUser entityMstUser;

    @Inject
    private UserService userService;

    @PostConstruct
    private void init() {
        this.entityMstUser = new MstUser();
    }

This works, the service object got injected, although the userService is managed by Spring ? How is that possible ?

Here's some additional things that i add along the way ..

I added this in the web.xml

<listener>
    <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<listener>
    <listener-class>org.jboss.weld.environment.servlet.Listener</listener-class>
</listener>

I also added an empty WEB-INF/beans.xml

And this inside the faces-config.xml org.springframework.web.jsf.el.SpringBeanFacesELResolver

Im a bit concerned here with using the @Named, because i've had a bad experience using it along with ViewScoped. I'd feel alot safer when i have @ManagedBean being able to be injected with service objects by the spring framework.

I wonder what you guys usually do to connect the ManagedBean with your business service objects .. using CDI, or pure Spring, or the combination of them ?

解决方案

missing variable resolver in faces-config.xml

<application>  
  <variable-resolver>  
    org.springframework.web.jsf.DelegatingVariableResolver 
  </variable-resolver>  
 </application> 

missing RequestContextListener listener in web.xml

<listener> 
  <listener-class>
    org.springframework.web.context.request.RequestContextListener
  </listener-class>  
</listener>  

这篇关于JSF2:从Spring向managedbean注入服务对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆