8

I just started with Spring Web MVC. I'm trying to avoid file extenstions in the url. How can i do this? (I'm using Spring 2.5.x)

Bean:

<bean name="/hello.htm" class="springapp.web.HelloController"/>

I want it to be:

<bean name="/hello" class="springapp.web.HelloController"/>

I cannot get it to work. Any ideas?

Edit:

Url-mapping

<servlet-mapping>
    <servlet-name>springapp</servlet-name>
    <url-pattern>*.htm</url-pattern>
</servlet-mapping>

I have tried changing the url-pattern with no luck (* and /*).

Ezombort
  • 1,892
  • 3
  • 16
  • 20

6 Answers6

10

In 3.0, / seems to work. That is...

<url-pattern>/</url-pattern>
ecdragon
  • 101
  • 1
  • 2
8

As far as I know you can't do this if you're using JSP's as your view for controllers.

Because when you pass a model to a JSP, Spring MVC internally performs a 'forward' to the URL of the JSP. If you use <url-pattern>/*</url-pattern> then this forward will also be handled by your DispatcherServlet and not by your JSP view.

What you can do is use <url-pattern>/something</url-pattern> and have your JSP's in a different directory

Tom van Zummeren
  • 9,130
  • 12
  • 52
  • 63
6
<servlet>
    <servlet-name>spring-mvc</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>spring-mvc</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

Then you need to register your urls to be handled by a particular controller. See the following

http://static.springsource.org/spring/docs/2.0.x/reference/mvc.html

Paul Whelan
  • 16,574
  • 12
  • 50
  • 83
4

In Spring 3.2 at least, the accepted answer above is very nearly but not quite what's needed. The web.xml bit below just worked for me, and I'm adding it to the thread here for reference of whoever googles this next...

<servlet-mapping>
  <servlet-name>springmvc</servlet-name>
  <url-pattern>/</url-pattern>
</servlet-mapping>
alarmo
  • 41
  • 2
2

Try first:

<servlet-mapping>
        <servlet-name>spring-mvc</servlet-name>
        <url-pattern>/hello</url-pattern>
</servlet-mapping>

If that doesn't work then problem is somewhere else. Is your Apache set up to forward those urls to Tomcat? Something like:

JkMount /hello worker1
serg
  • 109,619
  • 77
  • 317
  • 330
1

Have you tried <url-pattern>/*</url-pattern> in the servlet mapping and <bean name="/hello" .../> ?

Michael Lloyd Lee mlk
  • 14,561
  • 3
  • 44
  • 81