【发布时间】:2013-02-08 07:57:48
【问题描述】:
我跟着this example用Spring的WebApplicationInitializer通过java配置了我的DispatcherServlet --> javax.servlet.ServletContainerInitializer:
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
AnnotationConfigWebApplicationContext mvcContext = new AnnotationConfigWebApplicationContext();
mvcContext.register(MyConfiguration.class);
ServletRegistration.Dynamic appServlet = servletContext.addServlet("appServlet", new DispatcherServlet(mvcContext));
appServlet.setLoadOnStartup(1);
Set<String> mappingConflicts = appServlet.addMapping("/");
if (!mappingConflicts.isEmpty()) {
for (String s : mappingConflicts) {
LOGGER.error("Servlet URL mapping conflict: {}", s);
}
throw new IllegalStateException("'appServlet' cannot be mapped to '/'");
}
}
当我启动 Tomcat 时,我得到了上面的 IllegalStateException,因为 apparently there is already a Servlet 映射到 /,我只能假设这是 Tomcat 的默认 Servlet。如果我忽略 mappingConflicts,我的 DispatcherServlet 不会映射到任何东西。
有什么方法可以用我自己的方法覆盖这个默认的 servlet 映射,还是我卡住了将我的 DispatcherServlet 映射到 /*?
This answer 通过更改您的应用程序在 Catalina webapps 文件夹中的部署位置提供了一个解决方案,但我希望能够减少干扰。
【问题讨论】:
标签: java servlets spring-mvc servlet-3.0