I’m trying to learn Spring MVC basics and having issues with the way URL are resolved to controllers. I am working with this tutorial as a springboard. I can get it operating fine under tomcat and the url /context/welcome.htm resolves to the controller that is annotated as such:
@Controller
@RequestMapping("/welcome")
public class HelloWorldController
The web.xml is configured using
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>*.htm</url-pattern>
</servlet-mapping>
I would like to be able to type /context/welcome and resolve the HelloWorldController but this doesnt work. I have tried setting the * but this causes the servlet to fail to load. If someone could explain why this url pattern is invalid, and how to configure this controller to work with all requests such as /welcome and /welcome instead of just those ending with *.htm it would be greatly appreciated. In ASP.NET MVC,a routing page was provided to debug such controller mapping issues. What is the best way to do this with Spring?
Try this :
The pattern /* will put everything through your servlet.
The pattern / makes your servlet the default servlet for the app, meaning it will pick up every pattern that doesn’t have another exact match.