如何整合潤乾報表到spring boot專案
Spring Boot 是由 Pivotal 團隊提供的全新框架,其設計目的是用來簡化新 Spring 應用的初始搭建以及開發過程。該框架使用了特定的方式來進行配置,從而使開發人員不再需要定義樣板化的配置。下面介紹下一個簡單的 springBoot 專案的搭建以及與潤乾 V2018 整合的過程。
PS:如已掌握 springBoot 工程搭建,第一部分可忽略
開發工具:myeclipse 2017
一:maven 構建 springBoot 專案
2、選擇構建工具 Maven Project、Spring Boot 版本,Dependencies
選擇 web,可參考下圖所示:
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,在裡邊增加:
<dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> </dependency> 同樣,工程會自動載入相關的jar包,下載完成後,工程即可支援jsp檔案。 **二:整合潤乾** 1:在src/main下新建目錄webapp,並將潤乾V5設計器安裝後產生的demo應用下的檔案複製到webapp下。springBoot不需要web.xml,所以web.xml不需要複製,並且WEB-INF下的lib目錄不需要複製。 2:整合jar包,springBoot是通過maven管理jar包,在設計器安裝目錄下的\\report\\doc\\zh有個pom.xml,潤乾需要的第三方jar包在該檔案中配置,將潤乾的pom.xml中的內容複製到工程中即可。潤乾自己的jar包需要手動整合到springBoot專案中,整合方法: 在開發工具中,右鍵Referenced Libraries——Build Path——configure Build Path——Add External JARs,在彈出視窗中選擇引入的jar包,本例只引入幾個相關jar包,實際使用時引入除pom.xml中其他所有jar包, ![](http://img.raqsoft.com.cn/e/45a68f63c39e4467a368ccff57587adf.) 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的註冊,本例只介紹潤乾V2018整合到springBoot中的核心操作,所以只需要這一個servlet即可,其他servlet、Filter、Listener等按照相應的方法新增即可。 4、製作測試報表放到src/main/webapp/WEB-INF/reportFiles目錄下。 5、重啟工程,在瀏覽器中訪問:http://localhost:8080/reportJsp/showReport.jsp?rpx=test.rpx,即可展現報表,如圖: ![](http://img.raqsoft.com.cn/e/59088b06fbd849b6b3de1bf1830b111e.) 至此,潤乾V2018整合到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月份之後的,找潤乾客服獲取) **總結:**潤乾部署在springBoot上和和他常見部署最大的不同就是servlet的載入,在springBoot中要通過程式載入相應的servlet,載入方式也有多種,到時候根據實際情況進行設定即可。 著作權歸作者所有。商業轉載請聯絡作者獲得授權,非商業轉載請註明出處。
作者:gxy
連結:http://c.raqsoft.com.cn/article/1534778906951
來源:乾學院
著作權歸作者所有。商業轉載請聯絡作者獲得授權,非商業轉載請註明出處。