第一种 : 通过ModelAndView
1 public class ControllerTest1 implements Controller { 2 3 public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception { 4 //返回一个模型视图对象 5 ModelAndView mv = new ModelAndView(); 6 mv.addObject("msg","Tst"); 7 mv.setViewName("test"); 8 return mv; 9 } 10 }
第二种 : 通过Model
1 @RequestMapping(path = "/u1") 2 public String setName(@RequestParam("userName") String name, Model model){ 3 model.addAttribute("hello",name); 4 5 return "Login"; 6 } 7 /** 8 * 接收前端用户传递的参数,判断参数的名字,假设名字直接在方法上,可以直接使用 9 * 假设传递的是一个对象User,匹配User对象中的字段,如果名字一致就能匹配,要是不一样则为null 10 * 11 * @param student 12 * @param model 13 * @return 14 */ 15 @RequestMapping(path = "/u2") 16 public String setName2(Student student, Model model){ 17 model.addAttribute("student",student);//student传到前端的数据 18 //返回映射地址 19 return "Login"; 20 }
第三中:通过ModelMap
1 @RequestMapping(path = "/u3") 2 public String setName3(Student student,ModelMap modelMap){ 3 //"student"传到前端的数据 4 modelMap.addAttribute("student",student); 5 //返回映射地址 6 return "Login"; 7 }