Primefaces 6 - 数据表未排序+ pdf导出器无法正常工作

时间:2016-12-15 07:46:59

标签: primefaces jsf-2 datatable itext

我使用的是JSF 2.2,Tomcat 7,primefaces 6.0和iText 2.1.7。 我对初学者很陌生,我有两个问题:

  1. 尝试对数据进行排序
  2. 将数据表导出为pdf
  3. 每次用户在日历上进行选择时,都会重新加载表格。 我的代码是:

    的index.xhtml:

     <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
     <html xmlns="http://www.w3.org/1999/xhtml"
        xmlns:ui="http://java.sun.com/jsf/facelets"
        xmlns:h="http://java.sun.com/jsf/html"
        xmlns:f="http://java.sun.com/jsf/core"
        xmlns:c="http://java.sun.com/jsp/jstl/core"
        xmlns:p="http://primefaces.org/ui">
    <h:head>
    </h:head>
    <h:body>
        <h:form id="form">
        <p:layout fullPage="true">
            <p:layoutUnit id="up" position="north" size="130" header="" resizable="true" closable="false" collapsible="true">
                <h:panelGrid columns="4" cellpadding="5">
                    <p:row>
                        <h:graphicImage value="resources/images/logo_anamet.png" />         
                    </p:row>
                </h:panelGrid>  
            </p:layoutUnit>
            <p:layoutUnit position="west" size="350" header="" resizable="true" closable="false" collapsible="false">
                <p:growl id="growl" showDetail="true" />
                <p:outputLabel for="inline" value="#{messages.selectDate}" />
                <p:calendar id="inline" value="#{viewCompanyManagedBean.date}" mode="inline" >
                    <p:ajax event="dateSelect" listener="#{viewCompanyManagedBean.onDateSelect}" update="msgs"  />
                    <p:ajax event="dateSelect" listener="#{viewCompanyManagedBean.onDateSelect}" update=":form:shredder"  />
                </p:calendar>   
            </p:layoutUnit>
            <p:layoutUnit position="center">
                <p:growl id="msgs" showDetail="true" />
                <p:tabView id="shredder" >
                    <p:tab title="#{messages.tab1}">
                        <h:panelGrid id="display" columns="3" cellpadding="15">
                            <row> 
                                <p:dataTable var="measurements" id="measure" value="#{viewCompanyManagedBean.measurements}" rendered="#{not empty viewCompanyManagedBean.measurements}" paginatorTemplate="{Exporters}" paginator="true" paginatorPosition="bottom">
                                    <f:facet name="{Exporters}">
                                    <h:commandLink>
                                        <p:graphicImage name="images/pdf.png" width="22"/>
                                        <p:dataExporter id="pdf" type="pdf" target="measure" fileName="#{viewCompanyManagedBean.fileName}" />
                                    </h:commandLink>
                                   </f:facet>
                                    <p:column sortBy="#{measurements.dateTimeField}" sortOrder="DESCENDING">
                                    <f:facet name="header">
                                        <h:outputText value="#{messages.time}" />
                                    </f:facet>
                                    <h:outputText value="#{measurements.dateTimeField}" />
                                    </p:column>
                                    <p:column sortBy="#{measurements.scale.scaleId}" >
                                    <f:facet name="header">
                                        <h:outputText value="#{messages.scale}" />
                                    </f:facet>
                                    <h:outputText value="#{measurements.scale.scaleId}" />
                                    </p:column>
                                    <p:column sortBy="#{measurements.measurement}" >
                                    <f:facet name="header">
                                        <h:outputText value="#{messages.measurement}" />
                                    </f:facet>
                                    <h:outputText value="#{measurements.measurement}" />
                                    </p:column>
                                </p:dataTable>                          
                            </row>
                        </h:panelGrid>
                    </p:tab>                
                    <p:tab title="#{messages.tab2}">
                        <h:panelGrid columns="2" cellpadding="10">
                            <row> 
                                <p:dataTable var="car" value="">
                                    <p:column headerText="#{messages.saptrn}">
                                        <h:outputText value="" />
                                    </p:column>
                                    <p:column headerText="#{messages.scalepics}">   
                                        <p:contentFlow value="#{viewCompanyManagedBean.images}" var="image">
                                            <p:graphicImage value="images/#{image}" styleClass="content" />
                                            <div class="caption">#{image}/div>
                                        </p:contentFlow>
                                    </p:column>
                                </p:dataTable> 
                            </row>
                        </h:panelGrid>
                    </p:tab>
                </p:tabView>
            </p:layoutUnit>
        </p:layout>
        </h:form>
    </h:body>
    </html>
    

    ViewCompanyManagedBean.java:

    package com.example.beans;
    
    import com.example.entities.Company;
    import com.example.Scale;
    import com.example.entities.ScaleMeasurement;
    import com.example.services.CompanyService;
    import com.example.services.ScaleMeasurementService;
    import com.example.services.ScaleService;
    
    import java.io.IOException;
    import java.net.InetAddress;
    import java.net.UnknownHostException;
    import java.text.DateFormat;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.ArrayList;
    import java.util.Date;
    import java.util.List;
    import java.util.ResourceBundle;
    
    import javax.annotation.PostConstruct;
    import javax.faces.application.FacesMessage;
    import javax.faces.bean.ManagedBean;
    import javax.faces.bean.ManagedProperty;
    import javax.faces.bean.RequestScoped;
    import javax.faces.context.FacesContext;
    import javax.servlet.ServletContext;
    
    import org.jsoup.Jsoup;
    import org.jsoup.nodes.Element;
    import org.primefaces.context.RequestContext;
    import org.primefaces.event.SelectEvent;
    import org.jsoup.Jsoup;
    import org.jsoup.nodes.Document;
    import org.jsoup.nodes.Element;
    
    @ManagedBean
    @RequestScoped
    public class ViewCompanyManagedBean {
        public ViewCompanyManagedBean() {
    
        }
        @PostConstruct
        public void populateCompanyData() {     
            ScaleService ss = new ScaleService();
            ScaleMeasurementService sms = new ScaleMeasurementService();
            Scale right = ss.find("s02");
            Scale left = ss.find("s03");
            Date today = new Date();    
    
            this.measurements = sms.getMeasurementForScale(right,today);
            this.measurements.addAll(sms.getMeasurementForScale(left,today));
    
            SimpleDateFormat sdf = new SimpleDateFormat("ddMMyyyy");
            this.fileName = "Shredder_Measurements_"+  sdf.format(today);
    
            int counter = 1;
            for ( ScaleMeasurement a : this.measurements ) {
                counter++;
            }
            System.out.println(counter);
    
    
            images = new ArrayList<String>();
            for (int i = 1; i <= 7; i++) {
                images.add("s_" + i + ".jpg");
            }
        }
        @PostConstruct
        public void onDateSelect(SelectEvent event) throws UnknownHostException {
    
            FacesContext facesContext = FacesContext.getCurrentInstance();
            SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy");
            facesContext.addMessage(null,new FacesMessage(FacesMessage.SEVERITY_INFO, "Date Selected",format.format(event.getObject())));
            String date = format.format(event.getObject());
            try {
                Date selectedDate = format.parse(date);         
                ScaleService ss = new ScaleService();
                ScaleMeasurementService sms = new ScaleMeasurementService();
                Scale right = ss.find("ANM02");
                Scale left = ss.find("ANM03");  
    
                this.measurements = sms.getMeasurementForScale(right,selectedDate);
                this.measurements.addAll(sms.getMeasurementForScale(left,selectedDate));
    
                setMeasurements(this.measurements);
    
                SimpleDateFormat sdf = new SimpleDateFormat("ddMMyyyy");
                String newFileName = "Shredder_Measurements_"+  sdf.format(selectedDate);
                setFileName(newFileName);
                this.fileName = newFileName;
    
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        public Date getDate() {
            return date;
        }
    
        public void setDate(Date date) {
            this.date = date;
        }
    
        public List<ScaleMeasurement> getMeasurements() {
            return measurements;
        }
        public void setMeasurements(List<ScaleMeasurement> measurements) {
            this.measurements = measurements;
        }
        public String getFileName() {
            return fileName;
        }
        public void setFileName(String fileName) {
            this.fileName = fileName;
        }
        public List<String> getImages() {
            return images;
        }
        public void setImages(List<String> images) {
            this.images = images;
        }   
        // Used for Calendar
        private Date date;
        // Used for Shredder Measurements   
        private List<ScaleMeasurement> measurements = new ArrayList<ScaleMeasurement>();
        private String fileName;
        // Used for Pictures 
        private List<String> images;
    }
    

    我面临的问题是:   - 表格无法排序 - 每次选择排序时,表格都是    重新加载到当前日期   - 如果当前日期没有填写表格,那么我不能    以PDF格式提取前几天

    我的理解是我没有很好地处理ajax。 任何帮助将非常感激。

0 个答案:

没有答案