我使用教程mkyong使用注释配置创建spring mvc项目。但是当我运行项目时我有错误404
,浏览器消息显示HTTP状态404 -
type Status report
message
description The requested resource is not available.
堆栈跟踪
[2015-09-05 03:22:42,179] Artifact WebsJpa:war: Artifact is deployed successfully
[2015-09-05 03:22:42,180] Artifact WebsJpa:war: Deploy took 2 433 milliseconds
05-Sep-2015 15:22:42.350 WARNING [http-nio-8080-exec-1] org.springframework.web.servlet.PageNotFound.noHandlerFound No mapping found for HTTP request with URI [/] in DispatcherServlet with name 'dispatcher'
05-Sep-2015 15:22:42.370 WARNING [http-nio-8080-exec-2] org.springframework.web.servlet.PageNotFound.noHandlerFound No mapping found for HTTP request with URI [/] in DispatcherServlet with name 'dispatcher'
05-Sep-2015 15:22:44.252 WARNING [http-nio-8080-exec-4] org.springframework.web.servlet.PageNotFound.noHandlerFound No mapping found for HTTP request with URI [/] in DispatcherServlet with name 'dispatcher'
05-Sep-2015 15:22:44.742 WARNING [http-nio-8080-exec-5] org.springframework.web.servlet.PageNotFound.noHandlerFound No mapping found for HTTP request with URI [/favicon.ico] in DispatcherServlet with name 'dispatcher'
控制器的
@Controller
public class WebController {
@RequestMapping(value = "/", method = RequestMethod.GET)
public String index() {
System.out.println("Hello");
return "index";
}
}
SpringWebConfig
@EnableWebMvc
@Configuration
@ComponentScan({ "service"})
public class SpringWebConfig extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**")
.addResourceLocations("/resources/");
}
@Bean
public InternalResourceViewResolver viewResolver() {
InternalResourceViewResolver viewResolver
= new InternalResourceViewResolver();
viewResolver.setViewClass(JstlView.class);
viewResolver.setPrefix("/WEB-INF/jsp/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
}
SpringRootConfig
@Configuration
@ComponentScan({ "service" })
public class SpringRootConfig {
}
MyWebInitializer
public class MyWebInitializer extends
AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[] { SpringRootConfig.class };
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[] { SpringWebConfig.class };
}
@Override
protected String[] getServletMappings() {
return new String[] { "/" };
}
}