我正在使用Spring,Hibernate和JSf创建一个使用Netbeans的Web应用程序。当我运行我的应用程序时,我没有错误,应用程序已部署但在我的JSF中没有出现列表
我不知道自己要做什么 请帮助我,并提前致谢
我的listUniv.xhtml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui"
xmlns:fn="http://java.sun.com/jsp/jstl/functions">
<h:head/>
<h:body>
<h1>Liste des universités</h1>
<h:form id="mainForm">
<h:outputText value="#{fn:length(universiteBean.listUniversites)}" />
<p:dataList id="lista" var="Universite1" value="#{universiteBean.listUniversites}" emptyMessage="aucune universite trouvée!">
<li><h:outputText value="#{Universite1.libelle}" />
</li>
</p:dataList>
<p:commandButton value="ajouter" action="#{universiteBean.nouveau()}"/>
</h:form>
</h:body>
</html>
我的web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<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>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>listUniv.jsf</welcome-file>
</welcome-file-list>
<listener>
<listener-class>org.springframework.security.web.session.HttpSessionEventPublisher</listener-class>
</listener>
<!-- 1ere -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 2 eme -->
<context-param>
<param-name>contextConfigLocation</param-name>
<!-- <param-value>/WEB-INF/spring-beans.xml</param-value>-->
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
</web-app>
我的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:tx="http://www.springframework.org/schema/tx"
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/tx
http://www.springframework.org/schema/tx/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-beans-3.1.xsd">
<!-- BEAN DE LA DATA SOURCE -->
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/formadb?zeroDateTimeBehavior=convertToNull" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>
<!-- BEAN DE LA FABRIQUE DE SPRING avec definition du mapping-->
<bean id="sessionFactoryBean" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" >
<value>classpath:hibernate.cfg.xml</value>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<!-- Ajout d'un bean de gestion des transactions -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<!-- Lui définir le sessionFactory qui devra exploiter -->
<property name="sessionFactory" ref="sessionFactoryBean"/>
</bean>
<!-- BEAN UNIVERSITE -->
<bean id="universiteDao" class="dao.UniversiteDaoImpl">
<property name="sessionFactory">
<ref local="sessionFactoryBean"/>
</property>
</bean>
<bean id="universiteMetier" class="metier.UniversiteMetier" init-method="init">
<property name="universiteDao" ref="universiteDao"/>
</bean>
<!-- BEAN COMPOSANTE-->
<bean id="composanteDao" class="dao.ComposanteDaoImpl">
<property name="sessionFactory">
<ref local="sessionFactoryBean"/>
</property>
</bean>
<bean id="composanteMetier" class="metier.ComposanteMetier">
<property name="composanteDao" ref="composanteDao"/>
</bean>
我在hibernate.cfg.xml中使用了映射类
我的Dao课程:
package dao;
import entites.Universite;
import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
public class UniversiteDaoImpl extends HibernateDaoSupport implements IUniversiteDao{
@Override
public void addUniversite(Universite universite) {
getHibernateTemplate().save(universite);
}
@Override
public void updateUniversite(Universite univ) {
getHibernateTemplate().update(univ);
}
@Override
public void deleteUniversite(Universite univ) {
getHibernateTemplate().delete(univ);
}
@Override
public List findListUniversite() {
return getHibernateTemplate().find("from entites.Universite");
}
@Override
public Universite findUniversite(String libelle) {
List l= getHibernateTemplate().find("from entites.Universite univ where univ.libelle='"+libelle+"'");
if (l.size()> 0) {
return (Universite) l.get(0);
} else
return null;
}
public void init() {
System.out.println("-------UniversiteDaoImp-----------");
}
}
我的班级服务
package metier;
import dao.IUniversiteDao;
import entites.Universite;
import exceptions.ObjtNotFoundException;
import java.util.List;
public class UniversiteMetier implements IUniversiteMetier{
private IUniversiteDao universiteDao;
public void setUniversiteDao(IUniversiteDao universiteDao) {
this.universiteDao = universiteDao;
}
@Override
public void addUniversite(String libelle, String abreviation, String description) {
Universite univ= new Universite(libelle, abreviation, description);
universiteDao.addUniversite(univ);
}
@Override
public void updateUniversite(String libelle, String abreviation, String description) throws ObjtNotFoundException {
Universite univ= universiteDao.findUniversite(libelle);
if (univ== null) {
throw new ObjtNotFoundException();
}
univ.setLibelle(libelle);
universiteDao.updateUniversite(univ);
}
@Override
public List findListUniversite() {
return universiteDao.findListUniversite();
}
@Override
public Universite findUniversite(String libelle) throws ObjtNotFoundException {
Universite univ= universiteDao.findUniversite(libelle);
if (univ== null) {
System.out.println("universite introuvable!!!");
throw new ObjtNotFoundException();
}
return univ;
}
@Override
public void deleteUniversite(String libelle) throws ObjtNotFoundException {
Universite univ= universiteDao.findUniversite(libelle);
if (univ== null) {
throw new ObjtNotFoundException();
}
universiteDao.deleteUniversite(univ);
}
public void init(){
System.out.println("-------UniversiteMetierImp-----------");
}
}
和我的Bean
package web;
import entites.Universite;
import java.io.Serializable;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;
import metier.UniversiteMetier;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component("universiteBean")
//@Scope("session")
@ViewScoped
public class UniversiteBean implements Serializable{
private List<Universite> listUniversites;
@Autowired
private transient UniversiteMetier universiteMetier;
private Universite universite;
public UniversiteBean() {
}
@PostConstruct
public void init(){
refresh();
System.out.println("eeeeeeeeeeeeee"+listUniversites.size());
}
public void refresh(){
listUniversites= universiteMetier.findListUniversite();
}
public List<Universite> getListUniversites() {
return listUniversites;
}
public void setListUniversites(List<Universite> listUniversites) {
this.listUniversites = listUniversites;
}
public UniversiteMetier getUniversiteMetier() {
return universiteMetier;
}
public void setUniversiteMetier(UniversiteMetier universiteMetier) {
this.universiteMetier = universiteMetier;
}
public Universite getUniversite() {
return universite;
}
public void setUniversite(Universite universite) {
this.universite = universite;
}
}
答案 0 :(得分:-1)
更新:请将此代码缩小到最小,可验证的示例。您应该放入一些System.out.println调试语句,以查看事情是否正常运行。查看PostConstruct
是否被调用。添加调试语句以查明PostConstruct
是否将项目放入列表并更新问题。如果未调用PostConstruct
例程,则JSF
页面bean注释错误或引用不正确。将硬编码的项目放入列表中,并确定是否正确设置了primefaces datalist。这些是经过尝试的真正的调试技术,每个程序员都需要理解和掌握。
编辑:我很确定你不希望你将bean作为Component
。这意味着您正在构建一个独特的JSF组件。试试ManagedBean
。