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!
What if you change
to
Before getting started, make the adjustment that @RobBarreca points out. It should be aactx not ctx.
There are two options when doing Spring with embedded Tomcat (7.0): addContext and addWebapp. The latter is really the recommended way for getting started whereas addContext is the more advanced, gotta have control route. It is fine that you use addContext, but there is more configuration information that you have to specify. And I will focus on addContext in this answer.
You are really only missing one piece above and that is what Servlet Class will be used for JSP files? I would imagine that you probably have a file called welcome.jsp and it may well be in the correct place. However, your above code will say how to process your Controller correctly only.
The code that you would need to add to handle JSP files is as follows:
Actually you do need a second thing and that is to alter you servlet mapping for SpringMVC. It should be
If you did star, then it might try to handle jsp's for you, which it can't do even if your new JspServlet Servlet Class has been added.
Now this works, but one additional improvement you might want to make depending upon what content your actually serving is to specify a View Class in MvcConfig.java. This, for example, could allow you to handle jstl content if you did:
but there are many other cases where setting a View Class is preferable.
I am pretty sure that the suffix you have set to "/" is relative "WEB-INF". Try creating webapp/src/main/webapp/WEB-INF/ and place hello.jsp there.