我在jsf中使用带有primefaces的datalist,我面临问题,所选对象的问题支持bean类字段没有更新到选定的datalist行,它总是给出null对象。这是代码
查看文件:
`<?xml version="1.0"?>`
<f:view xmlns="http://www.w3.org/1999/xhtml"
xmlns:c="http://java.sun.com/jsp/jstl/core"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui"
xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head />
<h:body>
<h:form>
<p:dataList value="#{tableBean.getList()}" var="job" id="cars"
paginator="true" rows="5" effectSpeed="fast"
paginatorTemplate="{PreviousPageLink} {CurrentPageReport} {NextPageLink} {RowsPerPageDropdown}"
rowsPerPageTemplate="5" type="none">
<f:facet name="header">
Applicants List
</f:facet>
<p:column>
<div style="width: 200px">
<h:outputText value="Name:" style="margin-left:10px" />
<h:outputText value="#{job.name}" style="margin-left:10px" />
<br />
<h:outputText value="Applied for:" style="margin-left:10px" />
<h:outputText value="#{job.jobtitle}" style="margin-left:10px" />
<br />
<h:outputText value="Degree:" style="margin-left:10px" />
<h:outputText value="#{job.degree}" style="margin-left:10px" />
<br />
<h:outputText value="Experience:" style="margin-left:10px" />
<h:outputText value="#{job.experience}" style="margin-left:10px" />
<br />
<h:outputText value="Contact:" style="margin-left:10px" />
<h:outputText value="#{job.contact}" style="margin-left:10px" />
<br />
<h:commandLink value="Call for Interview" style="margin-left:10px"></h:commandLink>
</div>
<br/>
<p:commandButton icon="ui-icon-search"
actionListener="#{tableBean.printView}" title="View Detail">
<f:setPropertyActionListener value="#{job}"
target="#{tableBean.selectedJob}" />
</p:commandButton>
<p:separator id="separator" />
</p:column>
</p:dataList>
</h:form>
</h:body>
</f:view>
表bean类:
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.bean.ViewScoped;
import javax.faces.event.ActionEvent;
import DAO.JobApplicationDao;
import DTO.JobApplicationDto;
@ManagedBean
@ViewScoped
public class TableBean implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
public JobApplicationDto selectedJob;
public TableBean() {
}
public List<JobApplicationDto> getList() {
List<JobApplicationDto> joblist = new ArrayList<JobApplicationDto>();
JobApplicationDao dao = new JobApplicationDao();
joblist =dao.getDroppedCv();
return joblist;
}
public JobApplicationDto getSelectedJob() {
return selectedJob;
}
public void setSelectedJob(JobApplicationDto selectedJob) {
this.selectedJob = selectedJob;
}
public void printView(ActionEvent event) {
try{
System.out.print("Selected Person "+getSelectedJob.getName());
}catch(Exception e){
}
}
}
答案 0 :(得分:1)
您应该将setPropertyActionListener与action一起使用。在调用操作之前调用侦听器。似乎在actionListener之后调用了setPropertyActionListener。
将actionListener="#{tableBean.printView}"
更改为action="#{tableBean.printView}"
和方法
public void printView() {
try{
System.out.print("Selected Person "+getSelectedJob.getName());
}catch(Exception e){
}
}
请参阅此内容以更好地了解使用内容,
http://balusc.blogspot.com/2006/06/communication-in-jsf.html
答案 1 :(得分:-1)
您可以像这样使用数据表选择
<p:dataList value="#{tableBean.getList()}" selection="#{tableBean.selectedJob}" var="job" id="cars"
paginator="true" rows="5" effectSpeed="fast"
paginatorTemplate="{PreviousPageLink} {CurrentPageReport} {NextPageLink} {RowsPerPageDropdown}"
rowsPerPageTemplate="5" type="none">