UReport2 報表存儲

2022-09-07 15:45 更新
UReport2教學(xué)視頻http://pan.baidu.com/s/1boWTxF5,密碼:98hj

報表存儲目錄配置

       在第二小節(jié)中,我們介紹如何搭建一個包含UReport2的項目,在運行項目后,我們發(fā)現(xiàn)在項目的WEB-INF目錄有會自動生成了一個名為“ureportfiles”目錄,這個目錄是UReport2提供的默認(rèn)的報表文件存儲目錄,也就是說默認(rèn)情況下,UReport將在項目的WEB-INF/ureportfiles目錄下存儲設(shè)計好的報表文件。

默認(rèn)報表存儲目錄如果您的項目在Eclipse的開發(fā)環(huán)境運行時,采用的是jetty(比如run-jetty-run插件),那么就可以在項目的WEB-INF目錄下發(fā)現(xiàn)一個名為“ureportfiles”目錄。但如果你采用tomcat運行項目,那么在WEB-INF目錄下就沒有一個名為“ureportfiles”目錄,原因是在Eclipse中運行tomcat,tomcat需要創(chuàng)建一個臨時的工作目錄(該目錄一般位于workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\下),所以采用tomcat運行項目,則需要到這個臨時的工作目錄下找到對應(yīng)的項目,再到這個項目的WEB-INF目錄下找到對應(yīng)的“ureportfiles”目錄。

運行我們搭建好的項目,打開報表設(shè)計器,點擊工具欄上的保存按鈕(),選擇“保存”,可以看到如下圖所示的彈出窗口:

save-dialog\

  在這個窗口,我們只要輸入報表名稱,同時再選擇報表的存儲目的地就,可以保存當(dāng)前報表文件??梢钥吹剑琔Report2默認(rèn)給我們提供的存儲目的地是“服務(wù)器文件系統(tǒng) ”,實際上就是我們項目中WEB-INF目錄下的“ureportfiles”目錄,這個目錄是系統(tǒng)默認(rèn)自動生成的,如果需要我們可以添加一個屬性來更改這個目錄位置。

       在我們項目的WEB-INF目錄下創(chuàng)建一個名為config.properties,打開位于WEB-INF下名為context.xml的spring配置文件,在其中添加如下Bean以加載這個config.properties文件(如果我們的項目中已有自己的Properties文件,那么直接在這個Properties文件中配置即可)。

<bean id="propertyConfigurer" parent="ureport.props">
    <property name="location">
        <value>/WEB-INF/config.properties</value>
    </property>
</bean>
說明上面的Bean配置,實際上就是一個標(biāo)準(zhǔn)的org.springframework.beans.factory.config.PropertyPlaceholderConfigurer類配置,我們知道通過配置PropertyPlaceholderConfigurer,可以實現(xiàn)加載Spring外部properties的作用,所以如果您的項目中已配置了PropertyPlaceholderConfigurer類,那么直接在這個類對應(yīng)的properties文件中添加對應(yīng)的UReport2屬性即可,而不需要額外配置config.properties文件。

       打開config.properties文件,在其中添加名為“ureport.fileStoreDir”的屬性,該屬性的值用于定義UReport2中提供的默認(rèn)基于文件系統(tǒng)的報表存儲目錄,比如定義該屬性為下面樣式:

屬性值定義示例ureport.fileStoreDir=D:/ureportfiles

       表示在D盤根下名為ureportfiles的目錄中存儲報表文件,需要注意的是,這里指定特定目錄時,一定要保存這個目錄已存在,否則將不會被采用,比如上面的D盤下名為ureportfiles的目錄,就需要我們預(yù)先創(chuàng)建好。

自定義報表存儲器

       UReport2默認(rèn)提供的名為“服務(wù)器文件系統(tǒng)”的報表存儲機制,實際上是實現(xiàn)了UReport2提供的com.bstek.ureport.provider.report.ReportProvider接口,該接口源碼如下:

package com.bstek.ureport.provider.report;
import java.io.InputStream;
import java.util.List;
/**
 * @author Jacky.gao
 * @since 2016年12月4日
 */
