您好我正在尝试使用 Spring MVC Java 配置和内容视图解析器。
文件:-
public class WebInitializer implements WebApplicationInitializer {
/*
* (non-Javadoc)
*
* @see
* org.springframework.web.WebApplicationInitializer#onStartup(javax.servlet
* .ServletContext)
*/
@Override
public void onStartup(ServletContext servletContext)
throws ServletException {
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(SpringMvcConfig.class);
ctx.setServletContext(servletContext);
ctx.refresh();
Dynamic servlet = servletContext.addServlet("dispatcher",
new DispatcherServlet(ctx));
servlet.addMapping("/");
servlet.setLoadOnStartup(1);
servlet.setAsyncSupported(false);
}
}
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = { "com.sambhav.mvc.controller" })
public class SpringMvcConfig extends WebMvcConfigurerAdapter {
/*
* @Bean public UrlBasedViewResolver setupViewResolver() {
* UrlBasedViewResolver resolver = new UrlBasedViewResolver();
* resolver.setPrefix("/WEB-INF/jsp/"); resolver.setSuffix(".jsp");
* resolver.setViewClass(TilesView.class); return resolver; }
*/
@Bean
public TilesViewResolver getTilesViewResolver() {
TilesViewResolver tilesViewResolver = new TilesViewResolver();
/*
* tilesViewResolver.setPrefix("/WEB-INF/jsp/");
* tilesViewResolver.setSuffix(".jsp");
*/
tilesViewResolver.setViewClass(TilesView.class);
return tilesViewResolver;
}
@Bean
public TilesConfigurer getTilesConfigurer() {
TilesConfigurer tilesConfigurer = new TilesConfigurer();
tilesConfigurer.setCheckRefresh(true);
tilesConfigurer.setDefinitions(new String[] { "/WEB-INF/tiles.xml" });
return tilesConfigurer;
}
@Override
public void configureContentNegotiation(
ContentNegotiationConfigurer configurer) {
configurer.defaultContentType(MediaType.TEXT_HTML)
.mediaType("json", MediaType.APPLICATION_JSON)
.mediaType("xml", MediaType.APPLICATION_XML)
.favorPathExtension(true); // default is true. just for clarity
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/css/*").addResourceLocations(
"/WEB-INF/css/*");
registry.addResourceHandler("/js/*").addResourceLocations(
"/WEB-INF/js/*");
registry.addResourceHandler("/img/*").addResourceLocations(
"/WEB-INF/img/*");
}
}
尝试运行应用程序时,我没有注册调度程序 servlet - 没有出现相关信息日志。在带有断点的调试模式下运行 WebInitializer 也表明它没有被调用。
Jul 12, 2013 5:06:57 PM com.springsource.tcserver.serviceability.rmi.JmxSocketListener init
INFO: Started up JMX registry on 127.0.0.1:6969 in 68 ms
Jul 12, 2013 5:06:57 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 573 ms
Jul 12, 2013 5:06:57 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Jul 12, 2013 5:06:57 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: VMware vFabric tc Runtime 2.8.2.RELEASE/7.0.35.B.RELEASE
Jul 12, 2013 5:06:57 PM org.apache.catalina.startup.HostConfig deployDescriptor
INFO: Deploying configuration descriptor D:\Installations\sts3.2\vfabric-tc-server-developer-2.8.2.RELEASE\base-instance\conf\Catalina\localhost\spring-mvc-config.xml
Jul 12, 2013 5:06:57 PM org.apache.catalina.startup.SetContextPropertiesRule begin
WARNING: [SetContextPropertiesRule]{Context} Setting property 'source' to 'org.eclipse.jst.j2ee.server:spring-mvc-config' did not find a matching property.
Jul 12, 2013 5:06:57 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory D:\Installations\sts3.2\vfabric-tc-server-developer-2.8.2.RELEASE\base-instance\webapps\manager
Jul 12, 2013 5:06:57 PM org.apache.catalina.startup.HostConfig deployDirectory
INFO: Deploying web application directory D:\Installations\sts3.2\vfabric-tc-server-developer-2.8.2.RELEASE\base-instance\webapps\ROOT
Jul 12, 2013 5:06:57 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-bio-8080"]
Jul 12, 2013 5:06:57 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 311 ms
这导致 404 at / 或任何其他路径。
我想知道这里出了什么问题。
谢谢你的帮助。