Struts2 + jasper 插件,更改位置基本路径
Struts2 + jasper plugin, change location base path
我正在关注 this guide in order to compile and create a PDF using struts2 jasper reports plugin,但我必须从与 WEB_APP/report.jrxml.
不同的路径加载 report.jrxml
这是我的操作结果:
<action name="jasper" class="web.app.controller.JasperAction">
<result name="success" type="jasper">
<param name="location">${location}</param>
<param name="dataSource">map</param>
<param name="format">PDF</param>
</result>
</action>
其中 ${location} == /my/absolute/path
.
当然我收到这个错误:
javax.servlet.ServletException: java.io.FileNotFoundException: WEB_APP/my/absolute/path/report.jasper
如何更改 "base path"?我是否应该更好地配置此依赖项?
<dependency>
<groupId>net.sf.jasperreports</groupId>
<artifactId>jasperreports</artifactId>
<version>${jasperreports.version}</version>
<type>jar</type>
<scope>compile</scope>
<exclusions>
<exclusion>
<artifactId>commons-collections</artifactId>
<groupId>commons-collections</groupId>
</exclusion>
</exclusions>
</dependency>
我找到了解决方案,这是我实现的代码(我省略了数据源部分,因为我认为这对示例没有用):
行动CLASS
public class JasperAction extends ActionSupport implements ServletContextAware {
private static final Logger LOG = LogManager.getLogger(JasperAction.class);
private ServletContext servletContext;
private String location = "report.jasper";
@Override
public String execute() throws Exception {
String jrxmlPath = "/my/path/to/report.jrxml";
String jasperPath = servletContext.getRealPath("") + File.separator + location;
try {
JasperCompileManager.compileReportToFile(jrxmlPath, jasperPath);
} catch (Exception e) {
LOG.error(e);
return ERROR;
}
return SUCCESS;
}
@Override
public void setServletContext(ServletContext servletContext) {
this.servletContext = servletContext;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
}
struts.xml
<action name="jasper" class="web.bkgd.simba.controller.JasperAction">
<result name="success" type="jasper">
<param name="location">${location}</param>
<param name="format">PDF</param>
</result>
</action>
通过这个解决方案,我可以从我服务器的特定目录中获取我的 .jrxml 文件,用户可以在该目录中上传文件,而无需在每次应用程序时都进行部署(在我的情况下,这非常有用,因为 "report.jrxml" 可能在部署应用程序的每个生产服务器中都不同,并且可能会随着时间的推移而改变)。
我正在关注 this guide in order to compile and create a PDF using struts2 jasper reports plugin,但我必须从与 WEB_APP/report.jrxml.
不同的路径加载report.jrxml
这是我的操作结果:
<action name="jasper" class="web.app.controller.JasperAction">
<result name="success" type="jasper">
<param name="location">${location}</param>
<param name="dataSource">map</param>
<param name="format">PDF</param>
</result>
</action>
其中 ${location} == /my/absolute/path
.
当然我收到这个错误:
javax.servlet.ServletException: java.io.FileNotFoundException: WEB_APP/my/absolute/path/report.jasper
如何更改 "base path"?我是否应该更好地配置此依赖项?
<dependency>
<groupId>net.sf.jasperreports</groupId>
<artifactId>jasperreports</artifactId>
<version>${jasperreports.version}</version>
<type>jar</type>
<scope>compile</scope>
<exclusions>
<exclusion>
<artifactId>commons-collections</artifactId>
<groupId>commons-collections</groupId>
</exclusion>
</exclusions>
</dependency>
我找到了解决方案,这是我实现的代码(我省略了数据源部分,因为我认为这对示例没有用):
行动CLASS
public class JasperAction extends ActionSupport implements ServletContextAware {
private static final Logger LOG = LogManager.getLogger(JasperAction.class);
private ServletContext servletContext;
private String location = "report.jasper";
@Override
public String execute() throws Exception {
String jrxmlPath = "/my/path/to/report.jrxml";
String jasperPath = servletContext.getRealPath("") + File.separator + location;
try {
JasperCompileManager.compileReportToFile(jrxmlPath, jasperPath);
} catch (Exception e) {
LOG.error(e);
return ERROR;
}
return SUCCESS;
}
@Override
public void setServletContext(ServletContext servletContext) {
this.servletContext = servletContext;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
}
struts.xml
<action name="jasper" class="web.bkgd.simba.controller.JasperAction">
<result name="success" type="jasper">
<param name="location">${location}</param>
<param name="format">PDF</param>
</result>
</action>
通过这个解决方案,我可以从我服务器的特定目录中获取我的 .jrxml 文件,用户可以在该目录中上传文件,而无需在每次应用程序时都进行部署(在我的情况下,这非常有用,因为 "report.jrxml" 可能在部署应用程序的每个生产服务器中都不同,并且可能会随着时间的推移而改变)。