0

I am using JSF 2.2 , Tomcat 7, primefaces 6.0 and iText 2.1.7. I am quite new to primfaces, and I am having 2 problems:

  1. Trying to sort a datable
  2. Exporting the datatable to a pdf

The table is reloaded everytime the user makes a selection on a calendar. My code is:

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;
}

The problems that I am facing are: - The table cannot be sorted - everytime I select sorting, the table is reloaded to the current date - In case current date does not fill in the table, then I cannot extract the previous days in pdf

My understanding is that I am not handling ajax well. Any help will be really appreciated.

Emil Sierżęga
  • 1,785
  • 2
  • 31
  • 38
Mitsos
  • 63
  • 1
  • 7
  • 1
    Hi, welcome to Stackoverflow. It is good practice to ask 1 question per question. So please break them up in two. And when you post code, please do it in [mcve] flavour. See also http://www.stackoverflow.com/tags/jsf/info – Kukeltje Dec 15 '16 at 08:37
  • Try `@ViewScoped` instead of `@RequestScoped`. Find more info here: [link](http://stackoverflow.com/questions/7031885/how-to-choose-the-right-bean-scope) – djnose Dec 16 '16 at 13:45
  • @ViewScoped is the right answer ! – Mitsos Dec 17 '16 at 09:31

0 Answers0