First off, if this is a duplicate, I apologize. I did quite a bit of searching but couldn't find much and I suspect I wasn't using the right terms...
I'm building a site with Spring MVC and using the annotation-driven configuration. What I'd like to do is have urls that do not have any extension at the end (.html, .do, etc). So they would look like http://www.mysite.com/account/create
which I know is something that is traditionally accomplished using mod_rewrite
on Apache or using files without extensions. It seems as though it could be done without using a rewrite engine (I know about the urlrewritefilter project) as Spring has support for pulling parameters out of the request string (See section 15.3.2.1 of the documentation) and the pet store example doesn't appear to have extensions at the end of its urls.
However, it seems as though whenever I attempt to forward all requests to the dispatcher servlet (my thinking for how to negate the need for something like *.htm
), I run into trouble...I'm able to get it work (using XML configuration, I recently switched to annotation configuration) using just a standard "*.htm" for all of the pages.
My controller code looks like
@Controller
@RequestMapping("/home")
public class HomeController {
...
@RequestMapping(method = RequestMethod.GET)
public ModelAndView get() {
...
}
And my web.xml looks like
<servlet>
<servlet-name>dispatch</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatch</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
Am I missing something here? Is this possible without using a rewrite engine?
I should note, the above configuration doesn't work. It always returns a 404
error...
Using URL patterns such as /*
is generally a bad idea, since the container will forward everything to the DispatcherServlet
, including internal requests for JSPs. So when your controller returns the JSP name in the ModelAndView
, it will be fed back into the DispatcherServlet
again, giving you the 404.
You either need to retain the file extension in your paths, or you need a prefix of some kind to allow you to distinguish requestd for the DispstcherServlet
from requests for JSPs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With