【发布时间】:2016-09-12 20:12:01
【问题描述】:
我有以下单选按钮可选择的数据表:
<h:form>
<p:dataTable value="#{gerarDocumentoBacking.reclamantes}"
selection="#{gerarDocumentoBacking.reclamante}" var="re" rowKey="#{re.id}">
<p:column headerText="Id">
<h:outputText value="#{re.id}"/>
</p:column>
<p:column headerText="Relamantes">
<h:outputText value="#{re.nome}"/>
</p:column>
<p:column selectionMode="single"/>
</p:dataTable>
<p:commandButton value="Gerar" action="#{gerarDocumentoBacking.gerar}"/>
</h:form>
它按预期显示所有行和单选按钮。但是,它没有设置reclamante 变量,正如selection= 属性中指定的那样。
setReclamante 方法可用且公开:
@Named
@ViewScoped
public class GerarDocumentoBacking implements Serializable {
private static final long serialVersionUID = 1L;
private List<Reclamante> reclamantes;
private Reclamante reclamante;
@EJB
private ReclamanteService reclamanteService;
@PostConstruct
public void init() {
reclamantes = reclamanteService.listar();
}
public String gerar() {
System.out.println(reclamante.getNome());
return null;
}
public Reclamante getReclamante() {
return reclamante;
}
public void setReclamante(Reclamante reclamante) {
this.reclamante = reclamante;
}
...
使用一些 IDE 调试,我可以看到 setReclamante 方法以空值调用,因此我的 gerar 方法在 System.out.println(reclamante.getNome()); 行抛出 NullPointerException。我的数据表有 rowKey 属性,我知道它是有效的,因为它显示在数据表中。
我需要数据表外的命令按钮,因为我打算在 h:form 内使用其他数据表。
【问题讨论】:
标签: java jsf primefaces datatable