Crytal reports水晶报表在Spring boot web项目中的使用, 无需web.xml文件

喜你入骨 提交于 2020-01-09 11:36:42

【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>>

1. 配置pom.xml, 添加jsp支持:
		<dependency>
			<groupId>org.apache.tomcat.embed</groupId>
			<artifactId>tomcat-embed-jasper</artifactId>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jstl</artifactId>
		</dependency>
2. 创建Crytal reports水晶报表文件夹
2.1 在src/main目录下创建webapp文件夹,同时将webapp文件夹加入build path.
2.2 配置application.properties指定待解析的jsp文件存放路径

/WEB-INF/文件夹为jsp文件存放的文件夹,可以是/, 也可以是/WEB-INF/jsp//WEB-INF文件夹下的文件不能在浏览器中直接访问,/下的文件,可以在浏览器中直接访问

spring.mvc.view.prefix: /WEB-INF/
spring.mvc.view.suffix=.jsp
3. 相关文件结构

3.1 lib文件夹是水晶报表的依赖库, 通过安装CR4E(Crystal reports for Eclipces) 生成的水晶报表的web项目可以获得。
3.2 CRConfig.xml放置在build path的根目录下,可以是src/main/webapp, 也可以是src/main/resources
<?xml version="1.0" encoding="utf-8"?>

<CrystalReportEngine-configuration>
    <reportlocation>../</reportlocation>
    <timeout>0</timeout>
    <ExternalFunctionLibraryClassNames>
    	<classname></classname>
    </ExternalFunctionLibraryClassNames>
</CrystalReportEngine-configuration>

<reportlocation>../</reportlocation>很关键,指定了报表加载的位置, ../表示spring.mvc.view.prefix设定的值

3.3 crystalreportviewers120为web前端显示的js库, 通过安装CR4E(Crystal reports for Eclipces) 生成的水晶报表的web项目可以获得。可以放在src/main/webapp, 也可以是src/main/resources/static
3.4 添加控制器Controller入口和jsp文件

reportController:

package sageassistant;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class reportController {
	@GetMapping("/index")
	public String index() {
		return "index";
	}
}

index.jsp 这些代码可以在安装了CR4E(Crystal reports for Eclipces)后获得

<%------------Beginning of 'Insert Crystal Reports Viewer' snippet----------------%><%
//---------- Create the viewer and render the report -------------
	
//Create the CrystalReportViewer object
com.crystaldecisions.report.web.viewer.CrystalReportViewer crystalReportViewer = new com.crystaldecisions.report.web.viewer.CrystalReportViewer();

//Set the reportsource property of the viewer
crystalReportViewer.setReportSource("Report1.rpt");

//Set viewer attributes
crystalReportViewer.setOwnPage(true);
  
//Set the CrystalReportViewer print mode
crystalReportViewer.setPrintMode(com.crystaldecisions.report.web.viewer.CrPrintMode.PDF);

//Process the report
crystalReportViewer.processHttpRequest(request, response, getServletConfig().getServletContext(), null); 
%><%------------End of 'Insert Crystal Reports Viewer' snippet----------------%>
4 显示效果

5 容易出现的问题

CRConfig.xml中的<reportlocation>../</reportlocation>设置必须正确,否则容易出现找不到报表文件的问题。

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!