把傳統的web項目遷移到SpringBoot中,少不了web.xml中的context-param、Servlet、Filter和Listener等定義的遷移。 對於Servlet、Filter和Listener相關定義轉換相對來講比較明確:web
##Servlet定義的遷移 ###通常servlet的遷移apache
@WebServlet("/jsonIndexSearchServlet") public class JsonIndexSearchServlet extends HttpServlet { ... }
###FacesServlet的遷移json
@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定義的遷移app
@Bean public ServletListenerRegistrationBean<ServletContextListener> setStartupServletContextListener(){ ServletListenerRegistrationBean<ServletContextListener> result = new ServletListenerRegistrationBean<>(); result.setListener(new StartupServletContextListener()); result.setOrder(20); return result; }
##Filter定義的遷移url
@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定義的遷移spa
@Bean public InitParameterConfiguringServletContextInitializer initParamsInitializer() { Map<String, String> contextParams = new HashMap<>(); contextParams.put("org.apache.myfaces.AUTO_SCROLL", "true"); return new InitParameterConfiguringServletContextInitializer(contextParams); }