UReport2 安装与配置

2022-12-30 13:37 更新
UReport2 教学视频
http://pan.baidu.com/s/1boWTxF5,密码:98hj

UReport商业版BaskReport已推出,采出全新算法,全新设计器,实现百万条数据秒级加载, 价格低至1000/年,详见:https://www.basksoft.com  

     UReport2 的设计器是基于网页的,所以我们配置好一个项目,也就完成了报表设计器的安装,因为 UReport2 是一款纯 Java 的报表引擎,所以它支持现在流行的所有类型 J2EE 项目,这里我们将主要介绍基于 Maven 的 J2EE 项目中如何包含 UReport2。

基于 Maven 的 UReport2 项目搭建

       首先我们需要创建一个标准的 Maven 项目(具体创建过程可以使用 Eclipse 或其它工具,这里就不再赘述),然后要打开 Maven 的 pom.xml 文件,在其中添加 UReport2 的依赖信息,如下所示:

<dependency>
    <groupId>com.bstek.ureport</groupId>
    <artifactId>ureport2-console</artifactId>
    <version>[version]</version>
</dependency>
关于版本
在上面的依赖信息中,[version] 表示 ureport2-console 包的具体版本号,我们可以到 http://search.maven.org/ 上查询“ureport2-console”关键字,以查看该包的具体版本号。最新源码可以到 https://github.com/youseries/ureport 上下载。

       需要注意的是,在 http://search.maven.org/ 上我们只能查找到最新的release版本,如果您需要最新的snapshot,那么可以到 https://oss.sonatype.org/ 上查找,因为 sonatype 规定,只有正式版本才可以发到 http://search.maven.org/ 上,也就是 mave 的 central      repository 中,snapshot 版本只能存在于 https://oss.sonatype.org/ 中,所以如果我们要采用 https://oss.sonatype.org/ 中最新的 snapshot,那么就需要在 pom.xml 中添加一个 repository 信息,告诉 Maven 该到这里去下载 snapshot 版本的包,repository 信息如下所示:

Repository 信息

<repository>
    <id>sonatype</id>
    <url>https://oss.sonatype.org/content/groups/public/</url>
</repository>

       到这里,Maven 的 pom.xml 文件就配置完成了,pom.xml 的完整配置如所下所示:

pom.xml 完整内容

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.bstek.ureport</groupId>
    <artifactId>ureport2-demo</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>war</packaging>
    <dependencies>
        <dependency>
            <groupId>com.bstek.ureport</groupId>
            <artifactId>ureport2-console</artifactId>
            <version>2.2.2</version>
        </dependency>
    </dependencies>
</project>

       在实际使用中,我们的 pom.xml 文件可能还需要添加其它依赖信息,比如目标数据库驱动等,如果有那么只需要修改上述 pom.xml 文件,加上相应依赖即可。

       接下来,我们需要配置一个 UReport2 需要使用到的 servlet。打开项目的 web.xml 文件,在其中添加如下所示的 servlet 配置:

Servlet 配置

