Struts2 Jasper Rerports Plugin

Please go through the following tutorial before proceeding further.

http://struts.apache.org/2.0.14/docs/jasperreports-tutorial.html

Following  is a little modified version of “struts.xml” given in above tutorial

    
        
            /jasper/our_compiled_template.jasper
            myList
            PDF
        
        
            /jasper/our_compiled_template.jasper
            myList
            XLS
        

...

But at this point if your website user try to download the report file, it is saved as “myJasperTest.action”. User has to rename it as “someFileName.pdf” or “someFileName.xls” to view it in appropriate program. You can make user’s  life easier by just adding a few lines into “struts.xml” file in every jasper related result. Following parameters are for PDF report result.

application/pdf
filename="document.pdf"

After adding above lines, “struts.xml” should look like the following

    
        
            /jasper/our_compiled_template.jasper
            myList
            PDF

            application/pdf
            filename="document.pdf"
        
        
            /jasper/our_compiled_template.jasper
            myList
            XLS

            application/xls
            filename="document.xls"


...

Here you have statically define the file name as “document”. User will always see the file “document.pdf” or “document.xls”. But if you want to dynamically change the file name, it will need a little more effort. Just change the following line

filename="document.pdf"

with the following one

filename="${fileName}.pdf"

You have given “fileName” as place holder. Now You have to provide a field and accessor of given place holder in you action class. In this case,  we have “com.acme.test.action.JasperAction” as described in the tutorial.

package com.acme.test.action;
...
public class JasperAction extends ActionSupport {
    ...
    private String fileName;          //place holder field
    ...
    public String getFileName()      // accessor
    {
        return fileName;
    }
}

You can change “fileName” in your action class according to your requirement and logic.

See Also

Advertisements

Immutable Class

Wikipedia states that

In object-oriented and functional programming, an immutable object is an object whose state cannot be modified after it is created.

In simple words, we cannot change value of any field of  an immutable object after its creation. Consider the following simple Data Model class

public class Person
{
    private String firstName;
    private String lastName;
    private Address address;

    public Person(String firstName, String lastName, Address address)
    {
        this.firstName = firstName;
        this.lastName = lastName;
        this.address = address;
    }

    public void setFirstName(String firstName)
    {
        this.firstName = firstName;
    }

    public String getFirstName()
    {
        return firstName;
    }
    public void setLastName(String lastName)
    {
        this.lastName = lastName;
    }

    public String getLastName()
    {
        return lastName;
    }
    public void setAddress(Address address)
    {
        this.address= address;
    }

    public Address getAddress()
    {
        return address;
    }
}

We can initialize object of Person class with following line of code

Person p = new Person ("Bilal", Mirza, new Address("Lahore", "Pakistan"));
//We supposed Address constructor with city and country name parameters

We cab also change fields of the object p by using its setters and getters, as follows

p.setFirstName("Hashim");
p.setLastName("Amla");
p.setAddress(new Address("Durban", "South Africa"));

For making objects immutable, we need to make following changes in its class structure.

  1. Declare the class as final to prevent it be extended.
  2. Declare all field as private and final.
  3. Remove all setter methods.
  4. If class contains reference of another object, return copy of this object in its getter method.

Following class is immutable version of above class

public final class Person
{
    private final String firstName;
    private final String lastName;
    private final Address address;

    public Person(String firstName, String lastName, Address address)
    {
       this.firstName = firstName;
       this.lastName = lastName;
       this.address = address;
    }

   public String getFirstName()
   {
      return firstName;
   }
    public String getLastName()
    {
        return lastName;
    }
    public Address getAddress()
    {
        return address.clone();
    }
}

See Also