UReport2 安裝與配置

2022-09-07 15:57 更新
UReport2 教學(xué)視頻http://pan.baidu.com/s/1boWTxF5,密碼:98hj

       UReport2 的設(shè)計(jì)器是基于網(wǎng)頁(yè)的,所以我們配置好一個(gè)項(xiàng)目,也就完成了報(bào)表設(shè)計(jì)器的安裝,因?yàn)?nbsp;UReport2 是一款純 Java 的報(bào)表引擎,所以它支持現(xiàn)在流行的所有類型 J2EE 項(xiàng)目,這里我們將主要介紹基于 Maven 的 J2EE 項(xiàng)目中如何包含 UReport2。

基于 Maven 的 UReport2 項(xiàng)目搭建

       首先我們需要?jiǎng)?chuàng)建一個(gè)標(biāo)準(zhǔn)的 Maven 項(xiàng)目(具體創(chuàng)建過(guò)程可以使用 Eclipse 或其它工具,這里就不再贅述),然后要打開 Maven 的 pom.xml 文件,在其中添加 UReport2 的依賴信息,如下所示:

<dependency>
    <groupId>com.bstek.ureport</groupId>
    <artifactId>ureport2-console</artifactId>
    <version>[version]</version>
</dependency>
關(guān)于版本在上面的依賴信息中,[version] 表示 ureport2-console 包的具體版本號(hào),我們可以到 http://search.maven.org/ 上查詢“ureport2-console”關(guān)鍵字,以查看該包的具體版本號(hào)。最新源碼可以到 https://github.com/youseries/ureport 上下載。

       需要注意的是,在 http://search.maven.org/ 上我們只能查找到最新的release版本,如果您需要最新的snapshot,那么可以到 https://oss.sonatype.org/ 上查找,因?yàn)?sonatype 規(guī)定,只有正式版本才可以發(fā)到 http://search.maven.org/ 上,也就是 mave 的 central      repository 中,snapshot 版本只能存在于 https://oss.sonatype.org/ 中,所以如果我們要采用 https://oss.sonatype.org/ 中最新的 snapshot,那么就需要在 pom.xml 中添加一個(gè) 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 完整內(nèi)容

