SpringBoot中整合Struts2的方案

SpringBoot中整合Struts2的方案

前言

目前公司有一個運營端的專案,web層使用框架,是年代久遠的Struts2,近期打算改造為SpringMVC,而SpringMVC這個稱呼,也成為了一個歷史了,目前是SpringBoot中的web模組中部分功能而已了,所以系統架構準備一步到位,改造為SpringBoot的體系。

而改造的第一步,便是將SpringBoot平穩引入,便是本文要將的在SpringBoot中整合Struts2的相關內容了。

思路

總結下SpringBoot中整合Struts2的思路,後續再按照這個步驟,一步一步來詳細闡述。

引入Struts2的Maven依賴;

引入傳統struts。xml的配置

使用註解的方式,完成傳統web。xml的中Struts2配置

基於Struts2編寫HTTP介面測試;

測試HTTP介面;

案例

按照思路的方案,編寫了一個測試的demo,驗證了方案的可行性。

SpringBoot中整合Struts2的方案

1.引入依賴

我們主要引入Struts2的Maven依賴,具體內容如下:

<!—— struts2 依賴 ——> org。apache。struts struts2-core 2。3。28 org。apache。struts struts2-spring-plugin 2。3。28 org。apache。struts struts2-convention-plugin 2。3。28

2.配置struts.xml

傳統的Struts2的專案,都要在資源目錄下面,配置一個命名為struts。xml的配置檔案,配置Struts2的Action(HTTP介面)相關的屬性。

<?xml version=“1。0” encoding=“UTF-8”?><!DOCTYPE struts PUBLIC “-//Apache Software Foundation//DTD Struts Configuration 2。3//EN” “http://struts。apache。org/dtds/struts-2。3。dtd”> <!—— spring工廠 ——> <!—— struts的action包掃描路徑 ——> <!—— 解決There is no Action mapped for namespace [/xxx] and action name [xxx] associated with context path []。等問題 ——>

3.配置web.xml

作為早期JavaWeb開發的同學都知道,我們需要在裡邊配置一些請求相關的過濾器,等等。只不過到了SpringBoot時代,這個檔案的配置,早已被遺棄了,我們現在採用註解式配置,來完成這個不起眼的檔案的相關配置。

package com。example。springbootstruts2。config;import org。apache。struts2。dispatcher。ng。filter。StrutsPrepareAndExecuteFilter;import org。springframework。boot。web。servlet。FilterRegistrationBean;import org。springframework。context。annotation。Bean;import org。springframework。context。annotation。Configuration;import java。util。ArrayList;import java。util。List;/** * Struts的web。xml配置 * * @author hongcunlin */@Configurationpublic class WebXmlConfig { /** * 官網的方法配置過濾器 * * @return Struts2配置 */ @Bean public FilterRegistrationBean filterRegistrationBean() { FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(); // 加入Struts2 filterRegistrationBean。setFilter(new StrutsPrepareAndExecuteFilter()); List list = new ArrayList(); list。add(“/*”); filterRegistrationBean。setUrlPatterns(list); return filterRegistrationBean; }}

4.編寫HTTP介面

到了檢驗以上配置是否生效的重要環節了,我們基於Struts2編寫一個HTTP介面,並進行介面測試,如若能按照預期的返回,則證明沒有問題。

package com。example。springbootstruts2。action;import com。alibaba。fastjson2。JSON;import com。example。springbootstruts2。vo。BaseResult;import lombok。Data;import org。apache。struts2。ServletActionContext;import org。apache。struts2。convention。annotation。Action;import org。apache。struts2。convention。annotation。Namespace;import org。springframework。stereotype。Controller;import javax。servlet。ServletOutputStream;import java。io。IOException;import java。nio。charset。StandardCharsets;/** * @author hongcunlin */@Controller@Namespace(“/word”)@Datapublic class WordAction { /** * Struts2方法請求入參 */ private String word; /** * struts請求 * * @throws IOException ex */ @Action(value = “/get”) public void get() throws IOException { ServletOutputStream os = ServletActionContext。getResponse()。getOutputStream(); BaseResult result = new BaseResult(); result。setMsg(word); String json = JSON。toJSONString(result); os。write(json。getBytes(StandardCharsets。UTF_8)); }}

我們簡單編寫了一個http介面,將使用者輸入的值,傳入一個物件中,再返回一個json格式的內容給,這是前後端分離常見的一個互動,很重要。

5.測試HTTP介面

我們在瀏覽器上面,輸入HTTP介面相關的內容,

SpringBoot中整合Struts2的方案

可以看到,返回我們預期想要的結果,說明我們的環境是OK的,SpringBoot中成功基礎了Struts2了

最後

本文作為Struts2改造SpringBoot的第一部分,分享瞭如何在SpringBoot中整合Struts2。完整的切換改造過程,還有很長的路要走,如過濾器相關的改造、檔案上傳改造、基於類的HTTP介面改造為基於方法的,等等,後續再做分享。

SpringBoot中整合Struts2的方案