2013-02-28 49 views
0

我在使用jsf中的datalist和primefaces時,遇到問題支持所選對象的bean類字段沒有更新到選定的datalist行,它始終給出空對象。下面是代碼選中的行不在數據列表中選中JSF

視圖文件:

`<?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

首先,你是什麼意思與選擇?點擊'p:commandButton'? – 2013-02-28 21:35:35

回答

-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"> 
+0

'p:dataList'沒有選擇屬性。 – 2013-02-28 21:39:14

+0

upps!我很抱歉,我只是意識到,我認爲它是可數據的,我的錯誤 – 2013-02-28 21:42:52

+0

你可以嘗試添加process =「:form:cars」屬性到commandButton – 2013-02-28 21:46:00

1

你應該使用setPr帶動作的opertyActionListener。在調用動作之前調用監聽器。看起來像setPropertyActionListener在actionListener之後被調用。

變化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

相關問題