@SessionAttributes multiple values


@SessionAttributes multiple values

Examples shows you how you can use @SessionAttributes to hold multiple session values.

Examples shows you how you can use @SessionAttributes to hold multiple session values.

@SessionAttributes multiple values

In this section we will see how you can use @sessionattributes to set multiple session values to it.

While developing the application using Spring MVC can use the @sessionattributes attribute to save some data. This session data can be used in the UI form.

In the following example we are using @sessionattributes to put two session values formdata and someCombobox. The correct way to use @sessionattributes for multiple values are:


Note that the variable are in curly braces.

Here is the complete list of the code:

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;

public class TutorialController {

 @RequestMapping(method = RequestMethod.GET)
   public String showForm(   HttpServletRequest request,
                     HttpServletResponse response,
                     ModelMap model) {
	  MyFormDataClass t =	new MyFormDataClass();

      model.put("formdata", t );

	  List list = new ArrayList();
	  //Add some values in list
	  //Add the list to the session
	  model.put("someCombobox", list );
	  return "AddEditForm";

	@RequestMapping(method = RequestMethod.POST)
	public String onSubmit(@ModelAttribute("formdata") MyFormDataClass t) {
		return "redirect:tutorialsuccess.htm";

With the help of @SessionAttributes you can define multiple session variables.