【问题标题】:Struts2 + jasper plugin, change location base pathStruts2 + jasper 插件,更改位置基本路径
【发布时间】:2016-04-11 17:31:02
【问题描述】:

我正在关注this guide,以便使用struts2 jasper reports plugin 编译和创建PDF,但我必须从不同于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

如何更改“基本路径”?我应该更好地配置这个依赖吗?

<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>

【问题讨论】:

    标签: struts2 jasper-reports


    【解决方案1】:

    我找到了解决方案,下面是我实现的代码(我省略了数据源部分,因为我认为对示例没有用处):

    动作类

    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”可能会有所不同在部署应用程序的每个生产服务器中,并且可能会随着时间的推移而更改)。

    【讨论】:

      猜你喜欢
      • 2023-02-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-03
      • 2023-03-10
      相关资源
      最近更新 更多