Spring Web MVC - Spring Web Flow Working With JasperReports
Step 3: Engine and Wrap Class
An important part is the EngineSomeEntitiesJasperReport class, because it creates and fills the object JRBeanCollectionDataSource with data (here I am using the WrapSomeEntities, a simple pojo) filled explicitly. Then this class must be called by controller/action by S-MVC and SWF
public class EngineSomeEntitiesJasperReport {
public JRBeanCollectionDataSource engine(){
WrapSomeEntities wrapSomeEntitiesOne = new WrapSomeEntities();
wrapSomeEntitiesOne.setIdCustomer("MJE-88");
wrapSomeEntitiesOne.setNameCustomer("Manuel Jordan");
wrapSomeEntitiesOne.setPhoneCustomer("222222");
wrapSomeEntitiesOne.setIdProvider("XYZ-123");
wrapSomeEntitiesOne.setNameProvider("Company A");
wrapSomeEntitiesOne.setPhoneProvider("457898");
wrapSomeEntitiesOne.setIsbn("1590599799");
wrapSomeEntitiesOne.setTitleBook("Spring Recipes");
wrapSomeEntitiesOne.setPriceBook(new BigDecimal("49.99"));
WrapSomeEntities wrapSomeEntitiesTwo = new WrapSomeEntities();
wrapSomeEntitiesTwo.setIdCustomer("MJE-88");
wrapSomeEntitiesTwo.setNameCustomer("Manuel Jordan");
wrapSomeEntitiesTwo.setPhoneCustomer("222222");
wrapSomeEntitiesTwo.setIdProvider("XYZ-777");
wrapSomeEntitiesTwo.setNameProvider("Company B");
wrapSomeEntitiesTwo.setPhoneProvider("697451");
wrapSomeEntitiesTwo.setIsbn("020161622X");
wrapSomeEntitiesTwo.setTitleBook("The Pragmatic Programmer");
wrapSomeEntitiesTwo.setPriceBook(new BigDecimal("45.99"));
List<WrapSomeEntities> myList = new ArrayList<WrapSomeEntities>();
myList.add(wrapSomeEntitiesOne);
myList.add(wrapSomeEntitiesTwo);
JRBeanCollectionDataSource jRBeanCollectionDataSource = new JRBeanCollectionDataSource(myList);
return jRBeanCollectionDataSource;
}
}Dont Forget that our pojo WrapSomeEntities must contain all variables created like <field name="......."/> in the *.jrxml file by the query done in IReport
A more realistic code listing is:
public class EngineLineasArticulosJasperReport {
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
}
/**
* <p>
* Metodo que me llena y retorna el JRBeanCollectionDataSource
* </p>
*
* @return JRBeanCollectionDataSource
*/
public JRBeanCollectionDataSource engine(){
String query=" SELECT *, a.descripcion as descripcionarticulo " +
" FROM lineacategoria l, articulo a, medida d WHERE " +
" l.idLineaCategoria = a.idLineaCategoria AND a.idMedida=d.idMedida order by l.idLineaCategoria ";
Collection mycollecion = this.jdbcTemplate.query(
query ,
new RowMapper() {
public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
WrapLineasArticulos a = new WrapLineasArticulos();
a.setIdLineaCategoria(rs.getString("idLineaCategoria"));
a.setDescripcion(rs.getString("descripcion"));
a.setIdArticulo(rs.getString("idArticulo"));
a.setStockactual(rs.getBigDecimal("stockactual"));
a.setPrecioUnitario(rs.getBigDecimal("precioUnitario"));
a.setPrecioUnitarioVenta(rs.getBigDecimal("precioUnitarioVenta"));
a.setTotalValorizado(rs.getBigDecimal("totalValorizado"));
a.setXtraTextUnoArticulo(rs.getString("xtraTextUnoArticulo"));
a.setXtraNumDosArticulo(rs.getBigDecimal("xtraNumDosArticulo"));
a.setIdMedida(rs.getString("idMedida"));
a.setNombre(rs.getString("nombre"));
a.setDescripcionarticulo(rs.getString("descripcionarticulo"));
return a;
}
});
JRBeanCollectionDataSource jRBeanCollectionDataSource = new JRBeanCollectionDataSource(mycollecion);
return jRBeanCollectionDataSource;
}
}
If you look carefully the sql statement, it works for related tables (Pk/Fk relation). Another case could be for unrelated tables (working with JOIN clause). The rest of the code is obvious
Below the declararion of our WrapSomeEntities
public class WrapSomeEntities {
private String idCustomer;
private String nameCustomer;
private String phoneCustomer;
private String idProvider;
private String nameProvider;
private String phoneProvider;
private String isbn;
private String titleBook;
private BigDecimal priceBook;
public String getIdCustomer() {
return idCustomer;
}
public void setIdCustomer(String idCustomer) {
this.idCustomer = idCustomer;
}
public String getNameCustomer() {
return nameCustomer;
}
public void setNameCustomer(String nameCustomer) {
this.nameCustomer = nameCustomer;
}
public String getPhoneCustomer() {
return phoneCustomer;
}
public void setPhoneCustomer(String phoneCustomer) {
this.phoneCustomer = phoneCustomer;
}
public String getIdProvider() {
return idProvider;
}
public void setIdProvider(String idProvider) {
this.idProvider = idProvider;
}
public String getNameProvider() {
return nameProvider;
}
public void setNameProvider(String nameProvider) {
this.nameProvider = nameProvider;
}
public String getPhoneProvider() {
return phoneProvider;
}
public void setPhoneProvider(String phoneProvider) {
this.phoneProvider = phoneProvider;
}
public String getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
public String getTitleBook() {
return titleBook;
}
public void setTitleBook(String titleBook) {
this.titleBook = titleBook;
}
public BigDecimal getPriceBook() {
return priceBook;
}
public void setPriceBook(BigDecimal priceBook) {
this.priceBook = priceBook;
}
}
Our EngineSomeEntitiesJasperReport must be declared in our Spring context. For this bean, it is declared in applicationContext-jasperreportsengine.xml
<beans>
<bean id="idEngineSomeEntitiesJasperReport"
class="com.springjasperreports.model.jasperreport.engine.EngineSomeEntitiesJasperReport" />
</beans>
The bean declaration is clear by itself.
Step 04: Spring Web MVC
We have done the Model Layer (for simplicity in this case the EngineSomeEntitiesJasperReport class), now we must handle the controllers, so it's onto Spring Web MVC.
For that the ReportSimplePdfJasperReportController is created, see the code below
public class ReportSimplePdfJasperReportController extends AbstractController {
private EngineSomeEntitiesJasperReport engineSomeEntitiesJasperReport;
public void setEngineSomeEntitiesJasperReport(
EngineSomeEntitiesJasperReport engineSomeEntitiesJasperReport) {
this.engineSomeEntitiesJasperReport = engineSomeEntitiesJasperReport;
}
protected ModelAndView handleRequestInternal(
HttpServletRequest request, HttpServletResponse res)throws Exception{
ModelAndView mav = null;
try{
JRBeanCollectionDataSource jRBeanCollectionDataSource =
this.engineSomeEntitiesJasperReport.engine();
Map<String,Object> parameterMap = new HashMap<String,Object>();
parameterMap.put("datasource", jRBeanCollectionDataSource);
mav = new ModelAndView("reportsimplepdfjasperreport",parameterMap);
}
catch(Exception e){
}
return mav;
}
}
We are using a setter method to inject our engine class; the handleRequestInternal method is calling the engine method and receives the JRBeanCollectionDataSource object. For this example no parameters are used (i.e request.getParameter(...)). An important part of the code is parameterMap.put("datasource", jRBeanCollectionDataSource);
Our ReportSimplePdfJasperReportController must be declared in our Spring context. For this bean, it is declared in applicationContext-jasperreports.xml
<beans>
<bean id="idReportSimplePdfJasperReportController"
class="com.springjasperreports.controler.mvc.jasperreports.ReportSimplePdfJasperReportController" >
<property name="engineSomeEntitiesJasperReport" >
<ref bean="idEngineSomeEntitiesJasperReport" />
</property>
</bean>
</beans>
The bean declaration is clear by itself
- Login or register to post comments
- 29760 reads
- Printer-friendly version
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)










Comments
clermont38 replied on Fri, 2009/05/15 - 12:18pm
cnbaluramesh replied on Sat, 2009/06/13 - 4:03am
vijayasaradhi_p replied on Tue, 2009/08/18 - 10:56pm
rajesh_dzone replied on Sun, 2009/09/27 - 9:47pm
zizul replied on Fri, 2009/11/13 - 5:24am
Manuel Jordan replied on Sat, 2009/11/14 - 10:23pm
Hello Guys
The source is almost available.
My hard disk crushed months ago, and the source code is somewhere among many DVD,
Regards