Struts2 jasperreports - passing export parameters

2019-05-24 20:59发布

问题:

I'm using the struts2 jasperreports plugin and it works well. The problem is that I want to pass exportParameters and I'm not sure how to do that through the plugin.

回答1:

Which version of Struts2 you are using starting with 2.1.2+ it provides the feature to provides exportParameters

All you need to add following entry or similar entry in your struts config file inside your action class

<action name="myJasperTest" class="com.acme.test.action.JasperAction">
    <result name="success" type="jasper">
      <param name="location">foo.jasper</param>
      <param name="dataSource">mySource</param>
      <param name="exportParameters ">exportParameters </param>
    </result>
</action>

exportParameters - OGNL expression used to retrieve a map of JR exporter parameters from the value stack. The export parameters are used to customize the JR export. For example, a PDF export might enable encryption and set the user password to a string known to the report creator. All you need to define a map for your export parameters in your action class and provides its getter/setter than use its reference as described.

For details refer this URL

jasperreports

**Update**

Here is how they have done it in result type

 exporter = new JRXlsExporter();
Map exportParams = (Map) stack.findValue(exportParameters);
             if (exportParams != null) {
                 LOG.debug("Found export parameters; adding to exporter parameters...");
                 exporter.getParameters().putAll(exportParams);
            }

so what they did is they tried to find out a map in value stack with name exportParameters if they find it they are adding it.So have to do this in your action class

Map<String,String> exportParameters= //init your map here

set your properties in this map and create a getter and setter for this property

getExportParameters()
setExportParameters()

and in your struts config file declare the map as follows

<param name="exportParameters ">exportParameters </param>

rest framework will take care Hope this will help you