Here are all the pieces that should get someone started. I have not yet figured out how to set parameters for the report yet.
BookController, which does all the work:
Code:
import org.springframework.web.servlet.mvc.Controller;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
public class BookController implements Controller {
private BookList bookList = new BookList();
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
throws Exception
{
System.out.println("handle request");
return new ModelAndView("viewList", "reportData", getModel());
}
public BookList getModel() {
HashMap m = new HashMap();
m.put("NAME", "Lord of the Rings: Fellowship of the Ring");
m.put("BOOK_ID", new Integer(1));
bookList.add(m);
m = new HashMap();
m.put("NAME", "Lord of the Rings: The Two Towers");
m.put("BOOK_ID", new Integer(2));
bookList.add(m);
m = new HashMap();
m.put("NAME", "Lord of the Rings: Return of the King");
m.put("BOOK_ID", new Integer(3));
bookList.add(m);
return bookList;
}
public void setBookList(BookList b) {
System.out.println("setBookList()");
this.bookList = b;
}
public BookList getBookList() {
System.out.println("getBookList()");
return bookList;
}
}
BookList model object:
Code:
import java.util.*;
import net.sf.jasperreports.engine.JRRewindableDataSource;
import net.sf.jasperreports.engine.JRField;
import net.sf.jasperreports.engine.JRException;
public class BookList implements JRRewindableDataSource {
ArrayList books = new ArrayList();
int pos = -1;
public BookList() {
System.out.println("book data source instantiated");
}
public void add(Map m) {
books.add(m);
}
public void moveFirst() throws JRException {
System.out.println("rewind");
pos = 0;
}
public Object getFieldValue(JRField field) throws JRException {
System.out.println("requesting field '"+field.getName()+"'");
return ((Map)books.get(pos)).get(field.getName());
}
public boolean next() {
System.out.println("next");
if (pos+1 < books.size()) {
pos++;
System.out.println("next: returning true");
return true;
}
System.out.println("next: returning false");
return false;
}
}
The spring-servlet.xml:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<!--
- Application context definition for "springapp" DispatcherServlet.
-->
<beans>
<bean id="viewList" class="com.logicalapps.appscreate.BookController">
<property name="bookList">
<ref bean="bookBean"/>
</property>
</bean>
<bean id="bookBean" class="com.logicalapps.appscreate.BookList"/>
<bean id="handlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="*.do">viewList</prop>
</props>
</property>
</bean>
<!-- The ResourceBundleViewResolver: -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.ResourceBundleViewResolver">
<property name="basename"><value>views</value></property>
</bean>
</beans>
The views.properties:
Code:
viewList.class=org.springframework.web.servlet.view.jasperreports.JasperReportsPdfView
viewList.url=/WEB-INF/reports/bookreport.jrxml
viewList.reportDataKey=reportData
I am sure that there may be another way to do this and perhaps more elegant (I would love to see an example). Maybe those Spring Framework masters out there can help...I myself am a Spring Framework beginner (< few weeks =P). Anyone know how to pass parameters to the jasper report? Perhaps by adding some more wire-up and a couple lines of code...just not sure how. Either way, this doesn't appear to be working exactly how the documentation describes.