文档详情

springmvc_数据的处理.docx

发布:2017-01-29约1.37千字共5页下载文档
文本预览下载声明
1、提交数据的处理(1)提交的域名称和处理方法的参数一致即可提交的数据: 处理方法:@RequestMapping(/hello)public String hello(String name){System.out.println(name...+name);return index.jsp;}结果:(2)提交的域名称和参数名不一致提交的数据:处理方法:@RequestMapping(/hello)public String hello(@RequestParam(uname)String name){System.out.println(name...+name);return index.jsp;}结果:(3)提交的是一个对象,要求提交的表单域名和对象的属性名一致,参数使用对象作为参数即可提交的数据:处理方法:@RequestMapping(/user)public String user(User user){System.out.println(user);return index.jsp;}}结果:实体类:public class User {private int id;private String name;private String pwd; get,set… @Overridepublic String toString() {return User [id= + id + , name= + name + , pwd= + pwd + ];}}2、将数据显示到UI层(1)通过ModelAndView---需要通过视图解析器@RequestMapping(/hello)public ModelAndView hello(HttpServletRequest request,HttpServletResponse response){//相当于request.setAtttibute(msg,SpringMVC annotation);ModelAndView mv = new ModelAndView();mv.addObject(msg, SpringMVC annotation);mv.setViewName(hello);return mv;}(2)通过ModelMap---不需要通过视图解析器ModelMap需要作为处理方法的参数提交的数据:处理方法:@RequestMapping(/hello1)public String hello1(String name,ModelMap model){System.out.println(name...+name);//相当于request.setAtttibute(name,name);model.addObject(name, name);return index.jsp;}结果:ModelAndView和ModelMap的区别:相同点:都可以将数据封装显示到表示层页面中不同点:1、ModelAndView可以指定跳转的视图,而ModelMap不能 2、ModelAndView需要视图解析器,ModelMap不需要配置
显示全部
相似文档