如何把web.xml中的context-param、Servlet、Listener和Filter定義添加到SpringBoot中

把傳統的web項目遷移到SpringBoot中,少不了web.xml中的context-param、Servlet、Filter和Listener等定義的遷移。 對於Servlet、Filter和Listener相關定義轉換相對來講比較明確:web

Servlet定義的遷移

通常servlet的遷移

@WebServlet("/jsonIndexSearchServlet")
public class JsonIndexSearchServlet extends HttpServlet {
    ...
}

FacesServlet的遷移

@Bean
    public ServletRegistrationBean servletRegistrationBean() {
        FacesServlet servlet = new FacesServlet();
        //, "*.jsf"
        ServletRegistrationBean bean = new ServletRegistrationBean(servlet);
        bean.setOrder(40);
        bean.setName("FacesServlet");
        List<String> urlPattern = new ArrayList<>();
        urlPattern.add("*.jsf");
        bean.setUrlMappings(urlPattern);
        return bean;
    }

Listener定義的遷移

@Bean
    public ServletListenerRegistrationBean<ServletContextListener> setStartupServletContextListener(){
        ServletListenerRegistrationBean<ServletContextListener> result = new ServletListenerRegistrationBean<>();
        result.setListener(new StartupServletContextListener());
        result.setOrder(20);
        return result;
    }

Filter定義的遷移

@Bean
    public FilterRegistrationBean rewriteFilter() {
        FilterRegistrationBean rwFilter = new FilterRegistrationBean(new RewriteFilter());
        rwFilter.setDispatcherTypes(EnumSet.of(DispatcherType.FORWARD, DispatcherType.REQUEST,
                DispatcherType.ASYNC, DispatcherType.ERROR));
        rwFilter.addUrlPatterns("/*");
        rwFilter.setOrder(30);
        return rwFilter;
    }

context-param定義的遷移

@Bean
    public InitParameterConfiguringServletContextInitializer initParamsInitializer() {
        Map<String, String> contextParams = new HashMap<>();
        contextParams.put("org.apache.myfaces.AUTO_SCROLL", "true");
        return new InitParameterConfiguringServletContextInitializer(contextParams);
    }
相關文章
相關標籤/搜索