<servlet>
    <servlet-name>ureportServlet</servlet-name>
    <servlet-class>com.bstek.ureport.console.UReportServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>ureportServlet</servlet-name>
    <url-pattern>/ureport/*</url-pattern>
</servlet-mapping>

       在这个 servlet 配置当中,值为“/ureport/*”的 url-pattern 是一定不能变的,否则系统将无法运行。

       因为 UReport2 是架构在 spring 之上的,所以作为配置的最后一步就是让我们的项目加载 UReport2 的 spring 配置文件,加载的方法有很多,比如我们可以打开 web.xml,在其中添加一个 spring 提供的 listener 直接加载 UReport2 提供的 spring 配置文件,如下所示:

直接加载 UReport2 的 spring 配置文件

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:ureport-console-context.xml</param-value>
</context-param>

       如果您的项目中没有采用 spring,那么就可以采用上述配置直接加载 UReport2 提供的 spring 配置文件,如果您的项目中也用到了 spring,或项目中其它模块用到了 spring,那么可以在我们已存在的 spring 配置文件中导入 UReport2 提供的 spring 配置文件,如下配置所示:

导入 UReport2 提供的 spring 配置文件

<import resource="classpath:ureport-console-context.xml" />

       如果项目中没已存在的 spring 配置文件,那么我们可以在 WEB-INF 目录下新建一个名为 context.xml 的 spring 配置文件,在其中导入 UReport2 提供的 spring 配置文件,如下所示:

context.xml 文件内容

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
    ">
    <import resource="classpath:ureport-console-context.xml" />
</beans>
关于 Properties 文件加载
如果你是将 UReport 配置到一个已存在的 Spring 项目中,同时项目也要 Spring 中加载自己的 Properties 文件,比如通过下面的方式加载自己的 Properties 文件
<context:property-placeholder location="/WEB-INF/app.properties"/>
这时启动应用,我们会发现启动过程中系统会报各种属性找不到的异常,解决办法就是在上面配置中加上 ignore-unresolvable="true" 以及 order="1" 两个属性即可,其中 ignore-unresolvable 属性表达忽略当前配置的 Properties 文件中没有配置的属性,order 属性值表示加载优先级,值越小,优先级越高。
当然如果你的项目中 Properties 文件加载是通过配置 PropertyPlaceholderConfigurer 为 bean 的方式加载,那么同样需要为这个 bean 添加如下两个属性,作用也是一样:
<property name="ignoreUnresolvablePlaceholders" value="true"></property>
<property name="order" value="1"></property>

       接下来打开 web.xml,在其中添加一个 spring 提供的 listener,加载我们新建的这个 context.xml 文件,如下所示:

listener 中加载 context.xml 文件

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/context.xml</param-value>
</context-param>

       到这里,我们就在一个标准的 Maven 项目中加入了  UReport2,运行项目,在浏览器中访问 URL:http://localhost:8080/ureport-demo/ureport/designer,就可以看到 UReport2 的报表设计器界面。

非 Maven 项目中添加 UReport2

       如果我们的项目是一个传统的 J2EE 项目,没有采用 Maven 来对项目进行管理,如果要在这种项目中添加 UReport2,方法与上面的基于 Maven 项目基本类似,唯一不同的地方就是不用配置 pom.xml 文件,这样的话 UReport2 相关的 Jar 及依赖的第三方Jar我们需要手动复制到项目的 WEB-INF/lib 目录当中。

       UReport2 依赖的第三方 Jar 包,可以从后面的链接中下载(Jar 包较大,分两个压缩包,点击下载第一部分Jar包下载第二部分Jar包);UReport2 自己的 Jar 包有三个,分别是 ureport2-core、ureport2-font及ureport2-console,我们可以到 https://oss.sonatype.org/ 上分别下载他们(分别输入 ureport2-core、ureport2-font 或 ureport2-console 关键字查询下载最新版)。

       将下载下来的第三方 Jar 包和 UReport2 自己的三个 Jar 包一起放到项目的 WEB-INF/lib 目录下,然后向上面介绍的方法一样配置 UReport2 的 Servlet 并加载 UReport2 提供的 spring 配置文件,这样就可以将 UReport2 加入到项目当中。

运行

IDEA 开发环境运行

右键单击main class 文件 -> Run ‘xxxxx’

1

编译成 Jar 包运行

1.打开 Project Structure…

2

2.add Artifacts

3

 

3.设置jar属性

切记step 3,要删除默认的‘main\java’, 仅保留到src 目录

4

点击‘OK’后,如果报如下错误,只需要删除文件 MANIFEST.MF 即可。

5

build

6

 7

run jar 包

编译好的 jar 包在工程目录的 .\out\artifacts 目录下

例如 demo\out\artifacts\demo_jar

Run > java -jar demo.jar


以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号