2013-04-21 74 views
0

我有使用jsf 2.0和spring 3.0 + hibernate 4的web應用程序問題是:jsf託管的bean不能使用依賴注入的spring bean有我的配置文件:錯誤集成jsf + spring + hibernate + managedbean

的Rq當我更改爲myBean像@ManagedBean(name = 「userMB」) @RequestScoped 我把@ManagedProperty(值= 「#{}服務」)是公認的JSF頁面上,但有錯誤

package com.ardia.service; 
import java.util.List; 
import com.ardia.model.Composant; 

public interface ComposantService { 

    void inserComposant(Composant comp); 

    Composant getComposantById(int compId); 

    Composant getComposant(String compname); 

    List<Composant> getComposants(); 

} 

這是我的服務

package com.ardia.service; 
import java.util.List; 
import org.hibernate.Criteria; 
import org.hibernate.SessionFactory; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Service; 
import org.springframework.transaction.annotation.Transactional; 
import com.ardia.model.Composant; 

@Service("compService") 
@Transactional 
public class ComposantImp implements ComposantService { 


    @Autowired 
    SessionFactory sessionFactory; 


    @Override 
    public void inserComposant(Composant comp) { 
     // TODO Auto-generated method stub 

     sessionFactory.getCurrentSession().saveOrUpdate(comp); 
    } 

    @Override 
    public Composant getComposantById(int compId) { 
     // TODO Auto-generated method stub 
     return (Composant) sessionFactory. 
        getCurrentSession(). 
        get(Composant.class, compId); 
    } 

    @Override 
    public Composant getComposant(String compname) { 
     // TODO Auto-generated method stub 
     return null; 
    } 

    @Override 
    @SuppressWarnings("unchecked") 
    public List<Composant> getComposants() { 
     // TODO Auto-generated method stub 
     Criteria criteria = sessionFactory. 
        getCurrentSession(). 
        createCriteria(Composant.class); 
       return criteria.list(); 
    } 

} 

這是我的Managed Bean的

package ardia.beans; 
import java.io.Serializable; 
import java.util.List; 
import javax.annotation.PostConstruct; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.context.annotation.Scope; 
import org.springframework.stereotype.Component; 
import com.ardia.model.Composant; 
import com.ardia.service.ComposantService; 

@Component("compBean") 
@Scope("session") 
public class CoposantBean implements Serializable{ 

@Autowired 
private ComposantService service; 

private List<Composant> list; 

@PostConstruct 
public void init() 
{ 
    list=service.getComposants(); 

} 

public List<Composant> getList() { 
    return list; 
} 

public void setList(List<Composant> list) { 
    this.list = list; 
} 
} 

這是我的應用程序上下文春天

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


    <context:component-scan 
    base-package="ardia.beans" /> 
    <context:component-scan 
    base-package="com.ardia.service" /> 
<!-- Data Source Declaration --> 
    <bean id="DataSource" class="org.apache.commons.dbcp.BasicDataSource" > 
     <property name="driverClassName" value="org.postgresql.Driver" /> 
     <property name="url" value="jdbc:postgresql:PFE" /> 
     <property name="username" value="postgres" /> 
     <property name="password" value="root" /> 

    </bean> 

    <!-- Session Factory Declaration --> 
    <bean id="SessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
     <property name="dataSource" ref="DataSource" /> 
     <property name="packagesToScan" value="com.ardia.model" /> 
     <property name="hibernateProperties"> 
      <props> 
       <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop> 
       <prop key="hibernate.show_sql">true</prop> 
      </props> 
     </property> 


    </bean> 

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

<bean id="transactionManager" 
    class="org.springframework.orm.hibernate4.HibernateTransactionManager" > 
    <property name="sessionFactory" ref="SessionFactory"/> 
    </bean> 


</beans> 

這個web.xml和facec.conf.xml

<?xml version="1.0" encoding="UTF-8"?> 
<faces-config version="2.0" xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:xi="http://www.w3.org/2001/XInclude" 
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"> 
<application> 
    <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver> 
</application> 
</faces-config> 

面孔配置

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_3_0.xsd"> 
<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>classpath:application.xml</param-value> 
</context-param> 
<context-param> 
    <description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description> 
    <param-name>javax.faces.STATE_SAVING_METHOD</param-name> 
    <param-value>default</param-value> 
</context-param> 
<context-param> 
    <param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name> 
    <param-value>resources.application</param-value> 
</context-param> 
<listener> 
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
</listener> 
<listener> 
    <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class> 
</listener> 
<listener> 
    <listener-class>com.sun.faces.config.ConfigureListener</listener-class> 
</listener> 
<servlet> 
    <servlet-name>Faces Servlet</servlet-name> 
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> 
    <load-on-startup>1</load-on-startup> 
</servlet> 
<servlet-mapping> 
    <servlet-name>Faces Servlet</servlet-name> 
    <url-pattern>*.jsf</url-pattern> 
</servlet-mapping> 
<welcome-file-list> 
    <welcome-file>/index.jsp</welcome-file> 
</welcome-file-list> 
</web-app> 

,當我努力與數據表.the列表managedbean它不被識別

<p:dataTable id="cars" var="fab" value=***"#{here is not figure*** paginator="true" rows="10" > 

回答

1

持有春天立體聲輸入豆(這裏你支持Bean都拿着他們的)類應該通過Spring被實例化工廠,但支持bean是由JSF框架創建的。只需使用Spring EL解析器來解決這個問題。他是一個有關discussiondocumentation

此外,請確保您通過web.xml中

<!-- Spring Bootstrap --> 
<listener> 
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
</listener> 
<context-param> 
    <param-name>contextClass</param-name> 
    <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value> 
</context-param> 
<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value> 
     package1.where.springbeans.exist 
     package2.where.springbeans.exist 
    </param-value> 
</context-param> 
+0

THX在Web應用程序上下文加載結構的彈簧引導;但我使用我的facec.conf解析.xml !!!我沒有把你停下來.. – FERESSS 2013-04-21 08:47:24

+1

是啊,那就是你應該使用EL解析器的地方。你還在發現問題嗎? – maggu 2013-04-21 08:49:43

+1

編輯我的答案,以包含基於Web上下文的彈簧引導 – maggu 2013-04-21 08:52:38