controller进行数据保存。

1.1 数据保存到request作用域的方式.

  1. 使用ModelAndView,那么该方法的返回类型必须是ModelAndView
  2. 使用Model, 方法的返回值还是字符串类型。
  3. 使用Map.方法的返回值还是字符串类型。
  4. 原始的HttpServletRequest对象保存

1.2 数据保存到session作用域的方式.

  1. 使用原始的HttpSession保存。
  2. 使用注解@SessionAttributes(name={key1,key2})
 1 package com.zhiyou100.wc.controllter;
 2 
 3 import java.text.SimpleDateFormat;
 4 import java.util.Date;
 5 import java.util.Map;
 6 
 7 import javax.servlet.ServletContext;
 8 import javax.servlet.http.HttpSession;
 9 
10 import org.springframework.beans.propertyeditors.CustomDateEditor;
11 import org.springframework.stereotype.Controller;
12 import org.springframework.ui.Model;
13 import org.springframework.web.bind.ServletRequestDataBinder;
14 import org.springframework.web.bind.annotation.InitBinder;
15 import org.springframework.web.bind.annotation.RequestMapping;
16 import org.springframework.web.bind.annotation.SessionAttributes;
17 import org.springframework.web.servlet.ModelAndView;
18 
19 import com.zhiyou100.wc.bean.Users;
20 
21 @Controller
22 @RequestMapping("user")
23 @SessionAttributes(names= {"name","address"})//键名叫:name保存的作用域为session
24 public class UsersControllter {
25     
26 //    @RequestMapping("index.do")
27 //    public String index() {
28 //        
29 //        return "index";
30 //    }
31     
32     @RequestMapping("index.do")
33     public ModelAndView index() {
34         //1.可以保存到ModelAndView,那么方法的返回类型必须是ModelAndView
35         ModelAndView  mv=new ModelAndView("index");
36         mv.addObject("name","张三");
37         return mv;
38         
39     }
40     
41     @RequestMapping("index.do")
42     public String index(Model model) {
43         //2 . 可以保存到Model,那么方法的返回值还可以是字符串
44         model.addAttribute("name","张三");
45         return "index";
46         
47     }
48     
49     @RequestMapping("index.do")
50     public String index(Map<String,Object> map) {
51         //3 . 可以保存到map,
52         //以上三种方式的作用域都是request
53         map.put("name","张三");
54         return "index";
55         
56     }
57     
58     @RequestMapping("index.do")
59     public String index(HttpSession session) {
60         //4.使用原始的HttpSession
61         session.setAttribute("name","张三");
62         
63         return "index";
64     }
65     @RequestMapping("index.do")
66     public String index(Model model,HttpSession session) {
67         //4.使用原始的HttpSession
68         model.addAttribute("name","张三");
69         session.getServletContext().setAttribute("name", "我在application里");
70         return "index";
71     }
72     
73     @RequestMapping("toDate.do")
74     public String toDate(Date date) {
75         System.out.println(date);
76         return "index";
77     }
78     
79     @RequestMapping("register.do")
80     public String register(Users users) {
81         System.out.println(users);
82         return "index";
83     }
84     
85 //    @InitBinder
86 //    public void initBinder(ServletRequestDataBinder binder){
87 //        //只要网页中传来的数据格式为yyyy-MM-dd 就会转化为Date类型
88 //        binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"),
89 //                true));
90 //    }
91 }
View Code
原文地址:https://www.cnblogs.com/banzhuanlaowang/p/11456016.html