2011-02-06 100 views
2

我有web應用程序至極使用JSF 2.0和3.0春天的 問題是:使用依賴注入 有我的配​​置文件JSF管理bean不能用春豆:問題用的Spring bean從JSF管理的bean

web.xml中:

<web-app> 
<display-name>Archetype Created Web Application</display-name> 

<!-- Faces Servlet --> 
<servlet> 
    <servlet-name>Faces Servlet</servlet-name> 
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> 
    <load-on-startup> 1 </load-on-startup> 
</servlet> 

<!-- Faces Servlet Mapping --> 
<servlet-mapping> 
    <servlet-name>Faces Servlet</servlet-name> 
    <url-pattern>/calc/*</url-pattern> 
</servlet-mapping> 


<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>/WEB-INF/spring-beans.xml</param-value> 
</context-param> 

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

faces-config.xml中:

<application> 
    <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver> 
</application> 


<managed-bean> 
    <managed-bean-name>CalcBean</managed-bean-name> 
    <managed-bean-class>timur.org.bean.CalculatorConroller</managed-bean-class> 
    <managed-bean-scope>request</managed-bean-scope> 
    <managed-property> 
     <property-name>hibernateUtil</property-name> 
     <value>#{hibernateUtil}</value> 
    </managed-property> 
</managed-bean> 

彈簧的beans.xml:

<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:tx="http://www.springframework.org/schema/tx" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:lang="http://www.springframework.org/schema/lang" 
    xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation=" 
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd 
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd 
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd 
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd 
    http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-2.5.xsd"> 


<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> 
    <property name="driverClassName" value="org.postgresql.Driver"/> 
    <property name="url" value="jdbc:postgresql://localhost/timur"/> 
    <property name="username" value="postgres"/> 
    <property name="password" value="postgres"/> 
</bean> 

<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 
    <property name="dataSource" ref="dataSource"/> 
</bean> 

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 
    <property name="dataSource" ref="dataSource"/> 
    <property name="mappingResources"> 
     <list> 
      <value>/mapping/service.xml</value> 
      <value>/mapping/city.xml</value> 
      <value>/mapping/timurovec.xml</value> 
      <value>/mapping/client.xml</value> 
      <value>/mapping/calendar.xml</value> 
      <value>/mapping/order.xml</value> 
     </list> 
    </property> 
    <property name="hibernateProperties"> 
     <props> 
      <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop> 
      <prop key="show_sql">true</prop> 
      <prop key="hibernate.hbm2ddl.auto">create</prop> 
     </props> 
    </property> 

</bean> 

<bean id="hibernateUtil" class="timur.org.util.HibernateUtil"> 
    <property name="sessionFactory" ref="sessionFactory"></property> 
</bean> 

<tx:annotation-driven transaction-manager="txManager"></tx:annotation-driven> 

CalculateController:

public class CalculatorConroller { 

private HibernateUtil hibernateUtil; 


public void setHibernateUtil(HibernateUtil hibernateUtil) { 
    this.hibernateUtil = hibernateUtil; 
} 

public String action() { 

    hibernateUtil.createAndStoreEvent("",new Date()); 
    List<CityDomain> lc = hibernateUtil.getList(); 
    for (int i=0; i<lc.size(); i++){ 
     LogManager.getLogger(this.getClass()).debug(lc.get(i).getName()); 
    } 

    return "success"; 
} 

}

當我運行我的Web應用程序,並調用我的控制器動作變量 '的HibernateUtil' 是null並且沒有例外。但我可以得到春豆使用:

 hibernateUtil = (HibernateUtil) FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance()).getBean("hibernateUtil"); 

我怎樣才能得到這個春天豆使用依賴注入?

回答

1

爲什麼你不在spring-beans.xml中聲明你的面孔管理bean?

也許你還需要在你的臉上,配置補充一點:

<application> 
    <view-handler>com.sun.facelets.FaceletViewHandler</view-handler> 
    <variable-resolver>org.springframework.web.jsf.DelegatingVariableResolver</variable-resolver> 
</application> 

我不是百分之百肯定這雖然是因爲我現在在我的工作站我不是。但我認爲它必須像上面那樣。

那麼你只需將你的「CalcBean」移動到spring-beans.xml並像以前一樣使用它。

0

你缺少的是JSF依賴注入瞭解Spring及其bean的方法。只需將其添加到您的faces-config.xml

<application> 
    <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver> 
</application> 
+0

他已經有了這一個。 – 2011-02-07 10:00:11

0

非常感謝您的回答。 這是我的問題的解決方案: 我改變行:

<el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver> 

到:

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

,但它不一定是加CalculatorBean春天-beans.xml文件 感謝