Home  >  Spring MVC

Spring MVC @SessionAttributes and @CookieValue Annotation Example

By Arvind Rai, April 23, 2015
In this page we will learn Spring MVC @SessionAttributes and @CookieValue annotation. The JavaBean object can be added in session by two way in spring MVC. One by using @SessionAttributes and another by session.setAttribute(). The difference between them is that previous one is used to keep object in session for short lived. In spring MVC, we may need to populate our form with the data which was extracted in any previous request in the application. Here we can use @SessionAttributes. It works with @ModelAttribute and is annotated at type level. The objects are added in ModelAttribute and the name of it is assigned to @SessionAttributes value element. @CookieValue is used as argument of request mapping method. The HTTP cookie is bound to the @CookieValue parameter for a given cookie name. Find the example now.

@SessionAttributes

@SessionAttributes is used in conjunction with @ModelAttribute. @SessionAttributes is used at type level for a specific handler. If we have declared a @ModelAttribute as
@ModelAttribute("countrybean")
public Country addAttributes() { }  
Then the @SessionAttributes can be declared as
@Controller
@SessionAttributes(value = "countrybean", types = {Country.class})
public class HelloController { }  
The addAttributes() method will be created within the same controller. The @ModelAttribute name is assigned to @SessionAttributes as value. It has two elements, value and types. value is the name of session attribute in the model and types is the types of session attributes in the model. We can assign an array of @ModelAttribute names. Suppose we have two methods annotated with @ModelAttribute then both will be assigned to @SessionAttributes. Find the sample example.
@Controller
@SessionAttributes(value = {"bean1", "bean2" }, types = {Bean1.class, Bean2.class})
public class HelloController { 
  @ModelAttribute("bean1")
  public Bean1 addAttributes1() {}
  @ModelAttribute("bean2")
  public Bean2 addAttributes2() {}
} 

@CookieValue

In spring @CookieValue annotation binds a method parameter to the HTTP cookie. This is used in the method annotated with @RequestMapping. Suppose we have a HTTP cookie name as JSESSIONID, the we can use @CookieValue as below.
@RequestMapping(value="/updateForm")
public ModelAndView country(@CookieValue("JSESSIONID") String cookie){}  
The HTTP cookie will bind to the argument cookie in the method

Example for @SessionAttributes and @CookieValue

To understand @SessionAttributes and @CookieValue in real life, we will provide the example. @SessionAttributes is useful to support the form. Suppose in one request we have some bean which will be used by form in any other request. So we add the bean to ModelAttribute and then the name of it is assigned to SessionAttributes. In this way, the bean is saved in the session. When the request comes to the controller, first all the methods annotated with @ModelAttribute run. Now find the two controller being used in our example.
HelloController.java
@Controller
@SessionAttributes(value = "countrybean", types = {Country.class})
public class HelloController {
	@Autowired
	private CountryController controller;
	@ModelAttribute("countrybean")
        public Country addAttributes() {
	   Country country = new Country();
	   country.setCountryName("India");
	   country.setCapital("Delhi");
	   return country;
        }
	@RequestMapping(value="countryinfo")
	public String country(){
		return "start";
	}
} 

CountryController.java
@Controller
public class CountryController {
	@RequestMapping(value="/updateForm")
	public ModelAndView country(HttpSession session, @CookieValue("JSESSIONID") String cookie){
	   Country countryObj = (Country)session.getAttribute("countrybean");
	   System.out.println("Country Name:"+ countryObj.getCountryName());
	   System.out.println("Capital:"+ countryObj.getCapital());
	   System.out.println("JSESSIONID:"+ cookie);
	   return new ModelAndView("updateCountry","country", countryObj);
	}
} 
The flow of the request will be as follows.
1. When we hit the URL /countryinfo, the method addAttributes() in HelloController class runs and a ModelAttribute named countrybean is created.
2. When the URL /updateForm is requested, the method country of CountryController class runs.
3. The updateCountry.jsp page will be populated with the values extracted from session.
4. The request mapping method which has the @CookieValue argument, will provide the cookie value for the assigned cookie name.
Find the populated form as an output.
Spring MVC  @SessionAttributes and @CookieValue Annotation Example
The console output will be as shown below.
Country Name:India
Capital:Delhi
JSESSIONID:D301ED03E90EE36F1CE99B5F268109AF 

Download Complete Source Code

POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS


©2019 concretepage.com | Privacy Policy | Contact Us