2016-04-08 5 views
1

Ich folge this guide um eine PDF zu kompilieren und zu erstellen mit struts2 jasper reports plugin aber ich muss die report.jrxml von einem anderen Pfad als WEB_APP/report.jrxml laden.Struts2 + jasper plugin, Standort ändern Basispfad

Das ist mein Aktionsergebnis:

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

wo ${location} == /my/absolute/path.

ofcourse erhalte ich diesen Fehler:

javax.servlet.ServletException: java.io.FileNotFoundException: WEB_APP/my/absolute/path/report.jasper 

Wie kann ich den "Basispfad" ändern? Sollte ich diese Abhängigkeit besser konfigurieren?

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

Antwort

0

ich die Lösung gefunden haben, und hier ist der Code, den ich implementiert haben (ich habe die Datenquelle Teil weggelassen, weil ich denke, für das Beispiel nutzlos ist):

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

Mit dieser Lösung kann ich meine .jrxml Dateien aus einem bestimmten Verzeichnis auf meinem Server bekommen, wo ein Benutzer die Datei hochladen kann, ohne jedes Mal die Anwendung zu installieren (in meinem Fall ist das sehr nützlich, da "report.jrxml" anders sein kann) jeder Produktionsserver, auf dem die Anwendung bereitgestellt wird und im Laufe der Zeit geändert werden kann).