public interface ReportProvider {
    /**
     * 根據(jù)報表名加載報表文件
     * @param file 報表名稱
     * @return 返回的InputStream
     */
    InputStream loadReport(String file);
    /**
     * 根據(jù)報表名,刪除指定的報表文件
     * @param file 報表名稱
     */
    void deleteReport(String file);
    /**
     * 獲取所有的報表文件
     * @return 返回報表文件列表
     */
    List<ReportFile> getReportFiles();
    /**
     * 保存報表文件
     * @param file 報表名稱
     * @param content 報表的XML內(nèi)容
     */
    void saveReport(String file,String content);
    /**
     * @return 返回存儲器名稱
     */
    String getName();
    /**
     * @return 返回是否禁用
     */
    boolean disabled();
    /**
     * @return 返回報表文件名前綴
     */
    String getPrefix();
}

       實現(xiàn)了ReportProvider接口后,只需要將實現(xiàn)類配置到Spring中,讓其成為一個標(biāo)準(zhǔn)的Spring Bean,這樣UReport2就會檢測到它而將其加載。下面是UReport2默認(rèn)提供的名為“服務(wù)器文件系統(tǒng)”的報表存儲器源碼:

package com.bstek.ureport.provider.report.file;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import javax.servlet.ServletContext;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.web.context.WebApplicationContext;
import com.bstek.ureport.exception.ReportException;
import com.bstek.ureport.provider.report.ReportFile;
import com.bstek.ureport.provider.report.ReportProvider;
/**
 * @author Jacky.gao
 * @since 2017年2月11日
 */
public class FileReportProvider implements ReportProvider,ApplicationContextAware{
    private String prefix="file:";
    private String fileStoreDir;
    private boolean disabled;
    @Override
    public InputStream loadReport(String file) {
        if(file.startsWith(prefix)){
            file=file.substring(prefix.length(),file.length());
        }
        String fullPath=fileStoreDir+"/"+file;
        try {
            return new FileInputStream(fullPath);
        } catch (FileNotFoundException e) {
            throw new ReportException(e);
        }
    }
     
    @Override
    public void deleteReport(String file) {
        if(file.startsWith(prefix)){
            file=file.substring(prefix.length(),file.length());
        }
        String fullPath=fileStoreDir+"/"+file;
        File f=new File(fullPath);
        if(f.exists()){
            f.delete();
        }
    }
    @Override
    public List<ReportFile> getReportFiles() {
        File file=new File(fileStoreDir);
        List<ReportFile> list=new ArrayList<ReportFile>();
        for(File f:file.listFiles()){
            Calendar calendar=Calendar.getInstance();
            calendar.setTimeInMillis(f.lastModified());
            list.add(new ReportFile(f.getName(),calendar.getTime()));
        }
        Collections.sort(list, new Comparator<ReportFile>(){
            @Override
            public int compare(ReportFile f1, ReportFile f2) {
                return f2.getUpdateDate().compareTo(f1.getUpdateDate());
            }
        });
        return list;
    }
    @Override
    public String getName() {
        return "服務(wù)器文件系統(tǒng)";
    }
     
    @Override
    public void saveReport(String file,String content) {
        if(file.startsWith(prefix)){
            file=file.substring(prefix.length(),file.length());
        }
        String fullPath=fileStoreDir+"/"+file;
        FileOutputStream outStream=null;
        try{
            outStream=new FileOutputStream(new File(fullPath));
            IOUtils.write(content, outStream,"utf-8");
        }catch(Exception ex){
            throw new ReportException(ex);
        }finally{
            if(outStream!=null){
                try {
                    outStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
         
    }
    @Override
    public boolean disabled() {
        return disabled;
    }
     
    public void setDisabled(boolean disabled) {
        this.disabled = disabled;
    }
     
    public void setFileStoreDir(String fileStoreDir) {
        this.fileStoreDir = fileStoreDir;
    }
     
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        File file=new File(fileStoreDir);
        if(file.exists()){
            return;
        }
        if(applicationContext instanceof WebApplicationContext){
            WebApplicationContext context=(WebApplicationContext)applicationContext;
            ServletContext servletContext=context.getServletContext();
            String basePath=servletContext.getRealPath("/");
            fileStoreDir=basePath+fileStoreDir;
            file=new File(fileStoreDir);
            if(!file.exists()){
                file.mkdirs();
            }
        }
    }
    @Override
    public String getPrefix() {
        return prefix;
    }
}
禁用系統(tǒng)提供的默認(rèn)報表存儲器 如果我們定義了自己的報表存儲器,同時又不想再使用系統(tǒng)默認(rèn)提供的”服務(wù)器文件系統(tǒng)“的報表存儲器,那么我們只需要在之前介紹的config.properties文件中添加一個名為ureport.disableFileProvider屬性,將其值設(shè)置成true即可。

       通過上面的介紹,可以看到,通過實現(xiàn)ReportProvider接口,我們可以很容易的開發(fā)出其它類型的報表存儲器,比如開發(fā)一個新的報表存儲器將報表文件存儲到數(shù)據(jù)庫、FTP等。

以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號