1.提交数据的处理
a)提交的域名称喝处理方法的参数名一致即可
http://localhost:8080/springMVC-0/helloController/hello3.do?name=hhh
处理方式
@RequestMapping("/hello3.do")
public String hello(String name) throws IOException {
System.out.println(name);
return "/index.jsp";
}
b)如果域名名称和参数名不一致提交的数据
http://localhost:8080/springMVC-0/helloController/hello3.do?username=hhh
http://localhost:8080/springMVC-0/helloController/hello3.do?username=hhh
处理方法
@RequestMapping("/hello3.do")
public String hello(@RequestParam("username") String name) throws IOException {
System.out.println(name);
return "/index.jsp";
}
c)提交的事一个对象要求提交的表单域名和对象的属性名一致,处理方法的参数使用对象即可
http://localhost:8080/springMVC-0/helloController/user.do?name=zhangsan&&pwd=123
处理方法
@RequestMapping("/user.do")
public String user(User user)
{
System.out.println(user);
return "/index.jsp";
}
2.将数据显示到View层
a)通过ModelAndView,需要视图解析器
public ModelAndView show(HttpServletRequest request, HttpServletResponse response)
{
ModelAndView mv = new ModelAndView();
mv.addObject("msg","hello hhh");
mv.setViewName("hello");
return mv;
}
b)通过ModelMap来实现,不需要视图解析器。 ModelMap 需要作为处理方法的参数
@RequestMapping("/hello3.do")
public String hello(String name, ModelMap model) throws IOException {
model.addAttribute("name",name);
System.out.println(name);
return "/index.jsp";
}
ModelAndView 与 ModelMap的区别:相同点都可以将数据封装显示到表示层页面中
不同点ModelAndView可以指定跳转的视图,而ModelMap不能
ModelAndView需要视图解析器,ModelMap不需要配置