Apr 5

Birt 2.2与struts 1.3.9整合 不指定

chirs , 19:33 , 瓜哇天地 , 评论(0) , 引用(0) , 阅读(6561) , Via 本站原创 | |
此整合参考了ServletExample和SpringSide(春天的旁边1m03)中实现的例子!
1.去www.eclipse.org/birt/上下载birt-runtime-2_2_0.zip

2.在%WEB_APP%/WEB-INF/下新建一文件夹"platform""

3.把刚下载的birt-runtime-2_2_0.zip包中的ReportEngine目录下的"configuration"和"plugins"复制到%WEB_APP%/WEB-INF/platform/下

4.把ReportEngine目录下的"lib"里面的jar文件复制到%WEB_APP%/WEB-INF/lib/下面

5.对ActionServlet进行扩展

/**
* 对Struts进行扩展,实现Hibernate的初始化以及参数编码的自动处理
*
* @author [email]chirs@zhoujin.com[/email]
*/
public class FreeOCS_ActionServlet extends ActionServlet {

    private static final long serialVersionUID = 1L;
    private String encoding;
    public void init() throws ServletException {
        ServletContext context = getServletContext();
        if (Globals.WEBAPP_PATH == null) Globals.WEBAPP_PATH = context.getRealPath("");
        // 初始化系统安全控制
        try {
            KukeSecurityManager.init(context);
        } catch (IOException e) {
            throw new ServletException(e);
        }
        // 执行Struts的初始化过程
        super.init();
        BirtEngine.initBirtConfig();
        encoding = getInitParameter("encoding");
        if (encoding == null) encoding = Globals.ENC_UTF_8;
    }

    /**
     * 实现对编码的自动转码处理
     *
     * @param req
     * @param res
     * @throws ServletException
     * @throws IOException
     * @see org.apache.struts.action.ActionServlet#process(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
     */
    protected void process(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {

        HttpServletRequest request;
        if (RequestUtils.isMultipart(req)) {
            // 文件表单的编码处理
            request = req;
            request.setCharacterEncoding(encoding);
        } else {
            // 自动编码处理
            String enc = req.getCharacterEncoding();
            if (req instanceof RequestProxy) request = req;
            else if (encoding.equalsIgnoreCase(enc)) request = req;
            else request = new RequestProxy(req, encoding);
        }
        super.process(request, res);
    }

    public void destroy() {
        // 释放Struts
        super.destroy();
        BirtEngine.destroyBirtEngine();
    }
}

6.BirtEngine.java
public class BirtEngine {
    private static IReportEngine birtEngine = null;
    private static Properties configProps = new Properties();
    private final static String configFile = "ReportConfig.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.destroy();
        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();
        }
    }
}

7.ActionBase.java
/**
* *
* @author [email]chirs@zhoujin.com[/email]
*/
public abstract class ActionBase extends Action {
    final static String ISO8859_1 = "8859_1";

    protected static String FORWARD_LIST = "list";
    protected static String FORWARD_CREATE = "create";
    protected static String FORWARD_EDIT = "edit";
    protected static String FORWARD_UPDATE = "update";
    protected static String FORWARD_ERROR = "error";
    protected static String FORWARD_HTMLERROR = "htmlError";
    public final static String METHOD_IDENT_PARAM = "__method";

    protected final Log log = LogFactory.getLog(this.getClass().getName());

    private IReportEngine birtReportEngine = null;
    /**
     * 查询结果列表
     */
    private List resultList;
    /**
     * 设计文件的在web应用中的相对路径,不含context path. eg. /reports/sale_report.rptdesign
     */
    private String designFile = null;

    /**
     * 图片生成目录在web应用中的相对路径,,不含context path eg. "/plugins-demo/birt/sale_report.rptdesign/birt/images
     */
    private String imageDirectory = null;

    private String scriptableJOName = "dsFactory";

