日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

strtus2改成springboot_ssh框架使用springBoot升级迁移替换Struts2

發布時間:2024/9/27 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 strtus2改成springboot_ssh框架使用springBoot升级迁移替换Struts2 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

如果一個十來年的ssh項目想使用springboot的便捷性,但又不想從零開始,那么就讓我們開始遷移吧。

在pom文件添加springboot 依賴

創建application 啟動類

遷移web.xml

web.xml文件中定義了filter, struts2等等一堆filter, 這時我們需要做的一件事是在springboot中使我們的filter, struts2 能正常work。 這樣就不需要我們改變項目中已經定義好的大量的action、controller層代碼.

//聲明當前類為一個配置類

@Configuration

//自動掃描包路徑

@ComponentScan({"com.demo.action", "com.demo.util","com.demo.security"})

//引入之前已經存在的配置文件

@ImportResource(locations = {

"classpath:applicationContext-configuration.xml",

"classpath:applicationContext-orm.xml",

"classpath:applicationContext-business.xml",

"classpath:ApplicationContext-RabbitMQ.xml",

"classpath:applicationContext-security-authorization.xml",

})

public class ApplicationConfig {

private static final Logger log = LoggerFactory.getLogger(ApplicationConfig.class);

private LogRequestFilter logRequestFilter = new LogRequestFilter();

private StrutsPrepareAndExecuteFilter strutsPrepareAndExecuteFilter = new StrutsPrepareAndExecuteFilter(); //springboot啟動時初始化struts2攔截器

@Bean

@Order(1)

public FilterRegistrationBean loginFilter() {

/*

* * LoginFilter

* com.demo.servlet.LoginFilter

*

* * LoginFilter

* /login/*

* /login.html

*

*/

FilterRegistrationBean registrationBean = new FilterRegistrationBean();

registrationBean.setFilter(new LoginFilter());//注冊攔截filter

registrationBean.addUrlPatterns("/login/*", "/login.html");//攔截路徑

return registrationBean;

}

@Bean

@Order(2)

public FilterRegistrationBean strutsPrepareAndExecuteFilter() {

/*

* * struts2

* org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter

*

* * struts2

* /*

*

* * struts2

* FORWARD

*

*/

log.debug("Instantiating StrutsPrepareAndExecuteFilter");

FilterRegistrationBean registrationBean = new FilterRegistrationBean();

registrationBean.setFilter(strutsPrepareAndExecuteFilter);//注冊Struts2攔截器

registrationBean.setName("StrutsPrepareAndExecuteFilter");

registrationBean.addUrlPatterns("/*");//默認攔截所有請求

return registrationBean;

}

}

通過以上code, 我們已經將web.xml的配置完全遷移,這時我們通過springboot 已經可以開始啟動項目了。

當然,在springboot中使用Struts2是不合理的也是官方不支持使用的, 后續的工作我們可以逐步整合hiberante, springsecurity,最后將Struts2替換成springmvc. 慢慢減少我們的配置文件。

(1)FilterRegistrationBean: spring 對filter 攔截器的一個注冊類, 自帶setorder方法, [email?protected]�動和攔截的優先級。

(2)@Order 控制filter的啟動和攔截的優先級。

(3)@Bean注解用于告訴方法,產生一個Bean對象,然后這個Bean對象交給Spring管理。產生這個Bean對象的方法Spring只會調用一次,隨后這個Spring將會將這個Bean對象放在自己的IOC容器中。

(4) StrutsPrepareAndExecuteFilter 這個類就不多說了,相信大部分使用struts2的小伙伴都知道這是struts2的啟動類

(5) @ImportResource等同于xml配置。參考:https://blog.csdn.net/jiaobuchong/article/details/50530027

(6)@Configuration 用于定義配置類,可替換xml配置文件,[email?protected]法,這些方法將會被AnnotationConfigApplicationContext或AnnotationConfigWebApplicationContext類進行掃描,并用于構建bean定義,初始化Spring容器。

參考: https://blog.csdn.net/qq_35981283/article/details/77826537

https://www.jianshu.com/p/3bd687e9d1e7

springboot 系列教程:

https://blog.csdn.net/softwave/article/details/77152373

總結

以上是生活随笔為你收集整理的strtus2改成springboot_ssh框架使用springBoot升级迁移替换Struts2的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。