2016-04-08 9 views
1

this guideの後に、struts2 jasper reports pluginを使用してPDFをコンパイルして作成するが、WEB_APP/report.jrxmlとは別のパスからreport.jrxmlをロードする必要がある。Struts2 + jasperプラグイン、ロケーションベースパスを変更する

<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

これは私の行動の結果です。

ofcourseの私は、このエラーが表示されます。

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> 

答えて

0

私は解決策を見つけた、とここで私が実装したコードは(私は例えば無用だと思うので、私は、データ・ソースの一部を省略しました)です:

Actionクラス

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"はアプリケーションがデプロイされている各プロダクション・サーバーは時間の経過とともに変更される可能性があります)。

関連する問題