在基于 javaconfig 的 Spring 4.0 项目中,如何将某个 URL 的映射添加到 Spring DispatcherServlet 以外的 Servlet。
在我的例子中,我想使用通过 servlet org.h2.server.web.WebServlet
编辑: 在即将推出的 Spring Boot 1.3 中,可以使用配置参数启用 h2console:http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#boot-features-sql-h2-console
启用它就像将这两行添加到您的 application.properties
一样简单:
spring.h2.console.enabled=true
spring.h2.console.path=/console
最佳答案
最简单的方法是使用初始化器直接实现 WebApplicationInitializer 并添加到 onStartup(ServletContext servletContext)
方法后面的代码中;
ServletRegistration.Dynamic h2Servlet = servletContext.addServlet("h2Servlet", new org.h2.server.web.WebServlet());
h2Servlet.setLoadOnStartup(1);
h2Servlet.addMapping("/h2/*");
关于java - Spring JavaConfig : Add mapping for custom Servlet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21244066/