Sub Reporte

Para crear un subreporte solo tenemos que definir las dos clases Java y la clase principal debe contener un atributo de tipo private List<EntitySecundario> entitysecundario.

Usaremos los entitys

Facturas es el entity maestro y Detalles sera el entity para el subreporte.

Deseamos generar una vista de componentes como esta

con el subreport

Procedemos a crear las dos clases y generamos

Entity Factura

En la pestaña Sub Report creamos la clase detalles

Genera los reportes all, details, master, _subreport

Si buscamos en el proyecto web

Si lo ejecutamos

haciendo clic en Factura con Subreporte.

Nota:

Para un subreporte recuerde pasar la ruta del subreporte al parámetro "SUBREPORT_DIR" y el nombre del subreporte generado.

 String ruta = "/resources/reportes/factura/factura_master.jasper";
 HashMap parameters = new HashMap();
 String reportsDirPath = FacesContext.getCurrentInstance().getExternalContext().getRealPath("/resources/reportes/factura");
 reportsDirPath += "/";
 parameters.put("SUBREPORT_DIR", reportsDirPath + "/detalles_subreport.jasper");
 parameters.put("P_EMPRESA", "WizardReport");

Codigo que imprime el subreporte

 public String printFacturaSubReport() {

        try {
            List<Facturas> facturasList = new ArrayList<Facturas>();
            List<Detalles> detallesList = new ArrayList<>();

            detallesList.add(new Detalles(10, "Impresora"));
            detallesList.add(new Detalles(20, "Disco Duro"));
            facturasList.add(new Facturas(1, "Juana Bosquez", detallesList));

            //
            detallesList = new ArrayList<>();
            detallesList.add(new Detalles(30, "Memoria RAM"));
            facturasList.add(new Facturas(25, "Luris Bares", detallesList));

            String ruta = "/resources/reportes/facturas/facturas_master.jasper";
            HashMap parameters = new HashMap();

            printer.imprimir(facturasList, ruta, parameters);
//            String reportsDirPath = FacesContext.getCurrentInstance().getExternalContext().getRealPath("/resources/reportes/facturas/");
            String reportsDirPath = FacesContext.getCurrentInstance().getExternalContext().getRealPath("/resources/reportes/facturas");
//          reportsDirPath += "/";
            reportsDirPath += "/detalles_subreport.jasper";
//            parameters.put("SUBREPORT_DIR", reportsDirPath + "/detalles_subreport");
            parameters.put("SUBREPORT_DIR", reportsDirPath);
            parameters.put("P_EMPRESA", "WizardReport");

            printer.imprimir(facturasList, ruta, parameters);

        } catch (Exception ex) {
            JsfUtil.errorMessage("printFacturaSubReport() " + ex.getLocalizedMessage());
        }
        return null;
    }

Last updated