跳至主要内容

Generate reports with Seam 3 Reports and Apache Velocity

Seam 3 provides a collection of standard CDI extensions. Seam3 report module bridges CDI and several report engines, such as

Basic Configuration

Assume you have already created a Maven based Java EE 6 application. If not, you can create one using JBoss Forge.
Add Seam 3 reports related dependencies into your pom.xml.
  <dependency>
        <groupId>org.jboss.seam.reports</groupId>
        <artifactId>seam-reports-api</artifactId>
        <version>${seam-reports-version}</version>
  </dependency>

  <!-- If you are using Jasper Reports, add the following dependency --> 
  <dependency>
        <groupId>org.jboss.seam.reports</groupId>
        <artifactId>seam-reports-jasper</artifactId>
        <version>${seam-reports-version}</version>
  </dependency>
Generally, in order to generate a JasperReports based report in a Seam 3/Java EE6 project, you could consider the following steps:
  1. Create JasperRoports jrxml file using iReports or JasperStudio
  2. Compile jrxml file to PDF
You can use the official JaperReports Studio(for Eclipse users) or iReports(for NetBeans users) to create the reports template source file.
In your java code, inject JasperReports compiler to compile the jasperReports source, and JasperReports renderer to render the compiled result.
  @Inject
  @Jasper
  private transient ReportCompiler compiler;

  @Inject
  @PDF
  @Jasper
  private transient ReportRenderer pdfRenderer;
