我有一个Spring 4 MVC应用程序。当我尝试访问任何URL时,应用程序始终会重定向到根文件夹。
我的主要课程:
public class SiteMain implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext container) throws ServletException {
AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext();
dispatcherContext.setConfigLocation("com.mj.cchp.MvcConfig");
container.addListener(new ContextLoaderListener(dispatcherContext));
ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher", new DispatcherServlet(dispatcherContext));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
}
}
和MVC配置是:
@Configuration
@EnableWebMvc
@ComponentScan
@ImportResource({ "classpath:/WEB-INF/site.xml" })
public class MvcConfig extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/").setCachePeriod(31556926);
}
}