我正在使用Apache Tomcat 7.0.54
和Spring MVC 4.0.1
并尝试使用DispatcherServlet
配置Java
:
我已经编写了配置类:
public class AppInitializer extends AbstractDispatcherServletInitializer{
@Override
protected WebApplicationContext createServletApplicationContext() {
XmlWebApplicationContext context = new XmlWebApplicationContext();
context.setConfigLocation("/WEB-INF/her-servlet.xml");
return context;
}
@Override
protected String[] getServletMappings() {
return new String[] {"*.her"};
}
@Override
protected WebApplicationContext createRootApplicationContext() {
return null;
}
}
并且它不起作用。但如果我写下面的话:
public class AppInitializer implements WebApplicationInitializer{
@Override
public void onStartup(ServletContext sc) throws ServletException {
XmlWebApplicationContext context = new XmlWebApplicationContext();
context.setConfigLocation("/WEB-INF/her-servlet.xml");
ServletRegistration.Dynamic registration = sc.addServlet("her", new DispatcherServlet(context));
registration.setLoadOnStartup(1);
registration.addMapping("*.her");
}
}
它会正常工作。我不明白这个问题。你能帮我解决一下吗?