我的JSF应用程序的行为异常,我请同事帮助我确定解决方案。
应用程序通过Facade + DAO类从数据库中获取数据,并通过debug和println可以声明对象集合是正确的(在下面的示例中,该集合包含5个对象及其属性),但是,当传递此集合到Primefaces页面,dataTable不显示属性,很明显,行数正确,但未显示该属性,如图所示。
我研究了其他帖子,但所描述的错误与我的错误不同:
after filtering Empty rows blank rows displayed while paging in the datatable using Primefaces
由于托管bean正确地重新发布了集合,所以我认为该问题应该在显示器上显示(即,在JSF页面上),并尝试查找错误所在,因此我创建了一个页面,而没有使用Primefaces或Facelets,纯JSF组件,但故障仍然存在。基本代码如下:
以下是代码片段:
简单页面
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html">
<h:head>
<link href="scripts/teste.css" rel="stylesheet" type="text/css" media="all" />
</h:head>
<h:body>
<h:form>
<h:dataTable value="#{coletaMB.coletas}" var="coleta"
styleClass="order-table"
headerClass="order-table-header"
rowClasses="order-table-odd-row,order-table-even-row">
<h:column>
<f:facet name="header">Nr. Setor</f:facet>
<h:outputText value="#{coleta.setor.numero}"/>
---- #{coleta.setor.numero} ----
</h:column>
</h:dataTable>
</h:form>
使用此简单代码,页面如下所示:
托管bean
@ManagedBean(name="coletaMB")
@SessionScoped
public class ColetaMB{
@ManagedProperty(name="coleta", value="#{coleta}")
private Coleta coleta;
@ManagedProperty(name="coletaFacade", value="#{coletaFacade}")
private ColetaFacade coletaFacade;
private List<Coleta> coletas;
public List<Coleta> getColetas(){
if(coletas == null){
coletas = getListColetas();
}
return coletas;
}
private List<Coleta> getListColetas(){
coletas = new ArrayList<Coleta>();
try {
coletas = coletaFacade.getColetas();
return coletas;
} catch (DAOException e) {
(...)
}
}
(...)
}
Coleta.java
public class Coleta {
private int ano;
private Setor setor;
private int mes;
private int semana;
private int numeroEntrevista;
(*)getters and setter
}
Setor.java
public class Setor {
private Agencia agencia;
private String numero;
private String upa;
(*)getters and setters
}
Agencia.java
public class Agencia {
private int idAgencia;
private String nome;
(*)getters and setters
}
门面
public List<Coleta> getColetas() throws DAOException {
return dao.getColetas();
}
DAO
@Value("#{queries.sql01}")
private String sql01;
public List<Coleta> getColetas() throws DAOException {
try{
RowMapper<Coleta> mapper = getRowMapper();
return getJdbcTemplate().query(sql01, mapper);
} catch (DataAccessException de) {
de.printStackTrace();
throw new DAOException(de.getMessage());
}
}
private RowMapper<Coleta> getRowMapper() {
return new RowMapper<Coleta>() {
public Coleta mapRow(ResultSet rs, int rowNum) throws SQLException {
Agencia ag = new Agencia();
ag.setIdAgencia(rs.getInt(1));
ag.setNome(rs.getString(2));
Setor s = new Setor();
s.setAgencia(ag);
s.setUpa(rs.getString(3));
s.setNumero(rs.getString(4));
Coleta c = new Coleta();
c.setSetor(s);
c.setAno(rs.getInt(5));
c.setMes(rs.getInt(6));
c.setSemana(rs.getInt(7));
c.setNumeroEntrevista(rs.getInt(8));
return c;
}
};
}
在getListColetas
中,我插入了一个println来验证集合是否完整,也就是说,每个对象“ coleta”都有一个对象“ setor”,每个“ setor”都有一个对象“ agencia”。但是,遵循在JSF页面上使用“空”的建议,
<h:outputText value="#{empty coleta} - #{empty coleta.setor} - #{empty coleta.setor.numero}"/>
返回是false - true - true
,我不知道为什么。
我的完整应用程序正在使用以下库和依赖项(Spring仅用于DI和DAO类):
Resolved:在dataTable标记中,我将属性var="coleta"
更改为var="c"
,如下所示:
<h:dataTable value="#{coletaMB.coletas}" var="c"
styleClass="order-table"
headerClass="order-table-header"
rowClasses="order-table-odd-row,order-table-even-row">
<h:column>
<f:facet name="header">Nr. Setor</f:facet>
<h:outputText value="#{c.setor.numero}"/>
---- #{c.setor.numero} ----
</h:column>
</h:dataTable>
[我想JSF与ColetaMB中的@ManagedProperty
'coleta'有冲突,尽管我知道var属性特定于传递给dataTable的各种集合对象。