<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>

       在實(shí)際使用中,我們的 pom.xml 文件可能還需要添加其它依賴信息,比如目標(biāo)數(shù)據(jù)庫(kù)驅(qū)動(dòng)等,如果有那么只需要修改上述 pom.xml 文件,加上相應(yīng)依賴即可。

       接下來(lái),我們需要配置一個(gè) UReport2 需要使用到的 servlet。打開項(xiàng)目的 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>

       在這個(gè) servlet 配置當(dāng)中,值為“/ureport/*”的 url-pattern 是一定不能變的,否則系統(tǒng)將無(wú)法運(yùn)行。

       因?yàn)?UReport2 是架構(gòu)在 spring 之上的,所以作為配置的最后一步就是讓我們的項(xiàng)目加載 UReport2 的 spring 配置文件,加載的方法有很多,比如我們可以打開 web.xml,在其中添加一個(gè) 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>

       如果您的項(xiàng)目中沒有采用 spring,那么就可以采用上述配置直接加載 UReport2 提供的 spring 配置文件,如果您的項(xiàng)目中也用到了 spring,或項(xiàng)目中其它模塊用到了 spring,那么可以在我們已存在的 spring 配置文件中導(dǎo)入 UReport2 提供的 spring 配置文件,如下配置所示:

導(dǎo)入 UReport2 提供的 spring 配置文件

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

       如果項(xiàng)目中沒已存在的 spring 配置文件,那么我們可以在 WEB-INF 目錄下新建一個(gè)名為 context.xml 的 spring 配置文件,在其中導(dǎo)入 UReport2 提供的 spring 配置文件,如下所示:

context.xml 文件內(nèi)容

<?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>
關(guān)于 Properties 文件加載如果你是將 UReport 配置到一個(gè)已存在的 Spring 項(xiàng)目中,同時(shí)項(xiàng)目也要 Spring 中加載自己的 Properties 文件,比如通過(guò)下面的方式加載自己的 Properties 文件這時(shí)啟動(dòng)應(yīng)用,我們會(huì)發(fā)現(xiàn)啟動(dòng)過(guò)程中系統(tǒng)會(huì)報(bào)各種屬性找不到的異常,解決辦法就是在上面配置中加上 ignore-unresolvable="true" 以及 order="1" 兩個(gè)屬性即可,其中 ignore-unresolvable 屬性表達(dá)忽略當(dāng)前配置的 Properties 文件中沒有配置的屬性,order 屬性值表示加載優(yōu)先級(jí),值越小,優(yōu)先級(jí)越高。當(dāng)然如果你的項(xiàng)目中 Properties 文件加載是通過(guò)配置 PropertyPlaceholderConfigurer 為 bean 的方式加載,那么同樣需要為這個(gè) bean 添加如下兩個(gè)屬性,作用也是一樣:

       接下來(lái)打開 web.xml,在其中添加一個(gè) spring 提供的 listener,加載我們新建的這個(gè) 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>

       到這里,我們就在一個(gè)標(biāo)準(zhǔn)的 Maven 項(xiàng)目中加入了  UReport2,運(yùn)行項(xiàng)目,在瀏覽器中訪問 URL:http://localhost:8080/ureport-demo/ureport/designer,就可以看到 UReport2 的報(bào)表設(shè)計(jì)器界面。

非 Maven 項(xiàng)目中添加 UReport2

       如果我們的項(xiàng)目是一個(gè)傳統(tǒng)的 J2EE 項(xiàng)目,沒有采用 Maven 來(lái)對(duì)項(xiàng)目進(jìn)行管理,如果要在這種項(xiàng)目中添加 UReport2,方法與上面的基于 Maven 項(xiàng)目基本類似,唯一不同的地方就是不用配置 pom.xml 文件,這樣的話 UReport2 相關(guān)的 Jar 及依賴的第三方Jar我們需要手動(dòng)復(fù)制到項(xiàng)目的 WEB-INF/lib 目錄當(dāng)中。

       UReport2 依賴的第三方 Jar 包,可以從后面的鏈接中下載(Jar 包較大,分兩個(gè)壓縮包,點(diǎn)擊下載第一部分Jar包,下載第二部分Jar包);UReport2 自己的 Jar 包有三個(gè),分別是 ureport2-core、ureport2-font及ureport2-console,我們可以到 https://oss.sonatype.org/ 上分別下載他們(分別輸入 ureport2-core、ureport2-font 或 ureport2-console 關(guān)鍵字查詢下載最新版)。

       將下載下來(lái)的第三方 Jar 包和 UReport2 自己的三個(gè) Jar 包一起放到項(xiàng)目的 WEB-INF/lib 目錄下,然后向上面介紹的方法一樣配置 UReport2 的 Servlet 并加載 UReport2 提供的 spring 配置文件,這樣就可以將 UReport2 加入到項(xiàng)目當(dāng)中。

運(yùn)行

IDEA 開發(fā)環(huán)境運(yùn)行

右鍵單擊main class 文件 -> Run ‘xxxxx’

1

編譯成 Jar 包運(yùn)行

1.打開 Project Structure…

2

2.add Artifacts

3

 

3.設(shè)置jar屬性

切記step 3,要?jiǎng)h除默認(rèn)的‘main\java’, 僅保留到src 目錄

4

點(diǎn)擊‘OK’后,如果報(bào)如下錯(cuò)誤,只需要?jiǎng)h除文件 MANIFEST.MF 即可。

5

build

6

 7

run jar 包

編譯好的 jar 包在工程目錄的 .\out\artifacts 目錄下

例如 demo\out\artifacts\demo_jar

Run > java -jar demo.jar


報(bào)表源碼和示例: https://gitee.com/BSTEK-BEIJING/ureport


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)