Naor
Naor

Reputation: 24083

spring mvc url formatting

I am using spring mvc. I have 3 pages in my site (actually the controller handle those requests):

localhost/post.html
localhost/search.html
localhost/list.html

I would like the url to be localhost/XXX/post.html where XXX is a parameter that will transffered as a parameter to the controller method. For example, if the user asks localhost/bla/post.html then the controller method of /post will get bla as parameter.

Is this possible in spring mvc?

Upvotes: 0

Views: 1185

Answers (3)

e-zinc
e-zinc

Reputation: 4581

It easy can be done using annotations:

@Controller
public class MyController
{   

    @RequestMapping(value = "/{id}/post", method = RequestMethod.POST)
    public String post(@PathVariable("id") String id)
    {
    }

    @RequestMapping(value = "/{id}/search", method = RequestMethod.GET)
    public String search(@PathVariable("id") String id)
    {
    }

    @RequestMapping(value = "/{id}/list", method = RequestMethod.GET)
    public String list(@PathVariable("id") String id)
    {
    }

}

Upvotes: 0

Affe
Affe

Reputation: 47984

Yes, and quite easy to do. When you define the mapping for the handler URL you can put a variable into the path like this:

@RequestMapping("/{blah}/post.html")
public ModelAndView handleRequest(@PathVariable("blah") String blah) {
  //
}

Spring will set the value of the String for you when it calls the method.

(While not strictly necessary, it is also normal to have a subdirectory for the dispatcher servlet in your paths. Mapping a dispatcher to the root can be a pain. Messes up static resource access, for example. e.g., localhost/myapp/blah/post.html)

Upvotes: 2

Eugene Burtsev
Eugene Burtsev

Reputation: 1475

In JSF it may be done with PrettyFaces library. May be it would help you.

Upvotes: 0

Related Questions