SpringMVC的参数绑定-基本类型绑定
用户请求服务器的时候会给后台传递参数, 如何来快速的接收到用户传递的参数?可以使用参数绑定来解决这个问题
传统方法来进行传递参数
在Controller中的方法的形式参数上直接声明HttpServletRequest, HttpServletResponse , HttpSession
@RequestMapping("login01.action")
public ModelAndView login(HttpServletRequest req){
//获取集合
List<Person> list=Personutil.findAll();
String name= req.getParameter("name");
String password= req.getParameter("password");
System.out.println(name);
System.out.println(password);
ModelAndView modelAndView=new ModelAndView();
modelAndView.setViewName("main");
return modelAndView;
}
基本类型参数绑定来传递参数
传入的参数的名字和本方法的形参名字一致时
这个时候@RequestParam注解才会生效,并且只能是基本类型和字符串
浏览器传: name=jack&password=1234
方法形参: test03(Strnig name,String password)
@RequestMapping("login02.action")
public ModelAndView login02(String name,String password){
//获取集合
List<Person> list=Personutil.findAll();
System.out.println(name);
System.out.println(password);
ModelAndView modelAndView=new ModelAndView();
modelAndView.setViewName("main");
return modelAndView;
}
传入的参数的名字和本方法的形参名字不一致时(不常用)
浏览器传: name=jack&password=1234
方法形参: test03(Strnig name01,String password)
这时不能进行参数绑定,必须使用@RequestParam注解
@RequestMapping("login02.action")
public ModelAndView login02(@RequestParam(value="name",required = true,defaultValue = "123") String name,String password){
//获取集合
List<Person> list=Personutil.findAll();
System.out.println(name);
System.out.println(password);
ModelAndView modelAndView=new ModelAndView();
modelAndView.setViewName("main");
return modelAndView;
}
SpringMVC的参数绑定-POJO绑定
pojo就是指普通的JavaBean对象
当页面需要传递多个参数时(比如注册),我们可以将多个参数封装到一个JavaBean类,将这个JavaBean作为方法形参,SpringMVC可以直接将页面的数据赋值给JavaBean
JavaBean类中成员变量的名字和必须和表单中name属性的值一样
@RequestMapping("login03.action")
public ModelAndView test04(MyPojo mypojo){
//springMVC可以将表单的数据赋值给 一个javaBean对象
System.out.println(mypojo.getUsername());
System.out.println(mypojo.getPassword());
ModelAndView modelAndView= new ModelAndView();
mv.setViewName("main");
return modelAndView;
}
SpringMVC的参数绑定-queryvo绑定
queryVo是一个包装类,可以封装多个不同类型的成员变量
当前的javaBean类的成员变量有复杂类型 如:Person类中有birthday对象变量
如:
public class Person
private String name;
private Birthday b;
}
注册实例
add
@RequestMapping("add.action")
public ModelAndView add(Person person){
//springMVC可以将表单的数据赋值给 一个javaBean对象
System.out.println(person.getUsername());
System.out.println(person.getPassword());
System.out.println(person.getBirthday().getYear());
System.out.println(person.getBirthday().getMonth());
System.out.println(person.getBirthday().getDay());
ModelAndView mv= new ModelAndView();
mv.setViewName("main");
return mv;
}
addPersonUI.jsp
<form method="post" action="${pageContext.request.contextPath}/add.action">
用户名: <input type="text" name="username" /><br/>
用户密码: <input type="text" name="password"/><br/>
城市: <input type="text" name="city"/><br/>
年 <input type="text" name="birthday.year"/><br/>
月<input type="text" name="birthday.month"/><br/>
日<input type="text" name="birthday.day"/><br/>
<input type="submit" value="添加"/><br/>
</form>
Person
public class Person {
private int id;
private String username;
private String password;
private String city;
private Birthday birthday;
}
Birthday
public class Birthday {
private int year;
private int month;
private int day;
}
修改数据时,数据回显
为了修改数据方便
可以将被修改的数据,放到ModelAndView中,传给一个新的页面,页面使用el表达式,逐个设置给表单,把修改信息显示出来
<td><a href="${pageContext.request.contextPath}/update.action?id=${item.id}">修改</a> </td>
updatePersonUI.jsp
<form method="post" action="${pageContext.request.contextPath}/update2.action">
用户名: <input type="text" name="username" value="${person.username}"/><br/>
用户密码: <input type="text" name="password" value="${person.password}"/><br/>
城市: <input type="text" name="city" value="${person.city}"/><br/>
年 <input type="text" name="birthday.year" value="${person.birthday.year}"/><br/>
月<input type="text" name="birthday.month" value="${person.birthday.month}"/><br/>
日<input type="text" name="birthday.day" value="${person.birthday.day}"/><br/>
出生日期<input type="text" name="birthday2"value="${person.birthday2}" /><br/>
<input type="submit" value="更新"/><br/>
</form>
数组绑定
复选框选择
当前台有多个复选框表单,并把复选框选中的内容,发给给服务器后台,服务器去接收,返回的则是数组,后台需要使用数组来接收
jsp
<%@ page contentType="text/html;charset=UTF-8" isELIgnored="false" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%-- 页面会将选中的复选框的值,需要让复选框使用同一个name
程序认为需要将多个值 放到数组中,提交到后台
--%>
<form method="post" action="${pageContext.request.contextPath}/delete1.action">
<input type="checkbox" value="1" name="ids"/> 第 1条记录<br/>
<input type="checkbox" value="2" name="ids"/> 第 2条记录<br/>
<input type="checkbox" value="3" name="ids"/> 第 3条记录<br/>
<input type="submit" value="提交数据到后台" > <br/>
</form>
</body>
</html>
demo01Controller(后台代码)
package com.zx.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import java.util.Arrays;
@Controller
public class demo01Controller {
@RequestMapping("delete1.action")
public ModelAndView test01(Integer[] ids){
//参数只需要写数组
System.out.println(Arrays.toString(ids));
return null;
}
}
QueryOV对象绑定
在上面数组参数的前提下,把数组参数放到一个类中,其他的不变
MyQueryOv
public class MyQueryOv {
private Integer[] ids;
public Integer[] getIds() {
return ids;
}
public void setIds(Integer[] ids) {
this.ids = ids;
}
}
test02
@RequestMapping("delete2.action")
public ModelAndView test02(MyQueryOv ov){
//参数只需要写QueryOV类
System.out.println(Arrays.toString(ov.getIds()));
return null;
}
集合参数绑定
页面批量修改数据
批量修改,将修改的多条记录放到集合中提交给后台.多条记录提交,表单只会将多条记录放到一个list里面,提交到后台
update.jsp
<form method="post" action="${pageContext.request.contextPath}/update.action">
<table>
<tr>
<td><input type="hidden" value="1" name="list[0].id" /></td>
<td><input type="text" value="jack" name="list[0].name" /></td>
<td><input type="text" value="1234" name="list[0].password" /></td>
<td><input type="text" value="北京" name="list[0].city" /></td>
</tr>
<tr>
<td><input type="hidden" value="2" name="list[1].id" /></td>
<td><input type="text" value="rose" name="list[1].name" /></td>
<td><input type="text" value="1234" name="list[1].password" /></td>
<td><input type="text" value="长沙" name="list[1].city" /></td>
</tr>
</table>
<input type="submit" value="提交数据到后台" > <br/>
</form>
Person
public class Person {
private int id;
private String name;
private String password;
private String city;
@Override
public String toString() {
return "Person{" +
"id=" + id +
", name='" + name + '\'' +
", password='" + password + '\'' +
", city='" + city + '\'' +
'}';
}
}
Personutil
public class Personutil {
private List<Person> list;
public List<Person> getList() {
return list;
}
public void setList(List<Person> list) {
this.list = list;
}
}
test02
@RequestMapping("update.action")
public ModelAndView test02(Personutil list){
System.out.println(list.getList());
return null;
}
批量修改页面的回显
demo02.jsp
varStatus 表示状态 对象 包含的意思,用于确定list集合的索引,元素个数之类的
count 是集合的元素个数 index 是索引
<%--
表单 只会将多条记录放到一个list里面,提交到后台
--%>
<form method="post" action="${pageContext.request.contextPath}/update3.action">
<table>
<c:forEach items="${list}" var="person" varStatus="vs">
<tr>
<td><input type="hidden" value="${person.id}" name="list[${vs.index}].id" /></td>
<td><input type="text" value="${person.username}" name="list[${vs.index}].username" /></td>
<td><input type="text" value="${person.password}" name="list[${vs.index}].password" /></td>
</tr>
</c:forEach>
</table>
<input type="submit" value="提交数据到后台" > <br/>
</form>
</body>
</html>
test03
@RequestMapping 注解
path或者value表示地址
method 请求方式 get/post
@RequestMapping(path = "updateUI.action",method = {
RequestMethod.POST,RequestMethod.GET})//回显页面
public ModelAndView test04(){
//
Person p1 = new Person(1,"jack","1234");
Person p2 = new Person(2,"rose","1234");
List<Person> list = new ArrayList<Person>();
list.add(p1);
list.add(p2);
//使用ModelAndView实现请求转发
ModelAndView mv = new ModelAndView();
mv.addObject("list",list);
mv.setViewName("demo04_update_persons");
return mv;
}
总结
在使用参数绑定之前从页面拿到值,使用request的getparameter方法取得参数,而springmvc会自动将请求对象参数自动绑定,也就是自动的从request中将数据赋值给参数列表,省去了request的方法
前提 参数列表和请求request域中的名称相同
@RequestMapping注解其中path或者value表示地址,method 表示请求方式 get/post
基本类型参数绑定
pojo参数绑定 也就对象参数绑定,使用对象的get方法获得参数
queryvo绑定 对象的类型中有复杂类型是使用
如果参数名称不同可以通过注解@RequestParam补救,如果没有注解存在,就按参数列表来获取值,如果有注解存在,先按注解赋值(有注解优先看注解)