简体   繁体   中英

Spring annotation MVC return 404

I have built a Spring MVC skeleton Helloworld app that returns 404. here is the code:

Controller:

@Controller
@RequestMapping("/")
public class HelloWorldController {
  @RequestMapping(value = {"/helloworld**"}, method = RequestMethod.GET)
  public ModelAndView welcomePage() {
    ModelAndView model = new ModelAndView();
    model.setViewName("helloworld");
    return model;
  }
}

Config:

@Configuration
public class MvcWebApplicationInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    @Override
    protected Class<?>[] getRootConfigClasses() {
      return new Class[]{RootConfig.class};
    }

    @Override
    protected Class<?>[] getServletConfigClasses() {
      return new Class[]{ServletConfig.class};
    }

    @Override
    protected String[] getServletMappings() { return new String[]{"/"}; }
}

@EnableWebMvc
@Configuration
@ComponentScan(basePackages =  "com.tmlink.springmvc.controller.*")
public class RootConfig extends WebMvcConfigurerAdapter {
  @Bean
  public InternalResourceViewResolver viewResolver() {
    InternalResourceViewResolver viewResolver
            = new InternalResourceViewResolver();
    viewResolver.setViewClass(JstlView.class);
    viewResolver.setPrefix("/WEB-INF/views/");
    viewResolver.setSuffix(".jsp");
    return viewResolver;
  }
}

@Configuration
public class ServletConfig extends WebMvcConfigurerAdapter {}

There is no XML config and helloworld.jsp is in WEB-INF/views/helloworld.jsp

When requesting http://localhost:8080/helloworld.jsp I get a 404.

The log from Tomcat at startup:

Jun 02, 2015 1:03:56 PM org.springframework.web.context.ContextLoader initWebApplicationContext
INFO: Root WebApplicationContext: initialization started
Jun 02, 2015 1:03:56 PM org.springframework.web.context.support.AnnotationConfigWebApplicationContext prepareRefresh
INFO: Refreshing Root WebApplicationContext: startup date [Tue Jun 02 13:03:56 CEST 2015]; root of context hierarchy
Jun 02, 2015 1:03:56 PM org.springframework.web.context.support.AnnotationConfigWebApplicationContext loadBeanDefinitions
INFO: Registering annotated classes: [class com.tmlink.springmvc.springAdmin.RootConfig]
Jun 02, 2015 1:03:57 PM org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter initControllerAdviceCache
INFO: Looking for @ControllerAdvice: Root WebApplicationContext: startup date [Tue Jun 02 13:03:56 CEST 2015]; root of context hierarchy
Jun 02, 2015 1:03:57 PM org.springframework.web.context.ContextLoader initWebApplicationContext
INFO: Root WebApplicationContext: initialization completed in 924 ms
Jun 02, 2015 1:04:00 PM org.apache.catalina.util.SessionIdGenerator createSecureRandom
INFO: Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [3,122] milliseconds.
Jun 02, 2015 1:04:00 PM org.springframework.web.servlet.DispatcherServlet initServletBean
INFO: FrameworkServlet 'dispatcher': initialization started
Jun 02, 2015 1:04:00 PM org.springframework.web.context.support.AnnotationConfigWebApplicationContext prepareRefresh
INFO: Refreshing WebApplicationContext for namespace 'dispatcher-servlet': startup date [Tue Jun 02 13:04:00 CEST 2015]; parent: Root WebApplicationContext
Jun 02, 2015 1:04:00 PM org.springframework.web.context.support.AnnotationConfigWebApplicationContext loadBeanDefinitions
INFO: Registering annotated classes: [class com.xxxxx.springmvc.springAdmin.ServletConfig]
Jun 02, 2015 1:04:00 PM org.springframework.web.servlet.DispatcherServlet initServletBean
INFO: FrameworkServlet 'dispatcher': initialization completed in 49 ms

Tomcat version: 7.0.56, Java version: 1.7.0.67.

Any idea what is wrong here?

Change to

@RequestMapping("/*")

Found the error:

@ComponentScan(basePackages = "com.tmlink.springmvc.controller.*")

should have been:

@ComponentScan(basePackages = "com.tmlink.springmvc.controller")

in RootConfig class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM