I'm working in a project where we have to integrate Jasper Reports with Spring 2.5.6. After looking into the Spring Reference, I found that in the section 14.7. JasperReports there are all the details needed for the configuration of Jasper Report with Spring. Moreover, I found in the CVS an example of Jasper and Spring integration. However I was not able to find out a configuration with annotations based controllers. Therefore, I decided to code an annotation based version of the Jasper Demo Spring Example...
Controller
Here an example of configuration by using annotation based @Controller:
@Controller
public class ReportController extends MultiActionController {
// other code
@RequestMapping(value = "/exporterParameters.html")
public ModelAndView handleExporterParameters(HttpServletRequest request,
HttpServletResponse response) throws Exception {
return new ModelAndView("htmlReport", getModel());
}
}
web.xmlHere the configuration in the web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Jasper Reports</display-name>
<description>Jasper Reports Sample Application</description>
<servlet>
<servlet-name>jrtest</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jrtest</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>jrtest</servlet-name>
<url-pattern>*.pdf</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>jrtest</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>jrtest</servlet-name>
<url-pattern>*.csv</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>jrtest</servlet-name>
<url-pattern>*.xls</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<!-- log4j -->
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j.properties</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
</web-app>
jrtest-servlet.xmlHere instead the jrtest-servlet.xml. It is important to note the bean called htmlReport which contains the details related to the Jasper report definition. Moreover, here you can also see how to set the JRHtmlExporterParameter export properties.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:component-scan base-package="org.springframework.samples.jasperdemo.web"/>
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/>
<context:annotation-config />
<!-- If a JasperReports view requires more complex configuration then use the BeanNameViewResolver to
map a given view name to a given view bean -->
<bean id="nameViewResolver" class="org.springframework.web.servlet.view.BeanNameViewResolver"/>
<bean id="htmlReport" class="org.springframework.web.servlet.view.jasperreports.JasperReportsHtmlView">
<property name="url">
<value>/WEB-INF/reports/simpleReport.jasper</value>
</property>
<property name="exporterParameters">
<map>
<entry key="net.sf.jasperreports.engine.export.JRHtmlExporterParameter.HTML_FOOTER">
<value>Footer by Spring!</td><td width="50%">&nbsp; </td></tr></table></body></html></value>
</entry>
</map>
</property>
</bean>
</beans>
So, in this simple example you can see how to use Jasper with Spring with the controller based annotations.
I hope this help!
No comments:
Post a Comment