我正在使用 maven + spring + hibernate 构建一个无 xml 文件 webapp 并从Minimal Tomcat 7 embedding example开始
应用结构:
webapp
|_src/main/java
| |_com.myapp.test
| |_Main.java
| |_HelloController.java
| |_MvcConfig.java
|_src/main/resources
| |_hello.jsp
|_src/test/java
|_src/test/resources
HelloController.java
@Controller
@RequestMapping("/welcome")
public class HelloController {
@RequestMapping(method = RequestMethod.GET)
public String printWelcome(ModelMap model) {
model.addAttribute("message", "Spring 3 MVC Hello World");
return "hello";
}
MvcConfig.java
@Configuration
@EnableWebMvc
public class MvcConfig {
@Bean
public InternalResourceViewResolver configureInternalResourceViewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/");
resolver.setSuffix(".jsp");
return resolver;
}
}
在尝试添加弹簧功能时Main.java
Tomcat tomcat = new Tomcat();
tomcat.setPort(9090);
File base = new File("");
System.out.println(base.getAbsolutePath());
Context rootCtx = tomcat.addContext("", base.getAbsolutePath());
AnnotationConfigWebApplicationContext aactx = new AnnotationConfigWebApplicationContext();
aactx.scan("com.myapp");
aactx.register(MvcConfig.class);
DispatcherServlet dispatcher = new DispatcherServlet(ctx);
Tomcat.addServlet(rootCtx, "SpringMVC", dispatcher);
rootCtx.addServletMapping("/*", "SpringMVC");
tomcat.start();
然后我在localhost:9090/welcome.jsp
检查嵌入式服务器时遇到了这个错误
Jun 04, 2013 4:34:39 PM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/hello.jsp] in DispatcherServlet with name 'SpringMVC'
Jun 04, 2013 5:00:38 PM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/favicon.ico] in DispatcherServlet with name 'SpringMVC'
为什么我收到此错误以及如何解决?
提前致谢!