Hello Friends Hope you all are Fine.its been long time that i have posted a new post .well Reporting Tool always plays a major role in enterprise applications. I will going to demonstrate you Jasper Reports. for Designing & configuring Jasper Report i am using Ireport Designer 5.5.0 ,i will be using Traditional Student Application for showing reports.
The Project Structure is look like given below :
for connecting to db using ireport you need to go through https://www.netiq.com/documentation/idm401/reporting/data/bpbyf5y.html
The Report Layout in ireport designer will look like given below :
You can download japer report jars from http://sourceforge.net/projects/jasperreports/files/
you have to put .jrxml and .jasper file into project classpath . Spring Controller will look like given below:
StudentController.java
package org.demo.controller;
import java.sql.SQLException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.dbcp.BasicDataSource;
import org.dao.StudentDAO;
import org.demo.model.Student;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
@Controller
public class StudentController
{
@RequestMapping(method = RequestMethod.POST , value = "/GenerateReport")
public @ResponseBody void generatePdfReport(ModelAndView modelAndView,HttpServletRequest request,HttpServletResponse response) throws SQLException{
WebApplicationContext context =WebApplicationContextUtils.getWebApplicationContext(request.getServletContext());
StudentDAO dao=(StudentDAO)context.getBean("studentDAO");
BasicDataSource dataSource = (BasicDataSource)context.getBean("myDataSource");
String contextPath = request.getServletContext().getRealPath("report/StudentReport.jrxml");
dao.generatePdfReport(dataSource,contextPath);
}
}
Dao Layer will be as given below :
StudentDAO.java
package org.dao;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.design.JasperDesign;
import net.sf.jasperreports.engine.xml.JRXmlLoader;
import org.apache.commons.dbcp.BasicDataSource;
import org.demo.model.Student;
import org.springframework.orm.hibernate3.HibernateTemplate;
public class StudentDAO
{
private HibernateTemplate template;
public HibernateTemplate getTemplate() {
return template;
}
public void setTemplate(HibernateTemplate template) {
this.template = template;
}
public StudentDAO(HibernateTemplate template) {
this.template = template;
}
public void generatePdfReport(BasicDataSource dataSource, String contextPath) throws SQLException {
Connection conn= dataSource.getConnection();
try {
InputStream input = new FileInputStream(new File(contextPath));
System.out.println("Context Path is : ="+contextPath);
JasperDesign jasperDesign = JRXmlLoader.load(input);
System.out.println("Compiling Report Designs");
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
System.out.println("Creating JasperPrint Object");
Map<String, String> parameters = new HashMap<String, String>();
parameters.put("ReportTitle", "PDF JasperReport");
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport,null,conn);
// File f = new File("report/StudentReport.pdf");
File f = new File("d:\\StudentReport.pdf");
f.createNewFile();
//Exporting the report
OutputStream output = new FileOutputStream(f);
JasperExportManager.exportReportToPdfStream(jasperPrint, output);
System.out.println("Report Generation Complete");
conn.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
add_student.jsp
<script type="text/javascript" src="script/jquery-1.11.0.min.js"></script>
<script type="text/javascript" src="script/bootstrap.js"></script>
<script type="text/javascript" src="script/jquery-ui.min.js"></script>
<script type="text/javascript" src="script/jquery.layout-latest.js"></script>
<script type="text/javascript" src="script/local/grid.locale-en.js" ></script>
<script type="text/javascript" src="script/jquery.blockUI.js"></script>
<script type="text/javascript" src="script/js/javascript.js"></script>
<script type="text/javascript" src="script/js/InitStudent.js"></script>
<script type="text/javascript" src="script/js/StudentLogic.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#GenerateReport').click(function()
{
$.ajax({
url: "GenerateReport.html",
dataType:'json',
type:'post',
data: {},
success: function (dataCheck)
{
},
error: function (xhr, ajaxOptions, thrownError) {}
});
});
});
</script>
<table>
<tr>
<td>id:<input type="text" id="txtid" name="txtid"></td>
</tr>
<tr>
<td>Name:<input type="text" id="txtname" name="txtname"></td>
</tr>
<tr>
<td><input type="button" id="AddStudent" name="AddStudent" value="submit"></td>
</tr>
<tr>
<td><input type="button" id="GenerateReport" name="GenerateReport" value="Report"></td>
</tr>
</table>
The First page will look like given below:
You can Refer my earlier Post redirect get Pattern using spring mvc example for xml configuration required for project .
when you click on generate report button file will be stored on D:/.Report will look like given below:
Thanks for Reading this article. for any query ping me on pathak.nisarg@yahoo.com
The Project Structure is look like given below :
for connecting to db using ireport you need to go through https://www.netiq.com/documentation/idm401/reporting/data/bpbyf5y.html
The Report Layout in ireport designer will look like given below :
You can download japer report jars from http://sourceforge.net/projects/jasperreports/files/
you have to put .jrxml and .jasper file into project classpath . Spring Controller will look like given below:
StudentController.java
package org.demo.controller;
import java.sql.SQLException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.dbcp.BasicDataSource;
import org.dao.StudentDAO;
import org.demo.model.Student;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
@Controller
public class StudentController
{
@RequestMapping(method = RequestMethod.POST , value = "/GenerateReport")
public @ResponseBody void generatePdfReport(ModelAndView modelAndView,HttpServletRequest request,HttpServletResponse response) throws SQLException{
WebApplicationContext context =WebApplicationContextUtils.getWebApplicationContext(request.getServletContext());
StudentDAO dao=(StudentDAO)context.getBean("studentDAO");
BasicDataSource dataSource = (BasicDataSource)context.getBean("myDataSource");
String contextPath = request.getServletContext().getRealPath("report/StudentReport.jrxml");
dao.generatePdfReport(dataSource,contextPath);
}
}
Dao Layer will be as given below :
StudentDAO.java
package org.dao;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.design.JasperDesign;
import net.sf.jasperreports.engine.xml.JRXmlLoader;
import org.apache.commons.dbcp.BasicDataSource;
import org.demo.model.Student;
import org.springframework.orm.hibernate3.HibernateTemplate;
public class StudentDAO
{
private HibernateTemplate template;
public HibernateTemplate getTemplate() {
return template;
}
public void setTemplate(HibernateTemplate template) {
this.template = template;
}
public StudentDAO(HibernateTemplate template) {
this.template = template;
}
public void generatePdfReport(BasicDataSource dataSource, String contextPath) throws SQLException {
Connection conn= dataSource.getConnection();
try {
InputStream input = new FileInputStream(new File(contextPath));
System.out.println("Context Path is : ="+contextPath);
JasperDesign jasperDesign = JRXmlLoader.load(input);
System.out.println("Compiling Report Designs");
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
System.out.println("Creating JasperPrint Object");
Map<String, String> parameters = new HashMap<String, String>();
parameters.put("ReportTitle", "PDF JasperReport");
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport,null,conn);
// File f = new File("report/StudentReport.pdf");
File f = new File("d:\\StudentReport.pdf");
f.createNewFile();
//Exporting the report
OutputStream output = new FileOutputStream(f);
JasperExportManager.exportReportToPdfStream(jasperPrint, output);
System.out.println("Report Generation Complete");
conn.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
}
add_student.jsp
<script type="text/javascript" src="script/jquery-1.11.0.min.js"></script>
<script type="text/javascript" src="script/bootstrap.js"></script>
<script type="text/javascript" src="script/jquery-ui.min.js"></script>
<script type="text/javascript" src="script/jquery.layout-latest.js"></script>
<script type="text/javascript" src="script/local/grid.locale-en.js" ></script>
<script type="text/javascript" src="script/jquery.blockUI.js"></script>
<script type="text/javascript" src="script/js/javascript.js"></script>
<script type="text/javascript" src="script/js/InitStudent.js"></script>
<script type="text/javascript" src="script/js/StudentLogic.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#GenerateReport').click(function()
{
$.ajax({
url: "GenerateReport.html",
dataType:'json',
type:'post',
data: {},
success: function (dataCheck)
{
},
error: function (xhr, ajaxOptions, thrownError) {}
});
});
});
</script>
<table>
<tr>
<td>id:<input type="text" id="txtid" name="txtid"></td>
</tr>
<tr>
<td>Name:<input type="text" id="txtname" name="txtname"></td>
</tr>
<tr>
<td><input type="button" id="AddStudent" name="AddStudent" value="submit"></td>
</tr>
<tr>
<td><input type="button" id="GenerateReport" name="GenerateReport" value="Report"></td>
</tr>
</table>
The First page will look like given below:
You can Refer my earlier Post redirect get Pattern using spring mvc example for xml configuration required for project .
when you click on generate report button file will be stored on D:/.Report will look like given below:
Thanks for Reading this article. for any query ping me on pathak.nisarg@yahoo.com