美文网首页
记录struts2 老旧项目升级 springboot 2.0+

记录struts2 老旧项目升级 springboot 2.0+

作者: 硫克的苹果 | 来源:发表于2020-10-21 10:28 被阅读0次

    不需要改造目录结构的升级方法

    1.pom.xml文件升级

    1.1添加依赖的SpringBoot包

    <dependency>
                 <groupId>org.springframework.boot</groupId>
                 <artifactId>spring-boot-dependencies</artifactId>
                 <version>${springboot.version}</version>
                 <type>pom</type>
                 <scope>import</scope>
             </dependency>
             <dependency>
                 <groupId>org.springframework.boot</groupId>
                 <artifactId>spring-boot-starter-web</artifactId>
                 <version>${springboot.version}</version>
                 <exclusions>
                     <exclusion>
                         <groupId>ch.qos.logback</groupId>
                         <artifactId>logback-classic</artifactId>
                     </exclusion>
                 </exclusions>
             </dependency>
    
             <!--<dependency>
                 <groupId>org.springframework.boot</groupId>
                 <artifactId>spring-boot-starter-test</artifactId>
                 <version>${springboot.version}</version>
                 <scope>test</scope>
             </dependency>-->
    
             <dependency>
                 <groupId>org.springframework.boot</groupId>
                 <artifactId>spring-boot-starter-tomcat</artifactId>
                 <version>${springboot.version}</version>
                 <scope>provided</scope>
             </dependency>
             <dependency>
                 <groupId>org.springframework.boot</groupId>
                 <artifactId>spring-boot-autoconfigure</artifactId>
                 <version>${springboot.version}</version>
             </dependency>
             <dependency>
                 <groupId>org.apache.tomcat.embed</groupId>
                 <artifactId>tomcat-embed-jasper</artifactId>
                 <version>9.0.24</version>
             </dependency>
    

    1.2 修改打包方式war改成jar

    <packaging>jar</packaging>
    

    2.升级相关的spring全家桶的包版本到5版本

    spring-core,spring-security,spring-webmvc,spring-context,spring-context-support等
    如果用到hibernate,官网要求升级到5,其实可以不用升级,相应的jar包还保留用低版本不升级即可

    3.新增springboot启动主类,任意位置都可以,建议放到官网推荐根目录位置

    @ServletComponentScan(basePackages = "com.xxx.listener.*")//扫描现有监听器包路径
    @SpringBootApplication
    public class PlanningStartApplication extends SpringBootServletInitializer {
        public static void main(String args[]){
            SpringApplication.run(PlanningStartApplication.class,args);
        }
        protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
            return application.sources(PlanningStartApplication.class);
        }
    
    }
    

    4.新增springboot扫描类,用来处理之前web.xml文件,过滤器等

    //声明当前类为一个配置类
    @Configuration
    //自动扫描包路径
    @ComponentScan({"com.xxx1.**", "com.xxx2.*"})
    //引入之前已经存在的配置文件
    @ImportResource(locations = {
            "classpath:application-context.xml",
            "classpath:dispatcher-servlet.xml"
    })
    public class ApplicationConfig {
        private static final Logger log =  Logger.getLogger(ApplicationConfig.class);
        private StrutsPrepareAndExecuteFilter strutsPrepareAndExecuteFilter = new StrutsPrepareAndExecuteFilter(); //springboot启动时初始化struts2拦截器
        @Bean
        public ViewResolver getViewResolver(){
            InternalResourceViewResolver resolver = new InternalResourceViewResolver();
            resolver.setPrefix("/");
            resolver.setSuffix(".jsp");
            resolver.setViewClass(JstlView.class);
            resolver.setOrder(0);
            return resolver;
        }
        @Bean
        @Order(1)
        public FilterRegistrationBean i18nFilter() {
            FilterRegistrationBean registrationBean = new FilterRegistrationBean();
            registrationBean.setFilter(new I18NFilter());//注册拦截filter
            registrationBean.addUrlPatterns("/*");//拦截路径
            return registrationBean;
        }
    
        @Bean
        @Order(2)
        public FilterRegistrationBean currentContextFilter() {
            FilterRegistrationBean registrationBean = new FilterRegistrationBean();
            registrationBean.setFilter(new CurrentContextFilter());//注册拦截filter
            registrationBean.addUrlPatterns("/*");//拦截路径
            return registrationBean;
        }
    
        @Bean
        @Order(3)
        public FilterRegistrationBean loginFilter() {
            FilterRegistrationBean registrationBean = new FilterRegistrationBean();
            registrationBean.setFilter(new LoginFilter());//注册拦截filter
            registrationBean.addUrlPatterns("/*");//拦截路径
            return registrationBean;
        }
    
        @Bean
        @Order(4)
        public FilterRegistrationBean securityFilter() {
            FilterRegistrationBean registrationBean = new FilterRegistrationBean();
            registrationBean.setFilter(new SecurityFilter());//注册拦截filter
            registrationBean.addUrlPatterns("/*");//拦截路径
            return registrationBean;
        }
    
        @Bean
        @Order(5)
        public FilterRegistrationBean strutsPrepareAndExecuteFilter() {
            log.debug("Instantiating StrutsPrepareAndExecuteFilter");
            FilterRegistrationBean registrationBean = new FilterRegistrationBean();
            registrationBean.setFilter(strutsPrepareAndExecuteFilter);//注册Struts2拦截器
            registrationBean.setName("StrutsPrepareAndExecuteFilter");
            registrationBean.addUrlPatterns("/*");//默认拦截所有请求
            return registrationBean;
        }
    
        @Bean
        public ServletRegistrationBean getServletRegistrationBean() {
            ServletRegistrationBean bean = new ServletRegistrationBean(new DispatcherServlet());
            bean.addUrlMappings("*.service");
            return bean;
    
        }
    

    5.老项目用的jsp页面,由于springboot2不再提供jsp解析,需要添加包类来自己处理

         <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
             <artifactId>tomcat-embed-jasper</artifactId>
             <version>9.0.24</version>
        </dependency>
    

    然后再配置类里面添加

        @Bean
        public ViewResolver getViewResolver(){
            InternalResourceViewResolver resolver = new InternalResourceViewResolver();
            resolver.setPrefix("/");
            resolver.setSuffix(".jsp");
            resolver.setViewClass(JstlView.class);
            resolver.setOrder(0);
            return resolver;
        }
    

    6.在application.properties文件里面新增

    spring.main.allow-bean-definition-overriding=true
    

    用来处理相应的bug

    7.监听器处理

    7.1在老的监听器添加注解

    @WebListener
    public class OnlineUserListener implements HttpSessionListener, HttpSessionAttributeListener {
    

    7.2在Spring启动类里面添加注解

    @ServletComponentScan(basePackages = "com.xxx.listener.*")
    

    8.还有一点需要注意的,笔者用的idea,需要在打包里面添加

    image.png

    才能运行成功,否则访问页面会报404

    相关文章

      网友评论

          本文标题:记录struts2 老旧项目升级 springboot 2.0+

          本文链接:https://www.haomeiwen.com/subject/ylreectx.html