Spring MVC Annotation @PathVariable example

            Annotation which indicates that a method parameter should be bound to a URI template variable. Supported for RequestMapping annotated handler methods in Servlet environments.


package com.candidjava.springmvc.controller;

import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

public class CountryController

	public ModelAndView helloMap(@PathVariable Map<String, String> pathVars)

		String country = pathVars.get("countryName");
		String name = pathVars.get("userName");

		ModelAndView model = new ModelAndView("page");
		model.addObject("msg", "Country : " + country + " <=======>  Name :  " + name);

		return model;

	public ModelAndView helloString(@PathVariable("countryName") String cn, @PathVariable("userName") String un)
		ModelAndView model = new ModelAndView("page");
		model.addObject("msg", "Country : " + cn + " <=======>  Name :  " + un);
		return model;







    @PathVariable annotation spring example war

    @PathVariable annotation spring example zip


Related Post


Govind patkar
          to use "@PathVariable Map pathVars" we have to add something in dispatcher-servlet.xml like : , but when I add this tag in my dispatcher-servlet it is giving me error. Could you please give some alternative or the reason behind the error. error is : "Attribute name "base-package" associated with an element type "mvc" must be followed by the ' = ' character."