Announcement Announcement Module
Collapse
No announcement yet.
jasperreport and data source Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • jasperreport and data source

    Dear All,

    I'm a newbie with spring.
    I have set up the CVS jasperdemo working correctly. The demo uses "databeans" to populate the jasperreports, but what I want to do is access the data in a rdbms for compiled jasperreports.

    I have tried to set up the datasource in this matter:


    Code:
    public ModelAndView handleClientReportMulti(HttpServletRequest request,
       HttpServletResponse response) throws Exception {
    
    // DB ACCESS  
         BasicDataSource ds = new BasicDataSource (); 
           ds.setDriverClassName ("oracle.jdbc.driver.OracleDriver"); 
           ds.setUrl ("jdbc:oracle:thin:@localhost:1521:orcl"); 
           ds.setUsername ("user"); 
           ds.setPassword ("paswd"); 
      
      ctx.getBean("dataSource",DataSource.class);
      String uri = request.getRequestURI();
      String format = uri.substring(uri.lastIndexOf(".") + 1);
      Map model = new HashMap();
      model.put("ReportTitle", "INFO");
      model.put("format", format);
      model.put("Id", "310102255101");
      model.put("dataSource","ds");
    
    
      return new ModelAndView("clientReportMulti", model);
     }
    The error states :
    Code:
    java.lang.IllegalArgumentException: No report data source found in model and no [javax.sql.DataSource] specified in configuration
     org.springframework.web.servlet.view.jasperreports.AbstractJasperReportsView.fillReport(AbstractJasperReportsView.java:555)
     org.springframework.web.servlet.view.jasperreports.AbstractJasperReportsView.renderMergedOutputModel(AbstractJasperReportsView.java:506)
     org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:250)
     org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:928)
     org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:705)
     org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:625)
     org.springframework.web.servlet.FrameworkServlet.serviceWrapper(FrameworkServlet.java:386)
     org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:346)
     javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
     javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
    The point I do not understand is how to tell to a compiled jasper report from where to fetch the data...

    Thanks in advance...

  • #2
    If you want to use a DataSource to populate your report then inject it into the report using the jdbcDataSource property and don't pass any data source in the model.

    Rob

    Comment


    • #3
      I greatly appreciate your prompt respond and help on the subject matter.

      The base for my test is the CVS jasperdemo. Regarding to it I'm still trying to grasp the idea where/how to inject the dataSource:

      1) Should the point of injection be the WebApplicationContext or

      2) Should I initialize a separate application context containing the dataSource ("middle-tier service") ? and

      3) What kind of Injection should I use: Setter or Constructor ?

      4) How the dataSource will bet set in the Controller ?

      Comment


      • #4
        Dear All,

        I have been trying to set up the connexion with a RDBM, but still I do not understand how.

        ..inject it into the report using the jdbcDataSource property
        I'm confused here ..I haven't found any info over jdbcDataSource and how to use it!

        I would be very gratefull if somebody could give me a hint how to proceed. Essentially my problem is how to pass the information to jasperengine that the data should be fetched from RDBM.
        What kind of object should it be: JRDataSource, JRResultSetDataSource ...jdbcDataSource ?

        The jasperdemo is very clear when using static data. It would be nice to have in the referece manual an example how to handle RDBMS connexions as model source for compiled jasper files.


        Best regards,

        Comment


        • #5
          Originally posted by ari
          Dear All,

          I have been trying to set up the connexion with a RDBM, but still I do not understand how.

          ..inject it into the report using the jdbcDataSource property
          I'm confused here ..I haven't found any info over jdbcDataSource and how to use it!

          I would be very gratefull if somebody could give me a hint how to proceed. Essentially my problem is how to pass the information to jasperengine that the data should be fetched from RDBM.
          What kind of object should it be: JRDataSource, JRResultSetDataSource ...jdbcDataSource ?

          The jasperdemo is very clear when using static data. It would be nice to have in the referece manual an example how to handle RDBMS connexions as model source for compiled jasper files.


          Best regards,
          Hi,
          my experience: I implemted the JRDatasource interface; I created a DAO object, injected a jdbcTemplate to it(through the appcontext.xml), so that DAO impolemented the JRDatasource. All I needed to implement the hasNext() and next() methods.
          I mean, you do an sql count query, store it in an instance varible, and use it int the hasNext, next methods.

          Hope this helped you, if not, drop me an email to gal(at)aura.cz

          Comment


          • #6
            If you have a statically defined query inside the report then you need to pass in a JDBC DataSource. This DataSource can be configured on Spring's Jasper Reports view class using the [jdbcDataSource] property.

            Rob

            Comment


            • #7
              Thanks for the help..it's working now...

              Comment


              • #8
                With Velocity

                Hello, I am too having a similiar problem but slightly different. I am using velocity and have setup 2 viewResolvers ... 1 for Velocity and 1 for JasperReports

                <bean id="velocityViewResolver" class="org.springframework.web.servlet.view.veloci ty.VelocityViewResolver">
                <property name="cache" value="true" />
                <property name="prefix" value="" />
                <property name="suffix" value=".vm" />
                </bean>

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

                Then in my views.properties file I have setup a report and tried to inject the value of jdbcDataSource (same as reportDataKey) via my model by configuring it like so,:

                stock_available_on_hand.class=org.springframework. web.servlet.view.jasperreports.JasperReportsHtmlVi ew
                stock_available_on_hand.url=/WEB-INF/reports/stock_available_on_hand.jrxml
                stock_available_on_hand.jdbcDataSource=jdbcConnect ion

                This does not work however, the exception I get is:

                org.springframework.beans.TypeMismatchException: Failed to convert property value of type [java.lang.String] to required type [javax.sql.DataSource] for property 'jdbcDataSource'

                Which makes sense as it is a String. So how do I inject this property inside my Controller where I return the ModelAndView. I can't work out how to access or configure the View.

                Thanks in advance
                Eban

                Comment


                • #9
                  Took me 2 days to figure out how to inject a datasource bean for jdbcDataSource property in a views.properties file :

                  myview.(class)=org.springframework.web.servlet.vie w.jasperreports.JasperReportsPdfView
                  myview.url=/WEB-INF/reports/myreport.jasper
                  myview.jdbcDataSource(ref)=dataSource

                  where dataSource is the ID of a DataSource bean defined in appContext.xml

                  (from thread : http://forum.springframework.org/sho...ews.properties)

                  Hope this helps ...

                  Comment


                  • #10
                    Originally posted by laurentJ View Post
                    myview.jdbcDataSource(ref)=dataSource

                    where dataSource is the ID of a DataSource bean defined in appContext.xml
                    Hope this helps ...
                    Yep, sure did. I'd been struggling with this one for a while until I added the (ref) text.

                    Comment


                    • #11
                      Yes, right. ref is important. I successfully use my jrxml file. Thanks a lot. For sharing, let me share my codes here.

                      Very simple controller:
                      Code:
                      package web;
                      
                      import org.springframework.web.servlet.mvc.Controller;
                      import org.springframework.web.servlet.ModelAndView;
                      
                      import javax.servlet.ServletException;
                      import javax.servlet.http.HttpServletRequest;
                      import javax.servlet.http.HttpServletResponse;
                      import javax.sql.DataSource;
                      
                      import java.io.IOException;
                      import java.util.Map;
                      import java.util.HashMap;
                      
                      import org.apache.commons.logging.Log;
                      import org.apache.commons.logging.LogFactory;
                      
                      public class SimpleReportController implements Controller {
                      
                          /** Logger for this class and subclasses */
                          protected final Log logger = LogFactory.getLog(getClass());
                      
                          public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
                                  throws ServletException, IOException {
                      
                              String now = (new java.util.Date()).toString();
                              logger.info("returning simpleReport view with " + now);
                      
                              Map myModel = new HashMap();      
                              // myModel.put("dataSource", this.ds);
                      
                              return new ModelAndView("simpleReport", myModel);
                          }
                      }
                      view.properties
                      Code:
                      simpleReport.class = org.springframework.web.servlet.view.jasperreports.JasperReportsPdfView
                      simpleReport.url = /WEB-INF/reports/SimpleReport.jrxml
                      simpleReport.jdbcDataSource(ref) = dataSource

                      Comment


                      • #12
                        same problem but no solution stilll

                        web.xml
                        Code:
                        <?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">
                            <listener>
                                <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
                            </listener>
                            <servlet>
                                <servlet-name>HelloSpring</servlet-name>
                                <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
                                <load-on-startup>1</load-on-startup>
                            </servlet>
                            <servlet-mapping>
                                <servlet-name>HelloSpring</servlet-name>
                                <url-pattern>*.pdf</url-pattern>
                            </servlet-mapping>
                            <session-config>
                                <session-timeout>
                                    30
                                </session-timeout>
                            </session-config>
                            <welcome-file-list>
                                <welcome-file>
                                    index.jsp
                                </welcome-file>
                            </welcome-file-list>
                        </web-app>
                        HelloSpring-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">
                        
                        <beans>
                        
                            <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
                                <property name="mappings">
                                    <props>
                                    	<prop key="/HelloSpringController.pdf">HelloSpringController</prop>
                        
                                    </props>
                                </property>
                            </bean>
                            <bean id = "HelloSpringController" class="HelloSpringController" />
                             <bean id="viewResolver"  class="org.springframework.web.servlet.view.ResourceBundleViewResolver">
                                <property name="basename" value="HelloSpring"/>
                            </bean>
                            
                        
                        </beans>

                        Comment


                        • #13
                          ..

                          SpringColtroler.java
                          Code:
                          import java.io.ByteArrayOutputStream;
                          import java.io.IOException;
                          import java.sql.SQLException;
                          import java.util.HashMap;
                          import java.util.Map;
                          import javax.servlet.ServletContext;
                          import javax.servlet.ServletException;
                          import javax.servlet.ServletOutputStream;
                          import javax.servlet.http.HttpServletRequest;
                          import javax.servlet.http.HttpServletResponse;
                          import javax.sql.DataSource;
                          import net.sf.jasperreports.engine.JasperCompileManager;
                          import net.sf.jasperreports.engine.JasperFillManager;
                          import net.sf.jasperreports.engine.JasperPrint;
                          import net.sf.jasperreports.engine.JasperReport;
                          import org.springframework.web.context.ServletContextAware;
                          import org.springframework.web.servlet.ModelAndView;
                          import org.springframework.web.servlet.mvc.AbstractController;
                          import org.springframework.web.servlet.mvc.Controller;
                          import org.springframework.web.servlet.view.jasperreports.AbstractJasperReportsSingleFormatView;
                          
                          public class HelloSpringController implements Controller {
                          Map hm ;
                              public HelloSpringController() {
                              }
                          
                              @Override
                              public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse arg1) throws Exception {
                                  System.out.println("In My Controller");
                          
                                  String uri = request.getRequestURI();
                                  String format = uri.substring(uri.lastIndexOf(".") + 1);
                          
                                  hm = new HashMap();
                                  hm.put("username", format);
                          
                                  return (new ModelAndView("simpleReport",hm));
                          
                              }
                           
                           
                          
                          
                          }
                          HelloSpring.properties
                          Code:
                          # To change this template, choose Tools | Templates
                          # and open the template in the editor.
                          
                           
                          simpleReport.(class)=org.springframework.web.servlet.view.jasperreports.JasperReportsPdfView
                          simpleReport.url=/WEB-INF/reports/report2.jasper

                          Comment


                          • #14
                            problem

                            problem is i am getting empty pdf.
                            jrxml for pdf
                            Code:
                            <?xml version="1.0" encoding="UTF-8"?>
                            
                            <jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="report name" pageWidth="595" pageHeight="842" columnWidth="535" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
                            
                            	<parameter name="username" class="java.lang.String"/>
                            
                            	<background>
                            
                            		<band/>
                            
                            	</background>
                            
                            	<title>
                            
                            		<band height="79">
                            
                            			<staticText>
                            
                            				<reportElement x="235" y="46" width="100" height="20"/>
                            
                            				<textElement>
                            
                            					<font size="14" isBold="true"/>
                            
                            				</textElement>
                            
                            				<text><![CDATA[***]]></text>
                            
                            			</staticText>
                            
                            		</band>
                            
                            	</title>
                            
                            	<pageHeader>
                            
                            		<band height="35"/>
                            
                            	</pageHeader>
                            
                            	<columnHeader>
                            
                            		<band height="59"/>
                            
                            	</columnHeader>
                            
                            	<detail>
                            
                            		<band height="125">
                            
                            			<staticText>
                            
                            				<reportElement x="158" y="62" width="100" height="20"/>
                            
                            				<textElement/>
                            
                            				<text><![CDATA[Hello ]]></text>
                            
                            			</staticText>
                            
                            			<textField hyperlinkType="None">
                            
                            				<reportElement x="284" y="62" width="100" height="20"/>
                            
                            				<textElement/>
                            
                            				<textFieldExpression class="java.lang.String"><![CDATA[$P{username}]]></textFieldExpression>
                            
                            			</textField>
                            
                            		</band>
                            
                            	</detail>
                            
                            	<columnFooter>
                            
                            		<band height="45"/>
                            
                            	</columnFooter>
                            
                            	<pageFooter>
                            
                            		<band height="54"/>
                            
                            	</pageFooter>
                            
                            	<summary>
                            
                            		<band height="42"/>
                            
                            	</summary>
                            
                            </jasperReport>

                            Comment

                            Working...
                            X