如何将spring beans注入xhtml页面

时间:2014-02-17 17:46:31

标签: java spring jsf spring-mvc richfaces

我正在使用Spring,我遇到了bean的问题,这是我的代码:

import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.grh.bo.ICollaborateurBo;
import com.grh.bo.IUserBo;
import com.grh.entities.Collaborateur;

@Component
@Scope("session")
public class CollaborateurBean implements Serializable {
    private static final long serialVersionUID = 1L;

    @Autowired
    private ICollaborateurBo collaborateurBo;

    private String nom;
    private String prenom;
    private String sflag = "";

    private int currentProjectIndex;
    private int page = 1;


    public List<Collaborateur> listOfCollaborateur = new ArrayList<Collaborateur>();

    private Collaborateur Collaborateurr = new Collaborateur();

    List<Collaborateur> lis = new ArrayList<Collaborateur>(collaborateurBo.getAllCollaborateur());

    @PostConstruct
    public void init() {
         setListOfCollaborateur(recupererCollaborateur());
         Collaborateurr=new Collaborateur();
    }

    public List<Collaborateur> getLis() {
        return collaborateurBo.getAllCollaborateur();
    }

    public void setLis(List<Collaborateur> lis) {
        this.lis = lis;
    }

    public List<Collaborateur> getCollaborateurList() {

    return collaborateurBo.getAllCollaborateur();
    }

    private List<Collaborateur> recupererCollaborateur() {
        List<Collaborateur> ps = collaborateurBo.getAllCollaborateur();
        return ps;
    }

    public CollaborateurBean() {
        super();
    }

    public CollaborateurBean(ICollaborateurBo collaborateurBo, String nom,
            String prenom, String sflag, int currentProjectIndex, int page,
            List<Collaborateur> listOfCollaborateur,
            Collaborateur collaborateurr, List<Collaborateur> lis) {
        super();
        this.collaborateurBo = collaborateurBo;
        this.nom = nom;
        this.prenom = prenom;
        this.sflag = sflag;
        this.currentProjectIndex = currentProjectIndex;
        this.page = page;
        this.listOfCollaborateur = listOfCollaborateur;
        Collaborateurr = collaborateurr;
        this.lis = lis;
    }

    public Collaborateur getCollaborateurr() {
        return Collaborateurr;
    }

    public void setCollaborateurr(Collaborateur collaborateurr) {
        Collaborateurr = collaborateurr;
    }
    public List<Collaborateur> getListOfCollaborateur() {
        return listOfCollaborateur;
    }

    public void setListOfCollaborateur(List<Collaborateur> listOfCollaborateur) {
        this.listOfCollaborateur = listOfCollaborateur;
    }

    public String getSflag() {
        return sflag;
    }

    public void setSflag(String sflag) {
        this.sflag = sflag;
    }
    public void setCollaborateurBo(ICollaborateurBo collaborateurBo) {
        this.collaborateurBo = collaborateurBo;
    }
    public ICollaborateurBo getCollaborateurBo() {
        return collaborateurBo;
    }


    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }
    public int getCurrentProjectIndex() {
        return currentProjectIndex;
    }

    public void setCurrentProjectIndex(int currentProjectIndex) {
        this.currentProjectIndex = currentProjectIndex;
    }
    public String getNom() {
        return nom;
    }

    public void setNom(String nom) {
        this.nom = nom;
    }

    public String getPrenom() {
        return prenom;
    }

    public void setPrenom(String prenom) {
        this.prenom = prenom;
    }
}

在此页面collaborateur.xhtml我无法显示来自我的DATABASE collaborateur.xhtml的数据:

<h:dataTable value="#{CollaborateurBean.getLis()}" var="item">
    <h:column>
        <h:outputText value="#{item.nom}" />
    </h:column>

    <h:column>
        <h:outputText value="#{item.prenom}" />
    </h:column>
</h:dataTable>

当我测试collaborateur时,我显示来自我的数据库的数据。这是从数据库提供数据的代码:

<h:dataTable value="#{collaborateurBo.getAllCollaborateur()}" var="item">
    <h:column>
        <h:outputText value="#{item.nom}" />
    </h:column>

    <h:column>
         <h:outputText value="#{item.prenom}" /> 
    </h:column>
</h:dataTable>

这意味着问题出在Collaborateur bean中,而不是在collaborateurBo或CollaborateurDao中。

这是我的配置:

的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_3_0.xsd"
    id="WebApp_ID" version="3.0">
    <display-name>GRH</display-name>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/config/web-application-config.xml
        </param-value>
    </context-param>
    <context-param>
        <param-name>javax.faces.DATETIMECONVERTER_DEFAULT_TIMEZONE_IS_SYSTEM_TIMEZONE</param-name>
        <param-value>true</param-value>
    </context-param>
    <context-param>
        <param-name>javax.faces.DEFAULT_SUFFIX</param-name>
        <param-value>.xhtml</param-value>
    </context-param>
    <context-param>
        <param-name>facelets.SKIP_COMMENTS</param-name>
        <param-value>true</param-value>
    </context-param>
    <context-param>
        <param-name>javax.faces.PROJECT_STAGE</param-name>
        <param-value>Production</param-value>
    </context-param>
    <context-param>
        <param-name>javax.faces.FACELETS_REFRESH_PERIOD</param-name>
        <param-value>1</param-value>
    </context-param> 
    <context-param>
        <param-name>javax.faces.FACELETS_LIBRARIES</param-name>
        <param-value>/WEB-INF/springsecurity.taglib.xml</param-value>
    </context-param>

    <filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>
    <filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
    </filter-mapping>

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

    <servlet>
        <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/config/web-application-config.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
        <url-pattern>/spring/*</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>spring/main-flow</welcome-file>
    </welcome-file-list>
    <error-page>
        <error-code>403</error-code>
        <location>/spring/acces-interdit-flow</location>
    </error-page>
    <context-param>
        <param-name>org.richfaces.skin</param-name>
        <param-value>ruby</param-value>
    </context-param>
</web-app>

面-配置:

<?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>
        <el-resolver>org.springframework.web.jsf.el.SpringBeanFacesELResolver</el-resolver>
        <resource-bundle>
            <base-name>com.grh.prop.messages</base-name>
            <var>msg</var>
        </resource-bundle>
    </application>

    <managed-bean>
        <managed-bean-name>currentDate</managed-bean-name>
        <managed-bean-class>java.util.Date</managed-bean-class>
        <managed-bean-scope>request</managed-bean-scope>
    </managed-bean>
</faces-config>

web的应用程序-config.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:context="http://www.springframework.org/schema/context"
    xmlns:jms="http://www.springframework.org/schema/jms" xmlns:jee="http://www.springframework.org/schema/jee"
    xsi:schemaLocation="
           http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context.xsd
           http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms.xsd
           http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd">

    <!-- Scans for application @Components to deploy -->
    <context:component-scan base-package="org.springframework.webflow.samples.booking" />

    <context:component-scan base-package="com.grh" />



    <!-- Database Configuration -->
    <import resource="DataSource.xml" />
    <import resource="Hibernate.xml" />

    <!-- Spring Configuration -->
    <import resource="webmvc-config.xml" />
    <import resource="webflow-config.xml" />

    <!-- Spring security 3.0.5 -->
    <import resource="security-config.xml" />


</beans>

请提前帮助我,

0 个答案:

没有答案