tags:

views:

46

answers:

3

How do I get nice-looking URLs in Spring (without a UrlRewriteFilter)? Does Spring 3 have any convenient way of getting nice-looking URLs (like, annotations, or something)?

For example:

/springmvc/hello instead of /springmvc/hello_world.html.

+1  A: 

This part of your web.xml determines which requests are routed to the springmvc servlet - any ending .html in the tutorial.

<servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>*.html</url-pattern>
</servlet-mapping>

Change the url-pattern element to you prefer more. Try <url-pattern>/*</url-pattern> to route the requests you're mentioned without any suffix.

Brabster
`<url-pattern>/*</url-mapping>` messes up the rest of my app :(
Mr. X
A: 

The ControllerClassNameHandlerMapping is a great way to use convention over configuration to simplify your URL bindings.

James Earl Douglas
A: 

We use annotations in our RESTful services implemented by Spring MVC controllers. I'm not sure this is exactly what you are looking for, but our code look like this:

@Controller
@RequestMapping("/contact")
public class ContactController {
// ...
    @RequestMapping(value = "/info", method = RequestMethod.GET)
    public @ResponseBody
    ContactInfo getContactInfo() {
        return contactDelegate.getContactInfo();
    }

}

The URL for accessing this controller will be http://foo.com/about/info.

You will need to put <mvc:annotation-driven /> in your application context XML

Kristoffer E