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>
需要注意的是,在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> <id>sonatype</id> <url>https://oss.sonatype.org/content/groups/public/</url> </repository>
到这里,Maven的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.0.0</version> </dependency> </dependencies> </project>
在实际使用中,我们的pom.xml文件可能还需要添加其它依赖信息,比如目标数据库驱动等,如果有那么只需要修改上述pom.xml文件,加上相应依赖即可。
接下来,我们需要配置一个UReport2需要使用到的servlet。打开项目的web.xml文件,在其中添加如下所示的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配置文件,如下所示:
<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配置文件,如下配置所示:
<import resource="classpath:ureport-console-context.xml" />
如果项目中没已存在的spring配置文件,那么我们可以在WEB-INF目录下新建一个名为context.xml的spring配置文件,在其中导入UReport2提供的spring配置文件,如下所示:
<?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>
接下来打开web.xml,在其中添加一个spring提供的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加入到项目当中。
Attachments:



