I'm using maven + spring + hibernate to build a xml-free file webapp and started at Minimal Tomcat 7 embedding example
application structure:
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;
}
}
while trying to adding spring feature in 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();
then I got this error when go to localhost:9090/welcome.jsp
to check out the embedded server
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'
why I got this error and how to solve it?
Thanks in advance!