1. SpringMVC方法的返回值类型
3.1String类作为返回值
3.1.1Controller层
/** * 返回值类型为String时,一般用于返回视图名称 * 1、当方法返回值为null时,默认将请求路径当做视图 forward:/jsp/index.jsp 如果说没有视图解析器,返回值为null携带数据只能用json * 2、当方法返回一个String的字符串时,当字符串为逻辑视图名时只返回视图,如果携带数据则使用request,session或者json * 3、当方法返回值加入forward时代表转发,如果写为redirect:xxxx代表重定向,不是返回视图了,但是不会这样做!! */ @RequestMapping("/condRequest") public String secondRequest(HttpServletRequest request, HttpServletResponse response){ request.setAttribute("user","张三"); return "forward:/jsp/index.jsp"; }
3.1.2页面
3.2void作为返回值
3.2.1Controller层
/** * 如果方法写成了void就跟原来servlet含义是差不多 的 * json */ @RequestMapping(value = "/firstRequest",produces = "text/json;charset=utf-8") public void firstRequest(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws IOException { Invoicing info=new Invoicing(); info.setUserName("张三"); info.setPassword("123"); /** * json格式传递 */ response.setCharacterEncoding("UTF-8"); String value= JSON.toJSONString(info); response.getWriter().write(value); }
3.2.2页面
3.3ModelAndView作为返回值
3.3.1Controller层
/** * ModelAndView * @param request 请求对象 * @param response 响应对象 * @return ModelAndView model是用来传递数据用的,view是所需要跳转的页面 */ @RequestMapping("/model") public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response){ ModelAndView modelAndView=new ModelAndView(); //携带给页面数据 modelAndView.addObject("user","鸭头"); //指定跳转页面(视图解析器配置前后缀) modelAndView.setViewName("index"); return modelAndView; }
3.3.2页面
3.4Object作为返回值类型
3.4.1Controller层
/** *Object * 1.当方法返回值为null时,默认将请求路径当作视图 jsp/objectRequest.jsp 如果没有视图解析器,如果返回值为null携带数据只能用json * 2.当方法返回值为String类型字符串时,就是视图的逻辑名称 * 3.当返回对象或者集合数据时要使用json格式字符串,可选fashjson手动转换 ,也可以使用jackson自动转换 */ @RequestMapping("/objectRequest") @ResponseBody public Object objectRequest(){ List<Invoicing> invoicingList=new ArrayList<>(); Invoicing invoicing=new Invoicing(); invoicing.setUid(1); invoicing.setUserName("aaa"); invoicing.setPassword("123"); invoicing.setRealName("张三"); invoicingList.add(invoicing); return invoicingList; }
3.4.2页面
2. Spring MVC的自动装配
4.1请求参数的自动类型转换
JSP页面创建表单
<form method="post" action="/formRequest"> 姓名:<input id="userName" name="userName" class="userName"/><span></span><br/> 密码:<input id="password"name="password" class="password"/><br/> <input type="submit" class="submit"/> </form>
/** * 接收零散参数:装配原则为传递参数名和方法接收参数名 * 手动装配@RequestParam name代表页面发送的参数名字 ,required代表参数是否必须传递 false * 代表不传递,默认为true defaultValue代表默认 model代表给页面传递的数据 */ @RequestMapping(value = "/formRequest") public String formRequest(String userName,String password){ System.out.println(userName+"\t"+password); return "welcome"; }
注意点*:控制器Controller中的方法参数名称必须和表单元素的name属性值保持一致
4.2使用@RequestParam注解
@RequestParam的作用是,当表单元素与控制器方法的参数不匹配的情况下,使用@RequestParam注解声明参数名称。
@RequestParam 有三个属性:
(1)value:请求参数名(必须配置)
(2)required:是否必需,默认为 true,即 请求中必须包含该参数,如果
没有包含,将会抛出异常(可选配置)
(3)defaultValue:默认值,如果设置了该值,required 将自动设为 false,
无论你是否配置了required,配置了什么值,都是 false(可选配置)
/** * 接收零散参数:装配原则为传递参数名和方法接收参数名 * 手动装配@RequestParam name代表页面发送的参数名字 ,required代表参数是否必须传递 false * 代表不传递,默认为true defaultValue代表默认 model代表给页面传递的数据 */ @RequestMapping(value = "/formRequest") public String formRequest(@RequestParam(name="userName",required = false,defaultValue = "张三")String username,@RequestParam(name = "password",required = false,defaultValue = "123")String password){ System.out.println(username+"\t"+password); return "welcome"; }
4.3请求参数自动装配为对象
/* * 对象参数:传递的对象参数和对象中的属性名保持一致 * */ @RequestMapping("/invoivingObject") public String invoivingObject(Invoicing invoicing){ System.out.println(invoicing.getUserName()); return "welcome"; }
4.4restful风格参数传递
在使用 RESTful 风格之前,我们如果想要增加一条商品数据通常是这样的:
但是使用了 RESTful 风格之后就会变成:
/** * get请求时,如果需要传递参数,那么则把不能使用以往的方式?name=xxx&age=yy,但是现在要遵循restful风格,例:xxx/ttt/ddd * 根据地址栏url匹配拿值 使用@PathVariable(name=地址栏中的参数映射) * */ @RequestMapping("/restfulRequest/{b}/{d}") @ResponseBody public String restfulRequest(@PathVariable(name = "b") String username, @PathVariable(name = "d")String password){ System.out.println(username+"\t"+password); return "welcome"; }
4.5域属性对象传递参数
<form method="post" action="/invoivingObject"> 姓名:<input id="userName" name="userName" class="userName"/><span></span><br/> 密码:<input id="password"name="password" class="password"/><br/> teacherName1<input id="t_name" class="t_name" name="teacher.t_name"/> <input type="submit" class="submit"/> </form>
public class Invoicing { private Integer uid; private String userName; private String password; private String realName; private Teacher teacher; private List<Teacher> teachersList; public List<Teacher> getTeachersList() { return teachersList; } public void setTeachersList(List<Teacher> teachersList) { this.teachersList = teachersList; } public Teacher getTeacher() { return teacher; } public void setTeacher(Teacher teacher) { this.teacher = teacher; } public Integer getUid() { return uid; } public void setUid(Integer uid) { this.uid = uid; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getRealName() { return realName; } public void setRealName(String realName) { this.realName = realName; } @Override public String toString() { return "Invoicing{" + "uid=" + uid + ", userName='" + userName + '\'' + ", password='" + password + '\'' + ", realName='" + realName + '\'' + ", teacher=" + teacher + ", teachersList=" + teachersList + '}'; } }
/* * 域属性传递:传递参数为:域属性名.属性名 * */ @RequestMapping("/invoivingObject") public String invoivingObject(Invoicing invoicing){ System.out.println(invoicing.getUserName()); return "welcome"; }
4.6域属性集合传递参数
<form method="post" action="/invoivingObject"> 姓名:<input id="userName" name="userName" class="userName"/><span></span><br/> 密码:<input id="password"name="password" class="password"/><br/> teacherName2<input id="t_name" class="t_name" name="teachersList[0].t_name"/> teacherName3<input id="t_name1" name="teachersList[1].t_name"/> <input type="submit" class="submit"/> </form>
public class Invoicing { private Integer uid; private String userName; private String password; private String realName; private Teacher teacher; private List<Teacher> teachersList; public List<Teacher> getTeachersList() { return teachersList; } public void setTeachersList(List<Teacher> teachersList) { this.teachersList = teachersList; } public Teacher getTeacher() { return teacher; } public void setTeacher(Teacher teacher) { this.teacher = teacher; } public Integer getUid() { return uid; } public void setUid(Integer uid) { this.uid = uid; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getRealName() { return realName; } public void setRealName(String realName) { this.realName = realName; } @Override public String toString() { return "Invoicing{" + "uid=" + uid + ", userName='" + userName + '\'' + ", password='" + password + '\'' + ", realName='" + realName + '\'' + ", teacher=" + teacher + ", teachersList=" + teachersList + '}'; } }
/* * 集合参数传递:集合名[下标].属性名 * */ @RequestMapping("/teacherInvoicing") @ResponseBody public String teacherInvoicing(Invoicing invoicing){ System.out.println(invoicing.getTeachersList()); return "welcome"; }