2013-07-12 161 views
1

您好我正在嘗試使用Spring MVC Java配置和內容視圖解析器。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在/或任何其他路徑。

我想知道這裏出了什麼問題。

感謝您的幫助。

回答

1

我知道它的老問題。使用較新的Spring Boot,現在配置任何Spring應用程序變得更加容易。請參閱此處的Spring Boot啓動器項目: -

Spring Boot Blog