天天看點

潤乾V5與springBoot內建

Spring Boot是由Pivotal團隊提供的全新架構,其設計目的是用來簡化新Spring應用的初始搭建以及開發過程。該架構使用了特定的方式來進行配置,進而使開發人員不再需要定義樣闆化的配置。下面介紹下一個簡單的springBoot項目的搭建以及與潤乾V5內建的過程。

PS:如已掌握springBoot工程搭建,第一部分可忽略

開發工具:myeclipse 2017

一:maven建構springBoot項目

1、通路http://start.spring.io/

2、選擇建構工具Maven Project、Spring Boot版本,Dependencies

選擇web,可參考下圖所示:

潤乾V5與springBoot內建

3、點選Generate Project下載下傳項目壓縮包

4、解壓後,使用eclipse,Import -> Existing Maven Projects -> Next ->選擇解壓後的檔案夾-> Finsh,OK done。

工程會自動根據pom.xml中的配置自動加載需要的jar檔案,jar加載完成後一個簡單的springBoot工程搭建完畢。

5、編寫controller測試:

package com.neo.springBoot;

import java.util.Map;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

@Controller

public class HelloController {

    @RequestMapping("/hello")

    public String index() {

        return "Hello world!";

    }

}

6、啟動springBoot,工程搭建後,會自動在com.neo.springBoot(neo.springBoot路徑為下載下傳工程時設定的包路徑)下生成一個Application.java,運作該類就能啟動工程,在該類上右鍵——Run As——Spring Boot App,即可啟動項目。

7、打開浏覽器通路:http://localhost:8080/hello,即可在頁面中輸出“Hello World!”

8、springBoot預設不支援jsp頁面展示,而報表需要通過jsp的tag标簽展示,是以要在功能中增加jsp展示的配置。

9、打開pom.xml,在裡邊增加:

潤乾V5與springBoot內建

 同樣,工程會自動加載相關的jar包,下載下傳完成後,工程即可支援jsp檔案。

二:內建潤乾V5

1:在src/main下建立目錄webapp,并将潤乾V5設計器安裝後産生的demo應用下的檔案複制到webapp下。springBoot不需要web.xml,是以web.xml不需要複制,并且WEB-INF下的lib目錄不需要複制。

2:內建jar包,springBoot是通過maven管理jar包,在設計器安裝目錄下的\report5\doc\zh有個pom.xml,潤乾需要的第三方jar包在該檔案中配置,将潤乾的pom.xml中的内容複制到工程中即可。潤乾自己的jar包需要手動內建到springBoot項目中,內建方法:

在開發工具中,右鍵Referenced Libraries——Build Path——configure Build Path——Add External JARs,在彈出視窗中選擇引入的jar包,本例隻引入幾個相關jar包,實際使用時引入除pom.xml中其他所有jar包,

潤乾V5與springBoot內建

3、配置servlet

潤乾報表需要servlet加載配置檔案,普通java web應用中servlet是在web.xml中配置,但springBoot總并不會讀取web.xml,是以所需要的servlet、listener、filter都需要單獨在springBoot中單獨配置。

Spring Boot提供了 

ServletRegistrationBean

FilterRegistrationBean

ServletListenerRegistrationBean

這3個來進行配置Servlet、Filter、Listener。

在工程中建立class,源碼如下:

package com.neo.springBoot;

import java.util.*; 

import org.springframework.boot.context.embedded.FilterRegistrationBean; 

import org.springframework.boot.context.embedded.ServletListenerRegistrationBean; 

import org.springframework.boot.context.embedded.ServletRegistrationBean; 

import org.springframework.context.annotation.Bean; 

import org.springframework.context.annotation.Configuration; 

import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import com.raqsoft.report.view.ReportServlet;

import com.raqsoft.report.view.ServletMappings; 

@Configuration 

public class WebConfig {  

    @Bean 

    public ServletRegistrationBean servletRegistrationBean() { 

        ServletRegistrationBean registration = new ServletRegistrationBean(new ReportServlet());

        registration.setLoadOnStartup(1);

        registration.addInitParameter("configFile", "/WEB-INF/raqsoftConfig.xml");

        registration.addInitParameter("headless", "none");

        registration.setName("reportServlet");

        registration.addUrlMappings("/reportServlet");

        ServletMappings.mappings.put( "com.raqsoft.report.view.ReportServlet", "/reportServlet");

        System.out.println("潤乾servlet注冊完成");

        return registration;

    } 

}

這樣就可以完成潤乾的reportServlet的注冊,本例隻介紹潤乾V5內建到springBoot中的核心操作,是以隻需要這一個servlet即可,其他servlet、Filter、Listener等按照相應的方法添加即可。

4、制作測試報表放到src/main/webapp/WEB-INF/reportFiles目錄下。

5、重新開機工程,在浏覽器中通路:http://localhost:8080/reportJsp/showReport.jsp?rpx=test.rpx,即可展現報表,如圖:

潤乾V5與springBoot內建

至此,潤乾V5內建到springBoot成功。

PS:工程啟動後,日中中可能有錯誤資訊:

INFO: Error in building

org.jdom.JDOMException: Error in building

    at org.jdom.input.SAXBuilder.build(SAXBuilder.java:373)

    at org.jdom.input.SAXBuilder.build(SAXBuilder.java:673)

    at com.raqsoft.report.view.ServletMappings.read(Unknown Source:23)

    at com.raqsoft.report.view.ReportServlet._$1(Unknown Source:1403)

    at com.raqsoft.report.view.ReportServlet.loadConfig(Unknown Source:641)

    at com.raqsoft.report.view.ReportServlet.loadConfig(Unknown Source:155)

    at com.raqsoft.report.view.ReportServlet.init(Unknown Source:138)

原因:潤乾原來部署方式一般是按照java web應用方式部署,是以需要web.xml,潤乾的程式中會找該檔案,而springBoot中不需要該檔案,是以雖說報表能夠正常展現,但是背景會有錯誤,解決辦法:

1、随便弄個web.xml放到WEB-INF下

2、替換最新版raqsoftReport.jar(2018年3月份之後的,找潤乾客服擷取)

總結:潤乾V5部署在springBoot上和和他常見部署最大的不同就是servlet的加載,在springBoot中要通過程式加載相應的servlet,加載方式也有多種,到時候根據實際情況進行設定即可。

繼續閱讀