2013-07-12 5 views
1

안녕하세요, Spring MVC Java config 및 Content View resolver를 사용하려고합니다.Spring MVC Java Configuration

파일 : -

@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/*"); 
    } 

} 

응용 프로그램을 실행하려고, 나는 등록하기 디스패처 서블릿을하지

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); 
} 

} - 더 관련 정보 로그가 표시되지 않습니다. 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

0

가능한 이유 :

  1. 의 web.xml. 나는 그것을 삭제하고 3.0보다 높은 버전의 servlet-api를 사용하도록 조언한다.

  2. 컨트롤러의 @RequestMapping이 잘못되었습니다. 보여줘, 제발.

그리고 마지막으로, 여기 당신은 자바 구성 기본 스프링 MVC 응용 프로그램에 대한 좋은 단계별 가이드를 찾을 수 있습니다 : http://www.javacodegeeks.com/2013/03/spring-mvc-creation-of-a-simple-controller-with-java-based-config.html

관련 문제