Does Spring MVC support extension-less URLs?

2020-04-10 02:08发布

问题:

Does Spring MVC support extension-less URLs like asp.net MVC does?

I am just getting started with Spring MVC.

Or maybe has nothing to do with Spring MVC but Tomcat?

回答1:

Yes, it does.

If you are using annotations, you annotate your controller methods or classes with something like:

@RequestMapping("/hello")

Which will respond to /hello when you map the dispatcher servlet like so:

<servlet-mapping>
    <servlet-name>dispatcher</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>


回答2:

You must set this in your /WEB-INF/web.xml:

<servlet-mapping>
  <servlet-name>dispatcher</servlet-name>
  <url-pattern>/</url-pattern>
</servlet-mapping>

(assuming your DispatcherServlet is named "dispatcher").