Tuesday, 10 November 2015

Jasper Report Integration with Spring 3.x


Step-1

Add dependency inside the pom.xml

 <!-- Dependency for Jasper iReport -->

    <dependency>
    <groupId>net.sf.jasperreports</groupId>
    <artifactId>jasperreports</artifactId>
    <version>3.7.6</version>
    <type>jar</type>
    <scope>compile</scope>
    <exclusions>
    <exclusion>
    <artifactId>commons-collections</artifactId>
    <groupId>commons-collections</groupId>
    </exclusion>
    <exclusion>
    <artifactId>commons-beanutils</artifactId>
    <groupId>commons-beanutils</groupId>
    </exclusion>
    <exclusion>
    <artifactId>commons-digester</artifactId>
    <groupId>commons-digester</groupId>
    </exclusion>
    <exclusion>
    <artifactId>commons-logging</artifactId>
    <groupId>commons-logging</groupId>
    </exclusion>
    </exclusions>
    </dependency>
    <dependency>
    <groupId>commons-digester</groupId>
    <artifactId>commons-digester</artifactId>
    <version>2.1</version>
    <type>jar</type>
    <scope>compile</scope>
    </dependency>
    
    <dependency>
    <groupId>commons-collections</groupId>
    <artifactId>commons-collections</artifactId>
    <version>3.2.1</version>
    <type>jar</type>
    <scope>compile</scope>
    </dependency>
    
    <dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>3.6</version>
    <type>jar</type>
    <scope>compile</scope>

    </dependency>

     <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context-support</artifactId>
        <version>${spring.version}</version>
    </dependency>

    
Step-2
Design  your report in iReport tool
example - fruits.jrxml


Step-3
Write code inside controller for Jasper Report Integration

@RequestMapping(value="fruitReportsPdf.do",method=RequestMethod.GET)
public ModelAndView fruitReportsPdf(){
List<FruitForm> fruitList=iFruitService.findAllFruits();

FruitData fruitData=new FruitData();
fruitData.setFruitMessage("I like fruits");
fruitData.setLeftImage("16.jpg");
fruitData.setRightImage("3.jpg");
fruitData.setFruitFormList(fruitList);
List<FruitData> fruitDataList=new ArrayList<FruitData>();
fruitDataList.add(fruitData);
// Assign the datasource to an instance of JRDataSource
// JRDataSource is the datasource that Jasper understands
// This is basically a wrapper to Java's collection classes
    // Wrap the collection in a JRBeanCollectionDataSource
// This is one of the collections that Jasper understands
JRDataSource jrDataSource = new JRBeanCollectionDataSource(fruitDataList);
// In order to use Spring's built-in Jasper support,
// We are required to pass our datasource as a map parameter
// parameterMap is the Model of our application
Map<String,Object> parameterMap = new HashMap<String,Object>();
parameterMap.put("keerti", jrDataSource);
ModelAndView modelAndView = new ModelAndView("pdfReport", parameterMap);
//pdfReport - the  the view name
return modelAndView;

}
 
     public class FruitData {
   
private String leftImage;
private String rightImage;
private String fruitMessage;

private List<FruitForm> fruitFormList;

public List<FruitForm> getFruitFormList() {
return fruitFormList;
}

 }  



Step-4

Design view resolver 
/WEB-INF/jasper-views.xml

<bean id="xlsReport"
  class="org.springframework.web.servlet.view.jasperreports.JasperReportsXlsView"
p:url="classpath:fruits.jrxml"

p:reportDataKey="keerti" />



Step-5

import /WEB-INF/jasper-views.xml inside
spring web application context file shown as below

  
fruit-processor-servlet.xml

<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
p:prefix="/" p:suffix=".jsp" p:order="1"/>

<!--  
Resolves view names based on the names declared on the declared xml location
All our Jasper views are declared inside the specified xml location.
Take note of the ordering of this ViewResolver.
-->
<bean class="org.springframework.web.servlet.view.XmlViewResolver"

p:location="/WEB-INF/jasper-views.xml"  p:order="0" />



Step-6

fruits.jrxml

<subDataset name="FADataset">
<field name="fid" class="java.lang.String"/>
<field name="fruitName" class="java.lang.String"/>
<field name="color" class="java.lang.String"/>
<field name="taste" class="java.lang.String"/>
<field name="price" class="java.lang.Integer"/>
<field name="category" class="java.lang.String"/>
</subDataset>

<field name="fruitFormList" class="java.util.List"/>
 <field name="leftImage" class="java.lang.String"/>
  <field name="rightImage" class="java.lang.String"/>

 <field name="fruitMessage" class="java.lang.String"/>


<datasetRun subDataset="FADataset">
<dataSourceExpression><![CDATA[new net.sf.jasperreports.engine.data.JRBeanCollectionDataSource($F{fruitFormList})]]></dataSourceExpression>

</datasetRun>


------------------------------------------------------------------------------------------------------

http://localhost:8080/spring-app-fruit/fruitReportsPdf.do





    
   

No comments:

Post a Comment