在網(wǎng)上查了下BIRT的文章,不是很多。而且講述的也不是十分全面。還是得自己琢磨啊。
          剛實(shí)驗(yàn)好,貼上來(lái)分享一下共同交流。希望也能幫助到對(duì)API部署還困惑的人。
          做這個(gè)純粹是看官網(wǎng)上的例子,還有好多疑點(diǎn),希望高手能給我解答下。
          首先,這個(gè)API部署其實(shí)在官網(wǎng)上是叫"Servlet Example"。我沒(méi)有在網(wǎng)上找到中文的這個(gè)教程,所以我就以我這貧乏的英語(yǔ)水品來(lái)自己理解了官網(wǎng)上的步驟。下面直接開(kāi)始,我不是原文翻譯的所以有些地方用詞不當(dāng)也是正常。
          既然是個(gè)Servlet Exampl那么它肯定符合Java EE的規(guī)范。我先把目錄結(jié)構(gòu)貼到下面,讓大家先有個(gè)整體感覺(jué)。

          圖片不是很清楚,湊合著看吧。
          我先說(shuō)下目錄結(jié)構(gòu)Tomcat和Webapps就不說(shuō)了。下面就是自己新建一個(gè)自己的文件夾在tomcat/webapps下面是自己的API BIRT的項(xiàng)目。名字自己起就好了,官網(wǎng)上是叫WebReport吧,上面寫的很清楚了。我自己是叫TestAPI   
          ImagesReports是用來(lái)放報(bào)表的地方,Images是圖片和圖像報(bào)表,然后Reports是文字報(bào)表。就是把自己建好的報(bào)表copy到 Reports里面。具體建報(bào)表就不說(shuō)了,自帶的幫助文檔和網(wǎng)上有很多資料。下面就是WEB-INF目錄了,建立好這個(gè)目錄,你的API所有配置和用的資源才可以發(fā)動(dòng)。在WEB-INF下面的lib沒(méi)用過(guò)也知道是放jar的地方。還有一個(gè)platform也是放一些報(bào)表運(yùn)行所需的配置信息和一些jar。其實(shí)我們都知道上面圖其實(shí)應(yīng)該在WEB-INF下面還有個(gè)classes文件夾,用來(lái)放編譯好的java文件。這也是個(gè)重要的目錄。上面沒(méi)有畫。
          官網(wǎng)上是分了五步,我這里就按自己的來(lái)了,也不說(shuō)分步了。
          首先也是需要一個(gè)runtime從http://www.eclipse.org/downloads/download.php?file=/birt/downloads/drops/R-R1-2_1_3-200707051847/birt-runtime-2.1.3.zip你可以下載到這個(gè)“runtime”不是很大。下載好以后打開(kāi),然后進(jìn)入ReportEngine目錄把lib里的所有jar包全部拷貝到自己建立的lib下面,如圖



          然后開(kāi)始往platform里面塞東西。把runtime/ReportEngine的兩個(gè)文件夾(plugins和configuration)拷貝到你自己的platform里面。
          plugins里面也是一些報(bào)表所需的jar而configuration里面是什么東西?就是一個(gè)config.ini文件,我不知道是干什么用的。
          拷完之后如下:


          如果你的報(bào)表用到數(shù)據(jù)庫(kù)驅(qū)動(dòng)了,必須把數(shù)據(jù)庫(kù)驅(qū)動(dòng)文件拷貝到\WEB-INF\platform\plugins\org.eclipse.birt.report.data.oda.jdbc_2.1.1.v20070705-1847\drivers這個(gè)路徑下面,看著長(zhǎng)其實(shí)不難找。
          拷貝完是這樣,貼個(gè)圖:


          配置就差不多了,就查一個(gè)web.xml文件了。
          下面是比較重要的東西。有三個(gè)文件。是API的核心操作文件。

        1. BirtConfig.properties - Configuration properties for the Engine.
        2. BirtEngine.java - Class used to initialize the Report Engine.
        3. WebReport.java - The servlet that handles report generation on a GET command.
        4.  

          就是上面三個(gè)文件是官網(wǎng)的原文,我拷貝過(guò)來(lái)的。
          第一個(gè)是構(gòu)造報(bào)表引擎的資源文件。
          第二個(gè)用來(lái)初始化報(bào)表引擎的類。
          第三個(gè)用GET方法來(lái)調(diào)用報(bào)表。
          我英文不好,自己理解是這樣的。(四級(jí)我都沒(méi)考,我覺(jué)得找個(gè)美國(guó)人待上一個(gè)月英語(yǔ)水平就上去了)
          具體這三個(gè)文件的代碼我貼到下面
          BirtConfig.properties

          logDirectory=c:/temp
          logLevel
          =FINEST


          BirtEngine.java

          import java.io.InputStream;
          import java.io.IOException;
          import java.util.Properties;
          import java.util.logging.Level;

          import org.eclipse.birt.report.engine.api.EngineConfig;
          import org.eclipse.birt.report.engine.api.IReportEngine;
          import javax.servlet.*;
          import org.eclipse.birt.core.framework.PlatformServletContext;
          import org.eclipse.birt.core.framework.IPlatformContext;
          import  org.eclipse.birt.core.framework.Platform;
          import org.eclipse.birt.core.exception.BirtException;
          import org.eclipse.birt.report.engine.api.IReportEngineFactory;

          public class BirtEngine {

          private static IReportEngine birtEngine = null;

          private static Properties configProps = new Properties();

          private final static String configFile = "BirtConfig.properties";

          public static synchronized void initBirtConfig() {
           loadEngineProps();
          }


          public static synchronized IReportEngine getBirtEngine(ServletContext sc) {
           
          if (birtEngine == null
           
          {
            EngineConfig config 
          = new EngineConfig();
            
          if( configProps != null){
             String logLevel 
          = configProps.getProperty("logLevel");
             Level level 
          = Level.OFF;
             
          if ("SEVERE".equalsIgnoreCase(logLevel)) 
             
          {
              level 
          = Level.SEVERE;
             }
           else if ("WARNING".equalsIgnoreCase(logLevel))
             
          {
              level 
          = Level.WARNING;
             }
           else if ("INFO".equalsIgnoreCase(logLevel)) 
             
          {
              level 
          = Level.INFO;
             }
           else if ("CONFIG".equalsIgnoreCase(logLevel))
             
          {
              level 
          = Level.CONFIG;
             }
           else if ("FINE".equalsIgnoreCase(logLevel)) 
             
          {
              level 
          = Level.FINE;
             }
           else if ("FINER".equalsIgnoreCase(logLevel)) 
             
          {
              level 
          = Level.FINER;
             }
           else if ("FINEST".equalsIgnoreCase(logLevel)) 
             
          {
              level 
          = Level.FINEST;
             }
           else if ("OFF".equalsIgnoreCase(logLevel)) 
             
          {
              level 
          = Level.OFF;
             }


             config.setLogConfig(configProps.getProperty(
          "logDirectory"), level);
            }


            config.setEngineHome(
          "");
            IPlatformContext context 
          = new PlatformServletContext( sc );
            config.setPlatformContext( context );


            
          try
            
          {
             Platform.startup( config );
            }

            
          catch ( BirtException e )
            
          {
             e.printStackTrace( );
            }


            IReportEngineFactory factory 
          = (IReportEngineFactory) Platform
            .createFactoryObject( IReportEngineFactory.EXTENSION_REPORT_ENGINE_FACTORY );
            birtEngine 
          = factory.createReportEngine( config );


           }

           
          return birtEngine;
          }


          public static synchronized void destroyBirtEngine() {
           
          if (birtEngine == null{
            
          return;
           }
            
           birtEngine.shutdown();
           Platform.shutdown();
           birtEngine 
          = null;
          }


          public Object clone() throws CloneNotSupportedException {
           
          throw new CloneNotSupportedException();
          }


          private static void loadEngineProps() {
           
          try {
            
          //Config File must be in classpath
            ClassLoader cl = Thread.currentThread ().getContextClassLoader();
            InputStream in 
          = null;
            in 
          = cl.getResourceAsStream (configFile);
            configProps.load(in);
            in.close();


           }
           catch (IOException e) {
            e.printStackTrace();
           }


          }


          }


          WebReport.java

          import java.io.IOException;
          import java.io.PrintWriter;
          import java.util.HashMap;
          import java.util.logging.Level;
          import java.util.logging.Logger;

          import javax.servlet.ServletContext;
          import javax.servlet.ServletException;
          import javax.servlet.http.HttpServlet;
          import javax.servlet.http.HttpServletRequest;
          import javax.servlet.http.HttpServletResponse;

          import org.eclipse.birt.report.engine.api.EngineConstants;
          import org.eclipse.birt.report.engine.api.HTMLRenderContext;
          import org.eclipse.birt.report.engine.api.HTMLRenderOption;
          import org.eclipse.birt.report.engine.api.IReportRunnable;
          import org.eclipse.birt.report.engine.api.IRunAndRenderTask;
          import org.eclipse.birt.report.engine.api.IReportEngine;


          public class WebReport extends HttpServlet {

          /**
           * 
           
          */

          private static final long serialVersionUID = 1L;
          /**
           * Constructor of the object.
           
          */

          private IReportEngine birtReportEngine = null;
          protected static Logger logger = Logger.getLogger( "org.eclipse.birt" );

          public WebReport() {
           
          super();
          }


          /**
           * Destruction of the servlet. 
           
          */

          public void destroy() {
           
          super.destroy(); 
           BirtEngine.destroyBirtEngine();
          }



          /**
           * The doGet method of the servlet. 
           *
           
          */

          public void doGet(HttpServletRequest req, HttpServletResponse resp)
            
          throws ServletException, IOException {

           
          //get report name and launch the engine
           resp.setContentType("text/html");
           
          //resp.setContentType( "application/pdf" ); 
           
          //resp.setHeader ("Content-Disposition","inline; filename=test.pdf");  
           String reportName = req.getParameter("ReportName");
           ServletContext sc 
          = req.getSession().getServletContext();
           
          this.birtReportEngine = BirtEngine.getBirtEngine(sc);
           
           
          //setup image directory
           HTMLRenderContext renderContext = new HTMLRenderContext();
           renderContext.setBaseImageURL(req.getContextPath()
          +"/images");
           renderContext.setImageDirectory(sc.getRealPath(
          "/images"));
           
           logger.log( Level.FINE, 
          "image directory " + sc.getRealPath("/images"));  
           System.out.println(
          "stdout image directory " + sc.getRealPath("/images"));
           
           HashMap
          <String, HTMLRenderContext> contextMap = new HashMap<String, HTMLRenderContext>();
           contextMap.put( EngineConstants.APPCONTEXT_HTML_RENDER_CONTEXT, renderContext );
           
           IReportRunnable design;
           
          try
           
          {
            
          //Open report design
            design = birtReportEngine.openReportDesign( sc.getRealPath("/Reports")+"/"+reportName );
            
          //create task to run and render report
            IRunAndRenderTask task = birtReportEngine.createRunAndRenderTask( design );  
            task.setAppContext( contextMap );
            
            
          //set output options
            HTMLRenderOption options = new HTMLRenderOption();
            options.setOutputFormat(HTMLRenderOption.OUTPUT_FORMAT_HTML);
            
          //options.setOutputFormat(HTMLRenderOption.OUTPUT_FORMAT_PDF);
            options.setOutputStream(resp.getOutputStream());
            task.setRenderOption(options);
            
            
          //run report
            task.run();
            task.close();
           }
          catch (Exception e){
            
            e.printStackTrace();
            
          throw new ServletException( e );
           }

          }


          /**
           * The doPost method of the servlet. 
           *
           
          */

          public void doPost(HttpServletRequest request, HttpServletResponse response)
            
          throws ServletException, IOException {

          this.doGet(request,response);

          /**
           * Initialization of the servlet. 
           *
           * 
          @throws ServletException if an error occure
           
          */

          public void init() throws ServletException {
           BirtEngine.initBirtConfig();
           
          }


          }


          官網(wǎng)上還有2.2版本W(wǎng)ebReport.java的代碼,我反正沒(méi)用上。您要有用上的可以去試試。
          這三個(gè)文件弄好了就ok了。官網(wǎng)上還羅嗦一大堆是在Eclipse下開(kāi)發(fā)要加什么插件什么插件的其實(shí)就是加上Servlet支持的就行。我沒(méi)有用純Eclipse開(kāi)發(fā)所以直接編譯好這兩個(gè)java,連同拷貝上那個(gè)BirtConfig.properties資源文件到自己的WEB-INF/classes下面就行了。

          這下好了,就只有配置你的servlet了。
          在WEB-INF下面建立一個(gè)web.xml文件,配置一下這個(gè)webreport的servlet
          代碼:

          <?xml version="1.0" encoding="UTF-8"?> 
          <web-app version="2.4" 
          xmlns
          ="http://java.sun.com/xml/ns/j2ee" 
          xmlns:xsi
          ="http://www.w3.org/2001/XMLSchema-instance" 
          xsi:schemaLocation
          ="http://java.sun.com/xml/ns/j2ee 
          http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
          > 
          <servlet> 
          <servlet-name>WebReport</servlet-name> 
          <servlet-class>WebReport</servlet-class> 
          </servlet> 
          <servlet-mapping> 
          <servlet-name>WebReport</servlet-name> 
          <url-pattern>/webReport</url-pattern> 
          </servlet-mapping> 
          </web-app> 


          最后,把自己做好的報(bào)表放到自己項(xiàng)目下面的reports(這是個(gè)復(fù)數(shù)的s)里面就好,這個(gè)目錄不能錯(cuò)的,在servlet里面是要找這個(gè)目錄的。
          很簡(jiǎn)單,然后自己再寫一個(gè)自己的jsp就ok了
          原來(lái)的WebReport.java文件的doPost方法里什么都沒(méi)有的,如果自己的jsp通過(guò)post傳遞的話不會(huì)顯示出來(lái)報(bào)表。我上面吧post里面調(diào)用get了。
          這下就全部OK了。
          下面我的這個(gè)小jsp給大家參考。

          <%@ page language="java" import="java.util.*" pageEncoding="gb2312"%> 
          <html> 
          <head> 
          </head> 

          <body> 
          <form name="theform" action="webReport" method="post"> 
          <table> 
          <tr> 
          <td>TestReport.rptdesign</td> 
          <td><input name="ReportName" type="text"></td> 
          <td><button type="submit">走你</button></td> 
          </tr> 
          </table> 
          </form> 
          </body> 
          </html> 


          OK在輸入框里面填寫你的報(bào)表名稱,記得要帶擴(kuò)展名。
          這就完工了,但是有個(gè)問(wèn)題,我在用腳本數(shù)據(jù)源的時(shí)候提示不成功。還有我的帶參數(shù)的報(bào)表這樣部署的話不讓輸入?yún)?shù)自己就全出來(lái)了。希望大俠能幫我參考下。留言啊。留言~~小弟新人,學(xué)習(xí)中,渴望進(jìn)步!明天還得加班。困啊
          高手新人都加好友?。篗SN:lewesbonnie@hotmail.com   QQ: 232172300

          轉(zhuǎn)載請(qǐng)注明出處和作者哈!!

          posted on 2008-06-06 23:08 leweslove 閱讀(2973) 評(píng)論(0)  編輯  收藏 所屬分類: Other

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          Copyright@2008-2009 By Evan
          主站蜘蛛池模板: 新化县| 长汀县| 曲麻莱县| 长寿区| 花莲市| 乌拉特前旗| 平乐县| 文水县| 同心县| 嘉禾县| 海宁市| 容城县| 板桥市| 陈巴尔虎旗| 富顺县| 图木舒克市| 牟定县| 小金县| 塔城市| 沅江市| 金湖县| 无为县| 山西省| 平泉县| 四平市| 色达县| 抚顺县| 水富县| 汝南县| 西贡区| 明光市| 南宫市| 绵阳市| 平定县| 松溪县| 恩平市| 玉龙| 盐山县| 惠安县| 凯里市| 青河县|