Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Copy these jars over to your WWS2_WEBAPP/WEB-INF/lib directory, and add them to your classpath.

...

Code Block
titlecom.mevipro.test.action.JasperAction
package com.mevipro.test.action;

import java.util.ArrayList;
import java.util.List;

import net.sf.jasperreports.engine.JasperCompileManager;

import com.mevipro.test.Person;
import com.opensymphony.xwork.ActionSupport;

public class JasperAction extends ActionSupport {

	//basic List - it will serve as our dataSource later on
	private List myList;

	/*
	 * (non-Javadoc)
	 *
	 * @see com.opensymphony.xwork.ActionSupport#execute()
	 */
	public String execute() throws Exception {

		// create some imaginary persons
		Person p1 = new Person(new Long(1), "Patrick", "Lightbuddie");
		Person p2 = new Person(new Long(2), "Jason", "Carrora");
		Person p3 = new Person(new Long(3), "Alexandru", "Papesco");
		Person p4 = new Person(new Long(4), "Jay", "Boss");

		/*
		 * store everything in a list - normally, this should be coming from a
		 * database but for the sake of simplicity, I left that out
		 */
		myList = new ArrayList();
		myList.add(p1);
		myList.add(p2);
		myList.add(p3);
		myList.add(p4);

		/*
		 * Here we compile our xml jasper template to a jasper file.
		 * Note: this isn't exactly considered 'good practice'.
		 * You should either use precompiled jasper files (.jasper) or provide some kind of check
		 * to make sure you're not compiling the file on every request.
		 * If you don't have to compile the report, you just setup your data source (eg. a List)
		 */
		try {
			JasperCompileManager.compileReportToFile(
					"WWS2_WEBAPP/jasper/our_jasper_template.jrxml",
					"WWS2_WEBAPP/jasper/our_compiled_template.jasper");
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
		//if all goes well ..
		return SUCCESS;
	}

	/**
	 * @return Returns the myList.
	 */
	public List getMyList() {
		return myList;
	}

}

...

Code Block
xml
xml
titleour_jasper_template.jrxml
<?xml version="1.0"?>
    <!DOCTYPE jasperReport
  PUBLIC "-//JasperReports//DTD Report Design//EN"
  "http://jasperreports.sourceforge.net/dtds/jasperreport.dtd">
    <jasperReport name="jasper_test">
      <!-- our fields -->
      <field name="name" class="java.lang.String"/>
      <field name="lastName" class="java.lang.String"/>
      <title>
        <band height="50">
          <staticText>
            <reportElement x="0" y="0" width="180" height="15"/>
            <textElement/>
            <text>
              <![CDATA[Webwork JasperReports Sample]]>
            </text>
          </staticText>
        </band>
      </title>
      <pageHeader>
        <band></band>
      </pageHeader>
      <columnHeader>
        <band height="20">
          <staticText>
            <reportElement x="180" y="0" width="180" height="20"/>
            <textElement>
              <font isUnderline="true"/>
            </textElement>
            <text>
              <![CDATA[NAME]]>
            </text>
          </staticText>
          <staticText>
            <reportElement x="360" y="0" width="180" height="20"/>
            <textElement>
              <font isUnderline="true"/>
            </textElement>
            <text>
              <![CDATA[LASTNAME]]>
            </text>
          </staticText>
        </band>
      </columnHeader>
      <detail>
        <band height="20">
          <textField>
            <reportElement x="180" y="0" width="180" height="15"/>
            <textElement/>
            <textFieldExpression>
              <![CDATA[$F{name}]]>
            </textFieldExpression>
          </textField>
          <textField>
            <reportElement x="360" y="0" width="180" height="15"/>
            <textElement/>
            <textFieldExpression>
              <![CDATA[$F{lastName}]]>
            </textFieldExpression>
          </textField>
        </band>
      </detail>
      <columnFooter>
        <band></band>
      </columnFooter>
      <pageFooter>
        <band height="15">
          <staticText>
            <reportElement x="0" y="0" width="40" height="15"/>
            <textElement/>
            <text>
              <![CDATA[Page:]]>
            </text>
          </staticText>
          <textField>
            <reportElement x="40" y="0" width="100" height="15"/>
            <textElement/>
            <textFieldExpression class="java.lang.Integer">
              <![CDATA[$V{PAGE_NUMBER}]]>
            </textFieldExpression>
          </textField>
        </band>
      </pageFooter>
      <summary>
        <band></band>
      </summary>
    </jasperReport>

Save this file in WWS2_WEBAPP/jasper/ as 'our_jasper_template.jrxml'.

...

expression. This means JR will ask WW Struts how to retrieve the field value. WW Struts will happily look up this value in the stack (find the person, and invoke the getName() getter), and return it. Similar for the

...

This parameter defines the location of our compiled jasper file, which will be filled by WW Struts with our dataSource:

Code Block
<param name="dataSource">myList</param>

...

You should now be able to execute http://localhost:8080/YOUR_WEBAPP/myJasperTest.action - and you should see a nice list of names.
WW Struts provides probably the most elegant way to deal with JasperReport files; specify the location of the .jasper file, specify what dataSource you want to use, and there you go.

...