JasperReport requires (by default) that images be in "WEB-INF/classes/". I'd like to share images between PDFs and normal JSP pages. I'd rather not clutter up classpath with garbage image files. How do I force JR to use a different 开发者_StackOverflow社区location for images?
In my book, Indispensable, I recommend creating the following report parameters:
- ROOT_DIR --
"/reports/"
- IMAGE_DIR --
$P{ROOT_DIR} + "images/"
- STYLE_DIR --
$P{ROOT_DIR} + "styles/"
- SUBREPORT_DIR --
$P{ROOT_DIR} + "subreports/"
- COMMON_DIR --
$P{SUBREPORT_DIR} + "common/"
This allows the images directory to be relative to the ROOT_DIR
path. It also allows you to change ROOT_DIR
dynamically. The parameters must be declared in their relative order.
In your case, using an absolute path:
- ROOT_DIR --
"/home/user/"
- IMAGE_DIR --
$P{ROOT_DIR} + "Pictures/"
Note that changing between operating systems, directory structures, environments (e.g., migrating to JasperReports Server from JasperReports & JSF) and so forth, can be accomplished without having to modify the report. (Well, some modifications are required for JasperReports Server.)
Following @Dave's advice I split out the base directory into a Parameter and gave it a default. That way I can specify either an absolute path, classpath or url. Here are the relevant pieces of code:
Controller:
modelMap.put("ROOT_DIR", "/srv/images/");
-Or-
modelMap.put("ROOT_DIR", "http://site.com/images/");
jrxml File:
<parameter name="ROOT_DIR" class="java.lang.String">
<defaultValueExpression><![CDATA["/home/user/Pictures/"]]></defaultValueExpression>
</parameter>
...
<imageExpression class="java.lang.String">
<![CDATA[$P{ROOT_DIR}+"leaf_banner_red.png"]]>
</imageExpression>
Upon further inspection, it doesn't seem like I can set a 'location' for images anywhere but I did find out that you can specify images 1 of 3 ways:
- Classpath: "image.png"
- Absolute: "/home/user/Pictures/image.png"
- Url: "https://encrypted.google.com/images/logos/ssl_ps_logo.png"
精彩评论