    /**
     * 在页面上显示的图片链接,是与请求url的相对路径
     */
    private String baseImageUrl = "images";
    /**
     * 输出格式参数
     */
    private String format = "pdf";
    /**
     * 打开报表设计文件(.rptdesign)
     */
    public void openReportDesign(String file) throws IOException, EngineException {
        designFile = file;
    }
    /**
     * 设置图片目录
     */
    public void setImageDirectory(String imageDirectory) {
        this.imageDirectory = imageDirectory;
    }
    /**
     * 设置图片地址
     */
    public void setBaseImageUrl(String baseImageUrl) {
        this.baseImageUrl = baseImageUrl;
    }
    /**
     * 设置报表输出格式
     */
    public void setFormat(String format) {
        this.format = format;
    }
    public void setScriptableJOName(String scriptableJOName) {
        this.scriptableJOName = scriptableJOName;
    }
    /**
     * 放入查询结果数据
     */
    public void putModel(List list) {
        resultList = list;
    }

    protected ServletContext context() {
        return servlet.getServletContext();
    }

    protected ServletConfig config() {
        return servlet.getServletConfig();
    }
    /**
     * birt 运行
     */
    @SuppressWarnings("deprecation")
    protected void outputReport(HttpServletRequest request, HttpServletResponse response) throws IOException {
        if (format == null || format.equalsIgnoreCase("html")) {
            response.setContentType("text/html");// 用HTML方式显示
        } else {
            response.setContentType("application/pdf");
            // response.setHeader("Content-Disposition", "inline; filename=test.pdf");
            response.setHeader("Content-Disposition", "attachment; filename=test.pdf");// 下载
        }
        ServletContext sc = request.getSession().getServletContext();
        birtReportEngine = BirtEngine.getBirtEngine(sc);

        IReportRunnable design;
        try {
            // Open report design
            design = birtReportEngine.openReportDesign(sc.getRealPath("/reports") + "/" + designFile);
            // create task to run and render report
            IRunAndRenderTask task = birtReportEngine.createRunAndRenderTask(design);
            BirtDataSourceObject birtDataSourceObject = new BirtDataSourceObject();
            // 设置输出选项
            HTMLRenderOption options = new HTMLRenderOption();
            birtDataSourceObject.setResultList(resultList);
            options.setBaseImageURL(request.getContextPath() + imageDirectory);
            options.setImageDirectory(sc.getRealPath(baseImageUrl));

            if (format == null || format.equalsIgnoreCase("html")) {
                response.setContentType("text/html");// 用HTML方式显示
                options.setOutputFormat(HTMLRenderOption.OUTPUT_FORMAT_HTML);// HTML
            } else {
                options.setOutputFormat(HTMLRenderOption.OUTPUT_FORMAT_PDF);// PDF
            }
            options.setOutputStream(response.getOutputStream());
            task.setRenderOption(options);
            task.addScriptableJavaObject(scriptableJOName, birtDataSourceObject);
            // run report
            task.run();
            task.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

8.BirtDataSourceObject.java
/**
*
* @author [email]chirs@zhoujin.com[/email]
*/
public class BirtDataSourceObject {
    private List resultList = new ArrayList();

    public List getResultList() {
        return resultList;
    }

    public void setResultList(List resultList) {
        this.resultList = resultList;
    }
}



测试:
1.BaseTestListAction.java
public class BaseTestListAction extends ActionBase {
public ActionForward doExecute(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws Exception {
        this.setFormat("pdf");
        this.openReportDesign("test.rptdesign");
        List<ViewListBean> list=new ArrayList<ViewListBean>();
        ViewListBean oa=new ViewListBean();
        oa.setAnnexa("测试1");
        oa.setAnnexb("测试2");
        oa.setAnnexc("测试3");
        list.add(oa);
        ViewListBean ob=new ViewListBean();
        ob.setAnnexa("测试4");
        ob.setAnnexb("测试5");
        ob.setAnnexc("测试6");
        list.add(ob);
        ViewListBean oc=new ViewListBean();
        oc.setAnnexa("测试7");
        oc.setAnnexb("测试8");
        oa.setAnnexc("测试9");
        list.add(oc);
        this.putModel(list);
        outputReport(request,response);
        return null;
    }
}

2.在open中输入
rtn = dsFactory.getResultList();
totalrows = rtn.size();
currentrow=0;

3.在fetch中输入
if(currentrow >= totalrows){
return false;
}
row["annexa"] = rtn.get(currentrow).getAnnexa();
row["annexb"] = rtn.get(currentrow).getAnnexb();
row["annexc"] = rtn.get(currentrow).getAnnexb();
currentrow++;
return true;

4.在close中输入
rtn = null;
Tags: , , ,
发表评论
表情
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
记住我
昵称   密码   游客无需密码
网址   电邮   [注册]