Day25 SpringMVC 批量更新操作

it2023-05-15  70

参数绑定-集合传递

(1)什么时候前台会给后台传集合? 批量修改,将修改的多条记录放到集合中提交给后台。

demo03_update_persons.jsp

<%-- 表单 只会将多条记录放到一个list里面,提交到后台 --%> <form method="post" action="${pageContext.request.contextPath}/update3.action"> <table> <tr> <td><input type="hidden" value="1" name="list[0].id" /></td> <td><input type="text" value="jack" name="list[0].username" /></td> <td><input type="text" value="1234" name="list[0].password" /></td> </tr> <tr> <td><input type="hidden" value="2" name="list[1].id" /></td> <td><input type="text" value="rose" name="list[1].username" /></td> <td><input type="text" value="1234" name="list[1].password" /></td> </tr> </table> <input type="submit" value="提交数据到后台" > <br/> </form>

MyQueryOv2

public class MyQueryOv2 { private List<Person> list; public List<Person> getList() { return list; } public void setList(List<Person> list) { this.list = list; } }

Person

public class Person { private Integer id; private String username; private String password; }

Demo01Controller

@RequestMapping("update3.action") public ModelAndView test03(MyQueryOv2 ov){//参数写QueryOv2类,但是页面要指定变量名与索引 System.out.println(ov.getList()); //[Person{id=1, username='jack', password='1234'}, Person{id=2, username='rose', password='1234'}] return null; } 如果前台传过来数组 ,参数可以写数组也可以写QueryOv类,但是如果是List,参数位置只能写QueryOV类

批量修改页面的回显

》批量修改 使用input标签显示数值 重点在 参数名 与 varStatus

varStatus 表示状态 对象 包含两个属性 》》count 是集合的元素个数 》》index 是索引 Demo01Controller

@RequestMapping("updateUI.action") public ModelAndView test04(){ Person p1 = new Person(1,"jack","12345"); Person p2 = new Person(2,"rose","12345"); List<Person> list = new ArrayList<Person>(); list.add(p1); list.add(p2); ModelAndView mv = new ModelAndView(); mv.addObject("list",list); mv.setViewName("demo04_update_person"); return mv; }

demo04_update_persons.jsp

<%-- 表单 只会将多条记录放到一个list里面,提交到后台 --%> <form method="post" action="${pageContext.request.contextPath}/update.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>
最新回复(0)