The @Jasper annotation states we will use JasperReports as report engine, @PDF is the final generated document format.
    ReportDefinition report;
    try {
        report = compiler.compile(<JasperReporst source>);
        Report reportInstance = report.fill(<JR DataSource>, null);

        pdfRenderer.render(reportInstance,
                externalContext.getResponseOutputStream());
    } catch (ReportException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
JRDataSource is a JasperReports specified "datasource" which is responsible of gathering the data for report generation. JasperReports provides several implementations natively, please refer to the JRDataSource javadoc for more detailed info. In this case, we have got a list, we can use JRBeanCollectionDataSource to wrap the existing list and use it in this reports.
Everything works well.
But personally, I dislike the jrxml syntax and do not want to use jrxml syntax to fill data, I only want to use JasperReports as report engine. I am familiar with Apache Velocity, is possible using Velocity as template and fill data?

Improve the codes with Apache Veloctiy

Now change the report generation process slightly, and introduce an extra step to generate the pure jrxml.
  1. Create a velocity template source(embed velocity syntax into jrxml)
  2. Convert the velocity template to pure jrxml
  3. Compile the jrmxl to PDF.
Create a jrxml firstly, and embed the velocity syntax.
  <detail>
        <band height="125" splitType="Stretch">
            #set( $y = 0 )
            #foreach($obj in $contacts)
            <staticText>
                <reportElement  x="0" y="$y" width="177" height="20"/>
                <textElement textAlignment="Left" verticalAlignment="Middle"/>
                <text><![CDATA[$!obj.name]]></text>
            </staticText>
            <staticText>
                <reportElement  x="177" y="$y" width="200" height="20"/>
                <textElement textAlignment="Left" verticalAlignment="Middle"/>
                <text><![CDATA[$!obj.phoneNumber]]></text>
            </staticText>
            <staticText>
                <reportElement x="377" y="$y" width="178" height="20"/>
                <textElement textAlignment="Left" verticalAlignment="Middle"/>
                <text><![CDATA[$!obj.emailAddress]]></text>
            </staticText>
            #set($y = $y + 20)
            #end
        </band>
    </detail>
In java codes, firstly fill data in the velocity based jrxml, then convert it to pure jrxml, finally compile it to PDF document.
    InputStream sourceTemplate = resourceProvider
            .loadResourceStream(reportTemplate);

    Map<String, Object> _values = new HashMap<String, Object>();
    _values.put("contacts", contacts);
    _values.put("usd", "$");

    String stringReport = new VelocityTemplate(sourceTemplate,
            velocityContext).merge(_values);

    if (log.isDebugEnabled()) {
        log.debug("report source file content@" + stringReport);
    }
    // source
    ReportDefinition report;
    try {
        report = compiler.compile(new ByteArrayInputStream(stringReport
                .getBytes("UTF-8")));
        Report reportInstance = report.fill(new JREmptyDataSource(), null);

        pdfRenderer.render(reportInstance,
                externalContext.getResponseOutputStream());
    } catch (ReportException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
Note, the velocity support is from the Seam3 mail module.
In the Seam3 repository, there is a standalone renderer module for rendering template, but it is not released a stable version at the moment.
So you have to add Seam3 mail dependencies in your pom.xml.
    <dependency>
        <groupId>org.jboss.seam.mail</groupId>
        <artifactId>seam-mail-api</artifactId>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.jboss.seam.mail</groupId>
        <artifactId>seam-mail</artifactId>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.velocity</groupId>
        <artifactId>velocity</artifactId>
    </dependency>

Run the project

I assume you have installed the latest Oracle JDK 7, JBoss AS 7.1.1.Final and Apache Maven 3.0.4.
  1. Check out the complete codes from github.com.
    git clone git://github.com/hantsy/seam3-report-demo.git
    
  2. Start JBoss AS from command line.
    <JBOSS_HOME>\bin\standalone.bat
    
  3. Deploy the application into the running JBoss AS.
    mvn clean package jboss-as:deploy
    
  4. Open your browser and go to http://localhost:8080/seam3-reports-demo.
发表评论

此博客中的热门博文

JPA 2.1: Attribute Converter

JPA 2.1: Attribute Converter If you are using Hibernate, and want a customized type is supported in your Entity class, you could have to write a custom Hibernate Type. JPA 2.1 brings a new feature named attribute converter, which can help you convert your custom class type to JPA supported type. Create an Entity Reuse thePostentity class as example. @Entity @Table(name="POSTS") public class Post implements Serializable { private static final long serialVersionUID = 1L; @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name="ID") private Long id; @Column(name="TITLE") private String title; @Column(name="BODY") private String body; @Temporal(javax.persistence.TemporalType.DATE) @Column(name="CREATED") private Date created; @Column(name="TAGS") private List<String> tags=new ArrayList<>(); } Create an attribute converter In this example…

Auditing with Hibernate Envers

Auditing with Hibernate Envers The approaches provided in JPA lifecyle hook and Spring Data auditing only track the creation and last modification info of an Entity, but all the modification history are not tracked. Hibernate Envers fills the blank table. Since Hibernate 3.5, Envers is part of Hibernate core project. Configuration Configure Hibernate Envers in your project is very simple, just need to addhibernate-enversas project dependency. <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-envers</artifactId> </dependency> Done. No need extra Event listeners configuration as the early version. Basic Usage Hibernate Envers provides a simple@Auditedannotation, you can place it on an Entity class or property of an Entity. @Audited private String description; If@Auditedannotation is placed on a property, this property can be tracked.

Spring Data Mongo: bridge MongoDB and Spring

Spring Data Mongo: bridge MongoDB and SpringMongoDBis one of the most popular NoSQL products, Spring Data Mongo(Maven archetype id is spring-data-mongodb) tries to provides a simple approach to access MongoDB. Configuration Add the following code fragments in your Spring configuration. <!-- Mongo config --> <mongo:db-factory id="mongoDbFactory" host="localhost" port="27017" dbname="conference-db" /> <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"> <constructor-arg ref="mongoDbFactory" /> </bean> <mongo:repositories base-package="com.hantsylabs.example.spring.mongo" /> Firstly, declare aDbFactorywhich is responsible for connecting to the MongoDB server. Then, register theMongoTemplateobject which envelop the data operations on MongoDB. Lastly addmongo:repositorieswith an essentialbase-packageattribute, Spring will discovery your d…