0

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?

3 Answers 3

2

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)

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks. I didn't understand your last recommendation about the dispatcher servlet. I don't want users of my site to surf to domain/myapp/post.html. I would like them to surf domain/post.html (or domain/XXX/post.html)
in web.xml, avoid mapping a org.springframework.web.servlet.DispatcherServlet to /*. Things will be easier as the app grows if you map it down a level to /app/* or something. Apache or IIS on the frontend can rewrite pretty public URLs into whatever.
But then the user should enter domain/app/post.html instead of domain/post.html.
If you've exposed a JavaEE App server directly to the internet, yes, but you don't want to do that anyway, do you? :)
0

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

Comments

0

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)
    {
    }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.