处理提交数据
提交的域名称和处理方法的参数一致
提交数据:HTTP://localhost:8080/hello/s1?name=cs
处理方法:
@RequestMapping("/hello/s1")
public String test1(String name) {
System.out.println (name);
return "/test";
}
后台输出:cs
提交的域名称和处理方法的参数不一致
提交数据:HTTP://localhost:8080/hello/s1?name=cs
处理方法:
@RequestMapping("/hello/s1")
public String test1(@RequestParam("name") String Names) {
System.out.println (Names);
return "/test";
}
后台输出:cs
提交一个对象
提交数据:HTTP://localhost:8080/hello/s1?name=cs&id=1
处理方法:
@RequestMapping("/hello/s1")
public String test1(User user) {
System.out.println (user);
return "/test";
}
数据显示到前端
ModelAndView
ModelAndView 是 SpringMVC 为我们提供的一个对象,该对象也可以用作控制器方法的返回值。
该对象中有两个方法ModelAndView返回结果时候,既可以返回数据(Model),也可以指定跳转结果页面(view)
@RequestMapping("/s3")
public ModelAndView test4() {
ModelAndView modelAndView = new ModelAndView ();
modelAndView.addObject ("msg", "我是附带的值");
modelAndView.setViewName ("test");
return modelAndView;
}
ModelMap
ModelMap继承LinkedHashMap
@RequestMapping("/hello")
public String handleRequest(ModelMap modelAndView) {
modelAndView.addAttribute ("msg", "我是附带给前端的值");
return "test";
}
Model
Model是一个接口,实现类为ExtendedModeMap,继承ModeMap类
@RequestMapping("/hello")
public String handleRequest(Model modelAndView) {
modelAndView.addAttribute ("msg", "我是附带给前端的值");
return "test";
}
ModelAndView和Model的区别
ModelAndView是方法返回值,实例是用户手动创建.
Model是方法参数,实例是springMVC自动创建并作为控制器方法参数传入,无需用户创建.
Model和ModelMap返回的是页面的指定路径,ModelAndView返回的是对象.