spring boot兼容struts2、Http Servlet、web.xml

本文介绍了如何在Spring Boot项目中兼容老项目的Struts2和原始Servlet,包括自动导入web.xml中的Servlet配置,以及解决Struts2与Servlet 3.1不兼容导致的文件上传问题。
摘要由CSDN通过智能技术生成

有些老项目还在使用原始的Http Servlet或是struts2, 而新功能都期望使用spring, 而spring已经不使用web.xml配置了. 折中的解决方法:

整合struts2

@SpringBootApplication
public class Main {
   
    public static void main(String[] args) {
   
        SpringApplication.run(Main.class, args);
    }
    @Bean
    public FilterRegistrationBean registration() {
   
    	//web.xml中的struts filter移到代码中
        StrutsPrepareAndExecuteFilter filter = new StrutsPrepareAndExecuteFilter();
        FilterRegistrationBean<StrutsPrepareAndExecuteFilter> registration =
                new FilterRegistrationBean<>(filter);
        registration.addUrlPatterns("/test/*");
        registration.addInitParameter("config", "struts-default.xml,struts2/struts-test.xml");
        return registration;
    }
}

兼容原始的Servlet

public class MyServlet1 extends HttpServlet {
   
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
   
		resp.getWriter().write("hello servlet1");
	}
}

public class MyServlet2 extends HttpServlet {
   
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
   
		resp.getWriter().write("hello servlet2");
	}
}


@SpringBootApplication
public class Main {
   
    public static void main(String[] args) {
   
        SpringApplication.run(Main.class, args);
    }
    
    @Bean
    public ServletRegistrationBean<HttpServlet> helloServlet1() {
   
        ServletRegistrationBean<HttpServlet> servRegBean = new ServletRegistrationBean<>();
        servRegBean.setServlet(new MyServlet1());
        servRegBean.addUrlMappings("/servlet1");
        return servRegBean;
    }

    @Bean
    public ServletRegistrationBean<HttpServlet> helloServlet2() {
   
        ServletRegistrationBean<HttpServlet> servRegBean = new ServletRegistrationBean<>();
        servRegBean.setServlet(new MyServlet2
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值