zoukankan      html  css  js  c++  java
  • SpringMVC学习 -- ModelAndView , Model , ModelMap , Map 及 @SessionAttributes 的使用

    输出模型数据:

    • ModelAndView:处理方法返回值类型为 ModelAndView 时 , 其中包含视图和模型信息。方法体即可通过该对象添加模型数据 , 即 SpringMVC 会把 ModelAndView 中的 model 模型放入到 request 域对象中。
    • Map, Model, ModelMap:目标方法的入参中包含 Map, Model, ModelMap 类型的数据, 返回值是 String 类型,Spring MVC 会自动把 Map, Model, ModelMap转化成模型信息放入到 request 域对象中,把返回的 String 类型数据转化成视图信息。
    • @SessionAttributes:只能定义在 Class, interface , enum 上。@SessionAttributes 除了可以通过属性名指定需要放到会话中的属性外,还可以通过模型属性的对象类型指定哪些模型属性需要放到会话中。Spring MVC 将在模型中对应的属性暂存到 HttpSession 中。
      1. value:指定的属性名。可以是 String[] 类型的数据。
      2. type:模型属性对应的类型。可以是 Class<?>[] 类型的数据。
    • @ModelAttribute:方法入参标注该注解后 , 入参的对象就会放到数据模型中(暂时不太懂)。
      1 package com.itdoc.springmvc.entities;
      2 
      3 import org.springframework.stereotype.Controller;
      4 import org.springframework.ui.Model;
      5 import org.springframework.ui.ModelMap;
      6 import org.springframework.web.bind.annotation.*;
      7 import org.springframework.web.servlet.ModelAndView;
      8 
      9 import java.util.Arrays;
     10 import java.util.Date;
     11 import java.util.List;
     12 import java.util.Map;
     13 
     14 /**
     15  * @BLOG http://www.cnblogs.com/goodcheap
     16  * @DESCRIBE
     17  * @AUTHOR WángChéngDá
     18  * @DATE 2017-03-09 13:55
     19  */
     20 @Controller
     21 /**
     22  * @SessionAttributes 只能定义在 Class, interface , enum 上。
     23  * @SessionAttributes 除了可以通过属性名指定需要放到会话中的属性外,
     24  * 还可以通过模型属性的对象类型指定哪些模型属性需要放到会话中。
     25  * Spring MVC 将在模型中对应的属性暂存到 HttpSession 中。
     26  * •value:指定的属性名。可以是 String[] 类型的数据。
     27  * •type:模型属性对应的类型。可以是 Class<?>[] 类型的数据。
     28  */
     29 //@SessionAttributes(value = {"time", "names"}, types = {String.class, User.class})
     30 public class TestModelAndView {
     31 
     32     public static final String SUCCESS = "success";
     33 
     34     @ModelAttribute
     35     public void getUser(@RequestParam(value = "id", required = false) Integer id,
     36                         Map map) {
     37         System.out.println("I am TestModelAndView's getUser method...");
     38         if (id != null) {
     39             //模拟从数据库获取对象
     40             User user = new User(1, "Tom", 12, "123456", "a@aaa.com");
     41             System.out.println("从数据库中获取一个对象: " + user);
     42             map.put("user", user);
     43         }
     44     }
     45 
     46 
     47     @RequestMapping(value = "/updateUser", method = RequestMethod.POST)
     48     public String updateUser(User user) {
     49         System.out.println("修改: " + user);
     50         return SUCCESS;
     51     }
     52 
     53     @RequestMapping("/getModel")
     54     public String getModel(Model model) {
     55         model.addAttribute("names", Arrays.asList("Tom", "Jack", "Lucy", "Mark"));
     56         model.addAttribute("time", new Date());
     57         model.addAttribute("school", "东北财经大学");
     58         model.addAttribute("user", new User("Tom", 13));
     59         return SUCCESS;
     60     }
     61 
     62     @RequestMapping("/getModelMap")
     63     public String getModelMap(ModelMap modelMap) {
     64         List<String> strings = Arrays.asList("Tom", "Jack", "Lucy", "Mark");
     65         modelMap.put("names", Arrays.asList("Tom", "Jack", "Lucy", "Mark"));
     66         modelMap.put("time", new Date());
     67         modelMap.put("school", "东北财经大学");
     68         modelMap.put("user", new User("Tom", 13));
     69         return SUCCESS;
     70     }
     71 
     72     /**
     73      * 目标方法的入参中包含 Map, Model, ModelMap 类型的数据, 返回值是 String 类型,
     74      * Spring MVC 会自动把 Map, Model, ModelMap 转化成模型信息放入到 request 域对象中,
     75      * 把返回的 String 类型数据转化成视图信息。
     76      *
     77      * @param map
     78      * @return
     79      */
     80     @RequestMapping("/getMap")
     81     public String getMap(Map<String, Object> map, String name) {
     82         map.put("names", Arrays.asList("Tom", "Jack", "Lucy", "Mark"));
     83         map.put("time", new Date());
     84         map.put("school", "东北财经大学");
     85         map.put("user", new User("Tom", 13));
     86         return SUCCESS;
     87     }
     88 
     89     /**
     90      * 目标的返回值是 ModelAndView, 其中包含视图和模型信息。
     91      * SpringMVC 会把 ModelAndView 中的 model 模型放入到 request 域对象中。
     92      *
     93      * @return
     94      */
     95     @RequestMapping("/testModelAndView")
     96     public ModelAndView testModelAndView() {
     97         String viewName = SUCCESS;
     98         ModelAndView mv = new ModelAndView(viewName);
     99         mv.addObject("names", Arrays.asList("Tom", "Jack", "Lucy", "Mark"));
    100         mv.addObject("time", new Date());
    101         mv.addObject("school", "东北财经大学");
    102         mv.addObject("user", new User("Tom", 13));
    103         System.out.println(mv);
    104         return mv;
    105     }
    106 }
  • 相关阅读:
    从原理上理解NodeJS的适用场景
    core 基本操作
    SQL Server 触发器
    Centos 7 Apache .netcore 做转发
    Windows Server 使用 WAMP 并配置 Https
    centos7 apache php git pull
    Visual StudioTools for Unity 使用技巧2
    如何实现Windows Phone代码与Unity相互通信(直接调用)
    关于NGUI与原生2D混用相互遮盖的问题心得
    关于NGUI制作图集在低内存设备上的注意事项
  • 原文地址:https://www.cnblogs.com/chinda/p/6530637.html
Copyright © 2011-2022 走看看