File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Struts and jasper report

 
Nakata kokuyo
Ranch Hand
Posts: 465
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good day to all,

i figure out how the struts action to call jasper report.

below is the details :

report name : rpt_creditnote.jasper
location : C:\jakarta-tomcat-5.5.9\webapps\usahawan\WEB-INF\report\

intention : to make sure the report is call successfully, this report
have some static string inside but no parameters yet.

i use the below code to call report, it show only blank report with no static text where i have put in for testing, i put under page header part of report



my questions :

1. why it called successfully but no static text inside the report
2. i have forward it to "success", which is using tiles and it suppose have one menu page on top and result page at bottom(report should put in result page).

3. can someone provide me better example to display report in struts framework ?

thank you very much for reading and guiding me, your help is highly appreciated , have a nice day
 
RoshaniG Gopal
Ranch Hand
Posts: 180
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Nakata
I have not worked much on Jasper but have a piece of code which might ans some Qs.
-------
import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.export.*;
import net.sf.jasperreports.engine.data.*;
import net.sf.jasperreports.engine.data.JRBeanArrayDataSource;


import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.xml.JRXmlLoader;
import net.sf.jasperreports.engine.JasperCompileManager;
import java.io.*;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperPrint;
import java.util.*;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.design.JasperDesign;
import net.sf.jasperreports.engine.util.JRLoader;

import net.sf.jasperreports.view.JasperViewer;

import common.Hello;
public class ShowHello
{

String fileName ;
Map hm = new HashMap();


public ShowHello(String jasperFileName)
{
fileName= jasperFileName;

}

public void test(Object[] Receipts, Map params, String outFileName)
{

try
{

List ds = new ArrayList();


for(int i=0; i<Receipts.length; i++)
ds.add(Receipts[i]);
System.out.println("start1");
hm=params;
CreatePDF(fileName,outFileName, hm, ds);

}
catch (Exception e)
{
e.printStackTrace();
}

}


public void CreatePDF(String jasperFileName, String outFilePDF, Map params, List dsList )
{

try
{

JasperReport jasperReport = JasperCompileManager.compileReport("C:/Documents and Settings/abc/Desktop/JASPER/jasper/Hello.jrxml");
System.out.println("****************"+jasperFileName);
JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(dsList);
System.out.println("-2");
JasperPrint print = JasperManager.fillReport(jasperReport,params,ds);
System.out.println("-1");
JRExporter exporter = new net.sf.jasperreports.engine.export.JRPdfExporter();

System.out.println("1");
exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME,outFilePDF);
System.out.println("2");
exporter.setParameter(JRExporterParameter.JASPER_PRINT,print);
System.out.println("3");
exporter.exportReport();
System.out.println("4");
System.out.println("Created file: " + outFilePDF);}
catch (JRException e)
{
System.out.println(e.getMessage());
}
catch (Exception e)
{
e.printStackTrace();
}


}

public static void main(String[] args)
{
Hello[] hi = new Hello[3];
hi[0]=new Hello();
hi[0].setId(1);
hi[0].setName("sun");


hi[1]=new Hello();
hi[1].setId(2);
hi[1].setName("sun1");

hi[2]=new Hello();
hi[2].setId(3);
hi[2].setName("sun2");

Map m=new HashMap();


//Generate the report
new ShowHello("..\\Hello.jasper").test(hi,m,"test.pdf");

}



}
 
Nakata kokuyo
Ranch Hand
Posts: 465
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roshani, i have try it, it generate a blank jasper for me
 
RoshaniG Gopal
Ranch Hand
Posts: 180
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, this will surely get you going.
http://struts.apache.org/2.x/docs/jasperreports-tutorial.html
 
Nakata kokuyo
Ranch Hand
Posts: 465
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Roshani,
it work now, and i just change the following


to


caused i didn't provide any ds on it, so pass connection will do

thank you for your help
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic