2014-02-08 54 views
0

在我的xhtml文件中,我必須將p:dialog的employeeId和employeeName值傳遞給jasper report.I已創建jrxml(文件),字段爲$ F {employeeId}和$ F {employementName}。當我選擇命令按鈕時,它必須將employeeId和employeeName傳遞給碧玉報告並生成爲.pdf.But但我不知道如何在JRBeanCollectionDataSource中傳遞employeeId和employeeName。如何將bean值傳遞給碧玉報告

 <p:dialog header="Employee Details" widgetVar="actDialog" appendToBody="false" 
       resizable="false" showEffect="explode" hideEffect="explode"> 

       <h:panelGrid id="dialog" columns="2" cellpadding="4"> 

        <h:outputText value="Employee Id:" /> 
        <h:outputText 
         value="#{employeeBean.current.employeeId}" 
         style="font-weight:bold" /> 

        <h:outputText value="Employee Name:" /> 
        <h:outputText 
         value="#{employeeBean.current.employeeName}" 
         style="font-weight:bold" /> 

       </h:panelGrid> 
      <p:commandButton value="Report" ajax="false" icon="ui-icon-check" style="width:70px;" action="#{employeeBean.generateReport}"/> 
      </p:dialog> 

豆:

private List<EmployementScheduleDTO> userList; 
    private EmployementScheduleDTO current; 

    public List<EmployementScheduleDTO> getUserList() { 
    return userList; 
    } 

    public void setUserList(List<EmployementScheduleDTO> userList) { 
    this.userList = userList; 
    } 

    public EmployementScheduleDTO getCurrent() { 
    return current; 
    } 

    public void setCurrent(EmployementScheduleDTO current) { 
    if (current != null) { 
     this.current = current; 
    } 
    } 

    public void generateReport() 
    { 
    reportGeneration(current, userList); 
    } 


    public void reportGeneration(EmployementScheduleDTO employeedto,List<EmployementScheduleDTO> a_beanList) 
    { 
    try 
     { 
      URL url = getClass().getClassLoader().getResource("/WEB-INF/reports/employee.jrxml"); 
      JasperReport jasperReport = JasperCompileManager.compileReport(url.getPath()); 
      JRBeanCollectionDataSource jrDataSource = 
      new JRBeanCollectionDataSource(a_beanList); 
      JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, null, jrDataSource); 

     } 
     catch (JRException jre) 
     { 
      jre.printStackTrace(); 
     } 
    catch(UnsupportedEncodingException e) 
    { 
     e.printStackTrace(); 
    } 

    } 
+0

假設當前' '持有'userList'成員的引用,我不明白爲什麼這不起作用。你能否提供實際和預期產出的例子? – GenericJon

回答

0

最後我發現從豆答案通過值碧玉report.Thank你爲那些誰迴應我...

public EmployeeBean report(String empid,String empname) 
{ 
    EmployeeBean empbean=new EmployeeBean(); 
    empbean.setEmployeeId(empid); 
    empbean.setEmployeeName(empname); 
    return empbean; 
} 

public void init() throws JRException 
{ 

    String employeeid=current.getEmployeeId(); 
    String employeename=current.getEmployeeName(); 
    ArrayList<EmployeeBean> databeanlist=new ArrayList<EmployeeBean>(); 
    databeanlist.add(report(employeeid, employeename)); 
    JRBeanCollectionDataSource beancollection=new JRBeanCollectionDataSource(databeanlist); 
    String realpath=FacesContext.getCurrentInstance().getExternalContext().getRealPath("common/reports/jasperreport.jasper"); 
    jasperprint=JasperFillManager.fillReport(realpath, new HashMap(),beancollection); 
} 

public void generateReport() throws JRException, IOException 
{ 
    logger.info("enter into generate report"); 
    init(); 
    HttpServletResponse httpservlet=(HttpServletResponse)FacesContext.getCurrentInstance().getExternalContext().getResponse(); 
    httpservlet.addHeader("Content-disposition", "attachment;filename=schedule.pdf"); 
    ServletOutputStream servletout=httpservlet.getOutputStream(); 
    JasperExportManager.exportReportToPdfStream(jasperprint, servletout); 
    FacesContext.getCurrentInstance().responseComplete(); 
}