Announcement Announcement Module
Collapse
No announcement yet.
Spring + JasperReports Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring + JasperReports

    Hi everyone,
    I want to use Spring MVC to display charts created with Jasper. I cannot find much in the way of clear documentation on how to do this. Everything I have found so far is either not applicable or missing steps.

    My requirements:
    - I need to display data stored in a database as a chart on an HTML page.

    - The query used to get the data to be charted, is driven by user input,
    meaning the user will select applicable dates and other parameters to plug
    into the query.

    - Spring DAO (jdbc) is being used to interface with the database. If for some
    reason spring jdbc won't work with jasper I can switch to a different tech
    like hibernate through spring

    I have looked at the Spring documentation regarding Jasper, but it neglected to say how to create the jrxml file.

    How do I do this ? Will jasper work given my requirement that the sql queries are not static ?

    Thanks in advance !

  • #2
    You can check my tutorial for Jasper and DynamicJasper at http://krams915.blogspot.com/p/tutorials.html

    You might find something useful there. Regarding your case, since the query is dynamically generated, it's better to use DynamicJasper

    By the way the Spring documentation for the JRXML file is located in the ViewResolver section of
    http://static.springsource.org/sprin...guration-views

    <bean id="viewResolver" class="org.springframework.web.servlet.view.Resour ceBundleViewResolver">
    <property name="basename" value="views"/>
    </bean>

    simpleReport.(class)=org.springframework.web.servl et.view.jasperreports.JasperReportsPdfView
    simpleReport.url=/WEB-INF/reports/DataSourceReport.jrxml
    Last edited by skram; Dec 24th, 2010, 06:58 PM.

    Comment


    • #3
      You can consider my work too

      Spring Web MVC Spring Web Flow Working With JasperReports

      HTH

      Comment


      • #4
        Thanks

        Thanks for both your replies. I am reading through and trying both your tutorials.
        i'd like to make the suggestion to both of you that in your tutorials you include the full xml namespace and scheme

        It makes following the tutorial much easier. Nothing works without it.

        Thanks

        Comment


        • #5
          Krams: I'm following your tutorial (http://krams915.blogspot.com/2010/12...tegration.html) but cannot get it to deploy to Tomcat. Tomcat has the following complaint about the applicationContext.xml

          catalina.2010-12-30.log
          Code:
          rg.springframework.beans.factory.BeanDefinitionStoreException: Parser configuration exception parsing XML from ServletContext resource [/WEB-INF/applicationContext.xml]; nested exception is javax.xml.parsers.ParserConfigurationException: Unable to validate using XSD: Your JAXP provider [gnu.xml.dom.JAXPFactory@de1b8a] does not support XML Schema. Are you running on Java 1.4 with Apache Crimson? Upgrade to Apache Xerces (or Java 1.5) for full XSD support.
          	at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:404)
          	at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:334)
          	at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:302)
          	at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:143)
          	at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:178)
          	at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:149)
          	at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:124)
          	at org.springframework.web.context.support.XmlWebApplicationContext.loadBeanDefinitions(XmlWebApplicationContext.java:93)
          	at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:130)
          	at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:458)
          	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:388)
          	at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:261)
          	at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:192)
          	at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:47)
          	at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4135)
          	at org.apache.catalina.core.StandardContext.start(StandardContext.java:4630)
          	at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:791)
          	at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:771)
          	at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:546)
          	at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:905)
          	at org.apache.catalina.startup.HostConfig.deployWARs(HostConfig.java:740)
          	at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:500)
          	at org.apache.catalina.startup.HostConfig.start(HostConfig.java:1277)
          	at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:321)
          	at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)
          	at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1053)
          	at org.apache.catalina.core.StandardHost.start(StandardHost.java:785)
          	at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
          	at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:445)
          	at org.apache.catalina.core.StandardService.start(StandardService.java:519)
          	at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)
          	at org.apache.catalina.startup.Catalina.start(Catalina.java:581)
          	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
          	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
          	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
          	at java.lang.reflect.Method.invoke(Unknown Source)
          	at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289)
          	at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)
          Caused by: javax.xml.parsers.ParserConfigurationException: Unable to validate using XSD: Your JAXP provider [gnu.xml.dom.JAXPFactory@de1b8a] does not support XML Schema. Are you running on Java 1.4 with Apache Crimson? Upgrade to Apache Xerces (or Java 1.5) for full XSD support.
          	at org.springframework.beans.factory.xml.DefaultDocumentLoader.createDocumentBuilderFactory(DefaultDocumentLoader.java:102)
          	at org.springframework.beans.factory.xml.DefaultDocumentLoader.loadDocument(DefaultDocumentLoader.java:70)
          	at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:388)
          	... 37 more
          Caused by: java.lang.IllegalArgumentException: http://java.sun.com/xml/jaxp/properties/schemaLanguage
          	at gnu.xml.dom.JAXPFactory.setAttribute(JAXPFactory.java:109)
          	at org.springframework.beans.factory.xml.DefaultDocumentLoader.createDocumentBuilderFactory(DefaultDocumentLoader.java:99)
          	... 39 more
          I get the same exception when I use xerces.

          applicationContext.xml
          Code:
          <?xml version="1.0" encoding="UTF-8"?>
          <beans xmlns="http://www.springframework.org/schema/beans"
          	xmlns:aop="http://www.springframework.org/schema/aop"
          	xmlns:context="http://www.springframework.org/schema/context"
          	xmlns:jee="http://www.springframework.org/schema/jee"
          	xmlns:tx="http://www.springframework.org/schema/tx"
          	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          	xmlns:mvc="http://www.springframework.org/schema/mvc"
          	xsi:schemaLocation="http://www.springframework.org/schema/aop
          	http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
          	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
          	http://www.springframework.org/schema/jee
          	http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
          	http://www.springframework.org/schema/tx
          	http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
          	http://www.springframework.org/schema/mvc
          	http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
          
          	<context:annotation-config />
          	<context:component-scan base-package="com.foo.opstats" />
          	<mvc:annotation-driven />
          	<import resource="jasper-views.xml" />
          </beans>
          web.xml
          Code:
          <?xml version="1.0" encoding="UTF-8"?>
          
          <web-app version="2.5" 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_5.xsd">
          
          	<display-name>Spring3Example</display-name>
          
          	<servlet>
                  <servlet-name>spring</servlet-name>
                  <servlet-class>
                      org.springframework.web.servlet.DispatcherServlet
                  </servlet-class>
                  <load-on-startup>1</load-on-startup>
              </servlet>
          
              <servlet-mapping>
                  <servlet-name>spring</servlet-name>
                  <url-pattern>/app/*</url-pattern>
              </servlet-mapping>
          
          	<listener>
          		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
          	</listener>
          
          </web-app>
          jasper-views.xml
          Code:
          <?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" 
          	xsi:schemaLocation="
          		http://www.springframework.org/schema/beans 
          		http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
          
          	<!-- 
          	Declare Spring's View Resolvers for Jasper
          	 
          	Based on the bean names we can infer that:
          	* pdfReport is for generating PDFs
          	* xlsReport is for Excel format reports
          	* htmlReport is for HTML reports
          	* csvReport is for CSV reports
          	-->
          	<!-- 
          	id: the name to be used as a reference in the controller
          	url: the path where the Jasper JRXML file is located
          	reportDateKey: the name of the datasource. This is passed by the controller
          	-->
          	
          	<bean id="htmlReport"
          	class="org.springframework.web.servlet.view.jasperreports.JasperReportsHtmlView"
          	p:url="classpath:opstats-job-chart.jrxml" />
          
          </beans>
          Thanks

          Comment


          • #6
            Originally posted by publicstaticvoidmain View Post
            Thanks for both your replies. I am reading through and trying both your tutorials.
            i'd like to make the suggestion to both of you that in your tutorials you include the full xml namespace and scheme

            It makes following the tutorial much easier. Nothing works without it.

            Thanks
            It's in the tutorial and in the accompanying source code:
            Code:
            <?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"
            xsi:schemaLocation="
            http://www.springframework.org/schema/beans 
            http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
            Ah I see. I get what you mean. You mean the schema for the applicationContext.xml, web.xml, and spring-servlet.xml. I'm gonna update that either later or tomorrow

            The error message is:
            Code:
            Caused by: javax.xml.parsers.ParserConfigurationException: Unable to validate using XSD: Your JAXP provider [gnu.xml.dom.JAXPFactory@de1b8a] does not support XML Schema. Are you running on Java 1.4 with Apache Crimson? Upgrade to Apache Xerces (or Java 1.5) for full XSD support.
            What version of Tomcat are you using?

            Also, double check what jar versions where deployed in your project. Did you try running the application using Maven?
            Last edited by skram; Dec 31st, 2010, 04:29 AM.

            Comment


            • #7
              Thanx for the reply. Using Tomcat 6.0.29

              Comment


              • #8
                Originally posted by skram View Post
                It's in the tutorial and in the accompanying source code:
                Code:
                <?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"
                xsi:schemaLocation="
                http://www.springframework.org/schema/beans 
                http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
                Ah I see. I get what you mean. You mean the schema for the applicationContext.xml, web.xml, and spring-servlet.xml. I'm gonna update that either later or tomorrow

                The error message is:
                Code:
                Caused by: javax.xml.parsers.ParserConfigurationException: Unable to validate using XSD: Your JAXP provider [gnu.xml.dom.JAXPFactory@de1b8a] does not support XML Schema. Are you running on Java 1.4 with Apache Crimson? Upgrade to Apache Xerces (or Java 1.5) for full XSD support.
                What version of Tomcat are you using?

                Also, double check what jar versions where deployed in your project. Did you try running the application using Maven?
                Can you tell me how to resolve the error I'm getting ? Im not using Maven. See attached for jar versions.

                Thanks
                Last edited by publicstaticvoidmain; Dec 31st, 2010, 08:38 AM.

                Comment


                • #9
                  I just tried with Tomcat 6.0.28 ( the version I have on dev ) and it works.

                  Then I run it with embedded Tomcat using Maven, and it run.

                  Then I run it wih embedded Jetty using Maven. It run as well.

                  Are your Jasper files (JRXML, images) included in the classpath?

                  Comment


                  • #10
                    Originally posted by skram View Post
                    I just tried with Tomcat 6.0.28 ( the version I have on dev ) and it works.

                    Then I run it with embedded Tomcat using Maven, and it run.

                    Then I run it wih embedded Jetty using Maven. It run as well.

                    Are your Jasper files (JRXML, images) included in the classpath?
                    They are added to the compile classpath in eclipse (build path), how do I add them to Tomcat's build path ? Can you provide me a sample WAR file ?

                    Deployed jars:

                    commons-beanutils-1.8.3.jar
                    commons-collections-3.2.1.jar
                    commons-dbcp-1.4.jar
                    commons-digester-2.1.jar
                    commons-logging-1.1.1.jar
                    commons-pool-1.5.4.jar
                    iText-2.1.5.jar
                    jasperreports-3.7.6.jar
                    jasperreports-fonts-3.7.6.jar
                    jasperreports-javaflow-3.7.6.jar
                    jcommon-1.0.16.jar
                    jfreechart-1.0.13-experimental.jar
                    jfreechart-1.0.13-swt.jar
                    jfreechart-1.0.13.jar
                    jstl-1.2.jar
                    servlet-api-2.5.jar
                    spring-aop-3.0.0.RELEASE.jar
                    spring-asm-3.0.0.RELEASE.jar
                    spring-beans-3.0.0.RELEASE.jar
                    spring-context-3.0.0.RELEASE.jar
                    spring-core-3.0.0.RELEASE.jar
                    spring-expression-3.0.0.RELEASE.jar
                    spring-jdbc-3.0.0.RELEASE-sources.jar
                    spring-jdbc-3.0.0.RELEASE.jar
                    spring-orm-3.0.0.RELEASE.jar
                    spring-test-3.0.0.RELEASE-sources.jar
                    spring-test-3.0.0.RELEASE.jar
                    spring-tx-3.0.0.RELEASE.jar
                    spring-web-3.0.0.RELEASE.jar
                    spring-webmvc-3.0.0.RELEASE.jar
                    spring-webmvc-portlet-3.0.0.RELEASE.jar
                    swtgraphics2d.jar
                    xerces.jar

                    Comment


                    • #11
                      I solved that problem it seems. Problem was caused by one of the jars I had -
                      xerces.jar and gnujaxp.jar (when I was using it instead of xerces)
                      Removing the jar atleast stopped Tomcat from complaining.

                      This is the beans tag I used btw

                      Code:
                      <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" 
                      	xmlns:mvc="http://www.springframework.org/schema/mvc" 
                      	xsi:schemaLocation="
                      	http://www.springframework.org/schema/beans
                      	http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                      	http://www.springframework.org/schema/context
                      	http://www.springframework.org/schema/context/spring-context-3.0.xsd
                      	http://www.springframework.org/schema/mvc
                      	http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
                      ">

                      Comment


                      • #12
                        That's great. I'm glad the problem is solved

                        Comment


                        • #13
                          Class Not Found

                          Removed, please see post below this.
                          Last edited by publicstaticvoidmain; Jan 4th, 2011, 06:25 PM.

                          Comment


                          • #14
                            Update

                            Skram: I converted your pom file to an ivy file and downloaded *most* of the libraries. There were some that could not be resolved. Included in what was downloaded were 2 spring 2.5 jars. When I had these jars in the deployed app I got this exception when Tomcat tried to load the application:

                            Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListe ner
                            java.lang.NoSuchMethodError: org.springframework.beans.factory.support.DefaultL istableBeanFactory.setSerializationId(Ljava/lang/StringV
                            at org.springframework.context.support.AbstractRefres hableApplicationContext.refreshBeanFactory(Abstrac tRefreshableApplicationContext.java:128)
                            ...


                            When I remove the 2 Spring 2.5 jars I get this exception:

                            Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListe ner
                            org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'htmlReport' defined in ServletContext resource [/WEB-INF/jasper-views.xml]: Initialization of bean failed; nested exception is java.lang.NoClassDefFoundError: org/objectweb/asm/Opcodes
                            at org.springframework.beans.factory.support.Abstract AutowireCapableBeanFactory.doCreateBean(AbstractAu towireCapableBeanFactory.java:519)
                            ...


                            These are the jars I currently have:
                            Code:
                            ant-1.7.0.jar
                            ant-1.7.1.jar
                            ant-contrib-1.0b3.jar
                            ant-launcher-1.7.0.jar
                            ant-launcher-1.7.1.jar
                            antlr-2.7.5.jar
                            antlr-2.7.5H3.jar
                            asm-1.4.3.jar
                            avalon-framework-impl-4.2.0.jar
                            barbecue-1.5-beta1.jar
                            barcode4j-2.0.jar
                            batik-anim-1.7.jar
                            batik-awt-util-1.7.jar
                            batik-bridge-1.7.jar
                            batik-css-1.7.jar
                            batik-dom-1.7.jar
                            batik-ext-1.7.jar
                            batik-gvt-1.7.jar
                            batik-js-1.7.jar
                            batik-parser-1.7.jar
                            batik-script-1.7.jar
                            batik-svg-dom-1.7.jar
                            batik-svggen-1.7.jar
                            batik-util-1.7.jar
                            batik-xml-1.7.jar
                            bcmail-jdk14-1.38.jar
                            bcmail-jdk14-138.jar
                            bcprov-jdk14-1.38.jar
                            bcprov-jdk14-138.jar
                            bctsp-jdk14-1.38.jar
                            bsh-2.0b4.jar
                            cglib-2.0.2.jar
                            cobertura-1.9.4.1.jar
                            commons-beanutils-1.8.3.jar
                            commons-collections-3.2.1-javadoc.jar
                            commons-collections-3.2.1-sources.jar
                            commons-collections-3.2.1.jar
                            commons-dbcp-1.4-javadoc.jar
                            commons-dbcp-1.4-sources.jar
                            commons-dbcp-1.4.jar
                            commons-digester-2.1-javadoc.jar
                            commons-digester-2.1-sources.jar
                            commons-digester-2.1.jar
                            commons-logging-1.1.1.jar
                            commons-pool-1.5.4.jar
                            dom4j-1.6.1.jar
                            ehcache-1.1.jar
                            groovy-1.7.6.jar
                            groovy-all-1.7.5.jar
                            hibernate-3.0.5.jar
                            icu4j-2.6.1.jar
                            iText-2.1.5.jar
                            itext-2.1.7.jar
                            ivy.jar
                            jasperreports-3.7.6-javadoc.jar
                            jasperreports-3.7.6-sources.jar
                            jasperreports-3.7.6.jar
                            jasperreports-fonts-3.7.6.jar
                            jasperreports-javaflow-3.7.6.jar
                            jaxen-1.1.1.jar
                            jcommon-1.0.15.jar
                            jcommon-1.0.16.jar
                            jdom-1.0.jar
                            jdtcore-3.1.0.jar
                            jfreechart-1.0.12.jar
                            jfreechart-1.0.13-experimental.jar
                            jfreechart-1.0.13-swt.jar
                            jfreechart-1.0.13.jar
                            js-1.7R1.jar
                            jtds-1.2.4.jar
                            junit-4.8.1-sources.jar
                            junit-4.8.1.jar
                            jxl-2.6.10.jar
                            log4j-1.2.14-sources.jar
                            log4j-1.2.14.jar
                            log4j-1.2.16.jar
                            persistence-api-1.0.jar
                            png-encoder-1.5.jar
                            poi-3.6-sources.jar
                            poi-3.6.jar
                            saaj-api-1.3.jar
                            serializer-2.7.1.jar
                            servlet-api-2.2.jar
                            servlet-api-2.3.jar
                            servlet-api-2.5.jar
                            spring-aop-3.0.0.RELEASE.jar
                            spring-asm-3.0.0.RELEASE.jar
                            spring-beans-3.0.0.RELEASE.jar
                            spring-context-3.0.0.RELEASE.jar
                            spring-core-3.0.0.RELEASE.jar
                            spring-expression-3.0.0.RELEASE.jar
                            spring-jdbc-3.0.0.RELEASE-sources.jar
                            spring-jdbc-3.0.0.RELEASE.jar
                            spring-orm-3.0.0.RELEASE.jar
                            spring-test-3.0.0.RELEASE-sources.jar
                            spring-test-3.0.0.RELEASE.jar
                            spring-tx-3.0.0.RELEASE.jar
                            spring-web-3.0.0.RELEASE.jar
                            spring-webmvc-3.0.0.RELEASE.jar
                            spring-webmvc-portlet-3.0.0.RELEASE.jar
                            xalan-2.7.1.jar
                            xercesImpl-2.6.2.jar
                            xml-apis-1.3.02.jar
                            xml-apis-ext-1.3.04.jar
                            xmlParserAPIs-2.6.2.jar
                            xom-1.0.jar
                            I'd attach a war file, but the company I work for won't allow it, even though the app contains no real logic (and doesn't work )

                            Thanks everyone in advance!

                            Comment


                            • #15
                              Resolved

                              www.paulchurchward.com/tutorials/jasper/chart

                              Comment

                              Working...
                              X