views:

1224

answers:

5

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 /*).

+1  A: 

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

mlk
I just tried *, I will try /* now. Thanks
Ezombort
Doesn't seem to work.
Ezombort
+1  A: 
<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
Thanks, I'll have a look at that. I'm using Spring 2.5.x, but I guess it's quite the same. http://static.springsource.org/spring/docs/2.5.x/reference/mvc.html
Ezombort
+2  A: 

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
That worked, thanks. Do you have any idèas why the general url-patterns won't work?
Ezombort
+2  A: 

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
Thanks, that explained a lot.
Ezombort
+2  A: 

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

<url-pattern>/</url-pattern>
ecdragon