How does springMVC pass data from controller to jsp pages

  • 2020-08-22 22:06:08
  • OfStack

1 > When the method returns, the data is stored in the ModelAndView object, as follows:


newModelAndView("/WEBINF/jsp/showData.jsp","message",message)

The first parameter is url, the second parameter is key for the data to be passed, and the third parameter is the data object.

Note here that the data is stored in request by default.

Example:


@RequestMapping(value="/mad/showData_1.do")
public ModelAndView showData_1(){
  String message = " This is the data to be passed ";
  /* Among them the first 1 The parameters for url, The first 2 Of the data to be passed key, The first 3 The parameters are data objects. The thing to notice here is that : Data is stored by default request In the. */
  return new ModelAndView("/WEB-INF/jsp/showData.jsp","message",message);
}

Foreground page access method:


request:${requestScope.message}

2 > You can add annotations in front of classes


@SessionAttributes({ " message " , " user " })

This annotation can set the corresponding model parameter and will also store 1 copy in session. The parameter in the annotation is 1 set and can be written as many as, as in the example above, where message and user are both key of stored data.

Sample program:


@SessionAttributes({"message","user"})   //modelAndView The corresponding data in session Stored in the 1 Copy of the

Page access:


session:${sessionScope.message}<br/>

3 > Data modelAndView returns 1 collection

This is the same as processing 1 above, because modelAndView accepts data types of Object, and the collection is processed in the same way

Example:


// use modelAndView Object to pass data to the foreground. 
// Passing multiple parameters (of different types) 
  @RequestMapping(value="/mad/showData_2.do")
  public ModelAndView showData_2(){
   System.out.println("showData_2");
   String message = " This is the data to be passed ";
   User user = new User(" zhang 3", 12, new Date());
   List<User> us= new ArrayList<User>();
   us.add(new User(" zhang 3", 12, new Date()));
   us.add(new User(" zhang 4", 13, new Date()));
   us.add(new User(" zhang 5", 14, new Date()));
   ModelAndView mad = new ModelAndView("/WEB-INF/jsp/showData.jsp");
   // Store data modelMap
   mad.addObject("message", message);
   mad.addObject(user);// The default is lowercase for the first letter of the class name 
   mad.addObject("users", us);
   return mad;
 }

Foreground page access method:


request:${requestScope.message}<br/>
<c:forEach items="${requestScope.users }"var="u">
   ${u.name }-${u.age }-${u.birth }<br/>
  </c:forEach>

4. Use modelAndView to pass multiple parameters.

mad. addObject(" message ",message); Method to set parameters.

In this method, the first parameter is key for the data, and the second parameter is the data object.

You can also use ES79en. addObject(user); methods

The argument to the data in this method is the data object, and the key of the data is the class name of the object's class (where the first letter is lowercase).

5. Use ModelMap to transfer multiple data to jsp.

Add the parameter ModelMap map to the method's argument list, and spring automatically creates the ModelMap object.

Then call put of map (key,value) or addAttribute(key,value) to put the data into map and spring will automatically store the data into request.

Example:


// use modelMap Object to pass data to the foreground. 
  // Passing multiple parameters (of different types) 
  @RequestMapping(value="/mad/showData_3.do")
  public String showData_3(ModelMap map){
   System.out.println("showData_3");
   String message = " This is the data to be passed ";
   User user = new User(" zhang 3", 12, new Date());
   List<User> us= new ArrayList<User>();
   us.add(new User(" zhang 3", 12, new Date()));
   us.add(new User(" zhang 4", 13, new Date()));
   us.add(new User(" zhang 5", 14, new Date()));
   // Store data modelMap
   map.put("message", message);
   map.addAttribute("user", user);
   map.put("users", us);
   return"/WEB-INF/jsp/showData.jsp";
  }

Page call:


request:${requestScope.message}<br/>
  session:${sessionScope.message }<br/>
  application:${applicationScope.message}<br/>
  <hr/>
  <h1>ModelMap The data in the </h1>
  ${requestScope.message}<br/>
  ${requestScope.user.name}<br/>
  <p> The list of </p>
  <c:forEach items="${requestScope.users}" var="u">
   ${u.name }-${u.age }-${u.birth }<br/>
  </c:forEach>

Related articles: