Announcement Announcement Module
Collapse
No announcement yet.
JasperReports: filename using JasperReportsMultiFormatView Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • JasperReports: filename using JasperReportsMultiFormatView

    Hi,
    I'm using JasperReportsMultiFormatView to generate an excel report, but I'd like to give it a name of my choice (for example, report1.xls, report2.xls, ecc...).

    my spring-views.properties looks like:

    Code:
    ...
    reportProva.class=org.springframework.web.servlet.view.jasperreports.JasperReportsMultiFormatView
    reportProva.url=/jreports/prova1.jasper
    ...
    My controller returns a ModelAndView like this:

    Code:
    return new ModelAndView("reportProva", model);
    How could I generate a new name in the controller for the report?

    Thanks,
    Luigi

  • #2
    Luigi,

    If you are using just a single format you should stick with the view class for that format and then map the request URI to the filename you want to use.

    If you want to use the multi format view, you need to setup mappings for each format to a content-disposition header that contains the filename. These mappings can be configured using the contentDispositionMappings property. A content disposition header looks something like this: inline; filename=report.xls.

    Rob

    Comment


    • #3
      Thanks Rob,
      I found the following snippet at the url http://opensource.atlassian.com/proj...browse/SPR-605 :

      Code:
              <property name="contentDispositions">
                  <props>
                      <prop key="html">inline; filename=report.html</prop>
                      <prop key="pdf">inline; filename=report.pdf</prop>
                      <prop key="xls">inline; filename=report.xls</prop>
                      <prop key="xml">inline; filename=report.xml</prop>
                      <prop key="ser">attachment; filename=report.ser</prop>
                  </props>
              </property>
      I think I should put something like this in my spring-views.properties, right?
      If it is so, should I write something like (I don't know how to set a property that is a map):

      Code:
      ...
      reportProva.class=org.springframework.web.servlet.view.jasperreports.JasperReportsMultiFormatView
      reportProva.url=/jreports/prova1.jasper
      reportProva.contentDispositionMappings=&#40;&#40;"xls","inline; filename=report.xls"&#41;,&#40;"html","inline; filename=report.html"&#41;&#41;
      ...
      And what if I want to specify the filename at runtime (not in a configuration file)?

      Thanks for your help,
      and thanks to Rod and all the team for this wonderful framework!

      Luigi

      [/i]

      Comment


      • #4
        Luigi,

        The current version of Spring does not support configuration of the content-disposition mappings through the properties file - you need to use the XML mechanism instead.

        Rob

        Comment


        • #5
          News?

          And what about now?

          Is it supported?

          Can we specify the filename at runtime?

          Thanks for your answers.

          Comment


          • #6
            It can be done like this.

            In the mapping file map the multiFormatView bean as follows

            Code:
            <bean id="jasperReportsMultiFormatView" name="jasperReportsMultiFormatViewBean" class="mypackage.CustomJasperReportsMultiFormatView" singleton="false">
                    
                    <!-- The value _rep_name_ will be replaced with the report name -->
                    <property name="contentDispositionMappings">
                        <props>
                            <prop key="html">attachment; filename=_rep_name_.html</prop>
                            <prop key="pdf">attachment; filename=_rep_name_.pdf</prop>
                            <prop key="xls">attachment; filename=_rep_name_.xls</prop>
                            <prop key="xml">attachment; filename=_rep_name_.xml</prop>
                            <prop key="csv">attachment; filename=_rep_name_.csv</prop>
                        </props>
                    </property>
                </bean>
            Where the CustomJasperReportsMultiFormatView is a subclass of JasperReportsMultiFormatView.

            in the CustomJasperReportsMultiFormatView class overide the renderReport() method. as follows.

            Code:
            protected void renderReport(JasperPrint populatedReport, Map model, HttpServletResponse response) throws Exception {
                    super.renderReport(populatedReport,model,response);
                    
                    // replace content disposition header filename with the report names.
                    Properties contentDispositions = this.getContentDispositionMappings();
                    
                    Enumeration enumContDispKeys = contentDispositions.keys();
                    // iterate over all disposition mappings and replace the word _rep_name_ with the reportName
                    while(enumContDispKeys.hasMoreElements()){
                        Object contDispKey = enumContDispKeys.nextElement();
                        // check whether string before cast.
                        if(contDispKey instanceof String){
                            // get the disposition string
                            String dispositionStr = contentDispositions.getProperty((String)contDispKey);
                            // set the new value in the properties
                            contentDispositions.setProperty((String)contDispKey,dispositionStr.replace("_rep_name_",populatedReport.getName()));
                        }
                    }
                }
            It works.

            Comment


            • #7
              overriding RenderReport

              Thanks so much for posting this info. Hugely helpful! But I found that in order to make it work, I needed to move the call "super.renderReport" to the end of the overridden renderReport method. That is, first muck with the content disposition, then render the report. Otherwise the report name doesn't get updated. (I'm using springframework 2.5.6 and JasperReports 3.6.2.)

              Since the post I'm replying to is over two years old, I'm replying mainly for the benefit of others who find this topic via Google.

              Walter Gillett

              Comment

              Working...
              X