SpringBootはJspを使用してCrystalレポートをプレビューします(2):Oracleデータベースに接続されているCrystalレポートをプレビューします

目次

I.はじめに

1開発環境

2プロジェクトの構造

2. CrystalReportをプレビューします

1 CrystalReportsオープンソースプロジェクトを追加します-CRJavaHelperツールクラス

2クリスタルレポートを追加

3jspファイルを作成します

4 CrystalReportsのサーブレットを構成します

5コントローラーを追加

3、テスト


プロジェクトはgitにアップロードされました:https//gitee.com/373616511/spring-boot-crystal-reports-jsp-demo.git

I.はじめに

1開発環境

プロジェクトの開発を続行します。SpringBootはJspを使用してCrystalレポートをプレビューします(1):プロジェクトをセットアップしてレポートをプレビューします

https://blog.csdn.net/cs373616511/article/details/109953182

2プロジェクトの構造

2. CrystalReportをプレビューします

1 CrystalReportsオープンソースプロジェクトを追加-CRJavaHelperツールクラス

ここで使用するCRJavaHelperツールクラスをダウンロードしますが、公式WebサイトのJavaドキュメントが見つかりませんでした

https://github.com/souvikduttachoudhury/CrystalReportsSpringBoot.git

src / com / businessobjects / samples / CRJavaHelper.java

CRJavaHelperツールクラスを使用してCrystalReportを操作し、プロジェクトにコピーします

CRJavaHelper .java

2クリスタルレポートを追加

test2.rpt

test2.rptの作成方法は、Baiduであるか、ブログhttps://blog.csdn.net/cs373616511/article/details/109269674を参照してください。

3jspファイルを作成します

cr2.jsp

<%@page contentType=" text/html " %>
<%@page pageEncoding="UTF-8" %>
<%@page import=" com.crystaldecisions.sdk.occa.report.* " %>  <%--  webreporting.jar --%>
<%@page import=" com.crystaldecisions.report.web.viewer.* " %>
<%@ page import="com.asyf.demo.utils.CRJavaHelper" %>
<%@ page import="com.crystaldecisions.sdk.occa.report.application.ReportClientDocument" %>
<%
    // 水晶报表的位置
    final String REPORT_NAME = "/WEB-INF/test2.rpt";
%>
<%
    try {
        //打开报表
        ReportClientDocument reportClientDoc = new ReportClientDocument();
        reportClientDoc.open(REPORT_NAME, 0);

        //更换数据源
        String username = "scott";
        String password = "root";
        String driverName = "oracle.jdbc.OracleDriver";
        String connectionURL = "jdbc:oracle:thin:@localhost:1521:orcl";
        String jndiName = "JDBC(JNDI)";
        CRJavaHelper.changeDataSource(reportClientDoc, username, password, connectionURL, driverName, jndiName);

        //给参数赋值
        //第二个参数代表子报表,设置主报表参数,设置为空串
        CRJavaHelper.addDiscreteParameterValue(reportClientDoc, "", "deptNoPar", 10);
        //C:\\Users\\Administrator\\Desktop\\001.jpg"
        CRJavaHelper.addDiscreteParameterValue(reportClientDoc, "", "imagePath", "");

        // 把报表源放进session,传递到报表显示页面
        session.setAttribute("reportSource", reportClientDoc.getReportSource());
        // 建立一个viewer对象实例,并设置
        CrystalReportViewer viewer = new CrystalReportViewer();
        viewer.setOwnPage(true);
        viewer.setOwnForm(true);
        viewer.setPrintMode(CrPrintMode.ACTIVEX);
        //
        // 从session中取报表源
        Object reportSource = session.getAttribute("reportSource");
        viewer.setReportSource(reportSource);
        viewer.setReportSource(reportClientDoc.getReportSource());      //显示水晶报表
        viewer.processHttpRequest(request, response, this.getServletConfig().getServletContext(), null);
    } catch (Exception ex) {
        out.println(ex);
    }
%>

4 CrystalReportsのサーブレットを構成します

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    //配置serverlet,否则无法加载图片以及一些其他功能
    @Bean
    public ServletRegistrationBean getServletRegistrationBean() {
        ServletRegistrationBean bean = new ServletRegistrationBean(new CrystalReportViewerServlet());
        bean.addUrlMappings("/CrystalReportViewerHandler");
        return bean;
    }

}

5コントローラーを追加

@GetMapping("/cr2")
    public String cr2(Model model) {
        return "cr2";
    }

3、テスト

デモンストレーション効果

 

おすすめ

転載: blog.csdn.net/cs373616511/article/details/109955330