Spring 3 Web MVC – Friendly URL using RequestMapping variable URI

Following on from a previous tutorial on getting a hello world Spring 3 Web MVC application running in Netbeans here, this tutorial will explain how to achieve friendly URL’s incorporating variables.





From the previous example application we have the following jsp called viewdemo.jsp

The Person bean is:

And the controller is:


Now add the following to the controller:




Note the RequestMapping value now has /{} on the end of it. Also note that the RequestMethod is GET for obvious reasons. value=”name” is not strictly necessary as there is only PathVariable, spring gets the picture. Note that when using it however, value =”matchMe” and what’s in /{matchMe} must match.

Now in demo.jsp, we’ll create a link that will invoke this method passing in the ‘name’ parameter:

The ‘Show Default ‘ link will call controller method

returning a person with ‘Jack’ as the name hardcoded into it. The ‘Show Mary’ link will call controller method

Hence the name parameter is passed in and thus revealed as the page is rendered.

For setting this project up in full, please refer to this tutorial

Note that there can be many parameters in the @RequestMapping(value=””) annotation.


The link to this then becomes


Now, a rather unfortunate problem arises here: Click any of the links once and everything is ok, click the link again and it’s clear that an extra /person is in the url http://localhost:8084/HelloSpring/person/person/Mary

This is a common problem and I’ll address it in a future blog but for now, it can be corrected easily by specifying the full context path in each link as so:


Now it’s possible to click eack link as often as one likes.

Hope this tutorial on Spring 3 Web MVC @PathVariable with @RequestMapping variable URI was helpful…..



  • Pingback: Annotated Dependency Injection / Inversion of Control (DI / IoC) in Spring 3 Web MVC using Netbeans | OutBottle Web Development Blogging()

  • Pingback: Using @RequestParam to pass in HTTP GET parameters in Spring 3 Web MVC | OutBottle Web Development Blogging()

  • Pingback: Integrating Hibernate with Spring 3 Web MVC | OutBottle Web Development Blogging()

  • anil

    very nice

  • tuanjinn


  • SRJ

    Now, a rather unfortunate problem arises here: an extra /person is in the url http://localhost:8084/HelloSpring/person/person/Mary

    Do you have a solution for this ? Breaking my head with this issue…

    • NameNotTaken

      Hi SRJ

      Unless you are seeing a different problem, I think the issue is addressed in the blog in the last couple of paragraphs.

      An nicer alternative to using request.getContextPath() is to do something like this:

      <%@ taglib uri="http://java.sun.com/jsp/jstl/core&quot; prefix="c" %>

      <c:set var="cp" value="${pageContext.request.servletContext.contextPath}" scope="session" />

      <a href="{cp}/person/Mary">Show Mary</a>

      Let me know if that solves your problem.


  • S Suresh Kesavan

    I changed url pattern as “/” and implemented the path variable but the CSS is not recognizing. any Help on this?
    Java Jobs

    • NameNotTaken



      You have to specify any paths that that you need to let through the Spring filter. I.e. JS, CSS and img files files within the WEB-INF folder need a mention/rule in the applicationContext.xml so that Spring does not try to (unsuccessfully) map them to a @RequestMapping

      Adding the rules means that those paths do not seek an @Controller @RequestMapping. Access to those url’s is then permitted.

  • S Suresh Kesavan

    Nice example and i have implemented in my following web site. Initially css was not recognizing then added following default mapping then it works find.


    Founder of river2c a Job Portal
    Java jobs in India

  • Muhammad Sadiq

    Awesome Example.
    How to map the following
    @RequestMapping(value =”emp/{name}”, method=GET) and
    @RequestMapping( value=”emp/{id}”, method=GET ).
    I want to access employee by either string name or int id. whether i have to map these urls to two different methods one with @PathVariable Stirng and another with Int OR something else.
    any help will be appreciated.

    • NameNotTaken

      Yea, I can see the problem. HTTP GET parameters (or URL’s) are all strings anyway even if they look like int’s so Spring won’t be able to distinguish. I guess just change it to emp/{{nameOrID}} and manually check if the parameter is an int or not. That may not be ideal though as the name may coincidentally be an int. I think it’s best to use two different url’s. 1. emp-by-id and 2. emp-by-name or something to that effect.

      • Muhammad Sadiq


  • Pingback: Java Spring Friendly Url mapping issues - DexPage()