Day25SSM之SpringMVC 批量更新操作

参数绑定-集合传递

  • (1)什么时候前台会给后台传集合?
    批量修改,将修改的多条记录放到集合中提交给后台。
  • (2)前台代码
    》批量修改 使用input标签显示数值
    重点在 参数名 与 varStatus
  • (3)后台代码

页面批量修改数据

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;

test03

    @RequestMapping("update3.action")
    public ModelAndView test03(MyQueryOv2 ov){
    
    //参数写QueryOv2类,但是页面要指定变量名与索引
        System.out.println(ov.getList());
        return null;
    }
  • 如果是数组 ,参数可以写数组也可以写QueryOv类,但是如果是List,参数位置只能写QueryOV类

批量修改页面的回显

test04

@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;
    }

demo04_update_persons.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>Title</title>
</head>
<body>

<%--
 表单 只会将多条记录放到一个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>

varStatus

  • (1) varStatus 表示状态 对象 包含
    》》count 是集合的元素个数
    》》index 是索引
for( int index = 0;index<list.size();index++){
    
    
}

小结

》开发修改功能
》》开发回显页面 XxxxupdateUI.jsp
》》开发处理方法 method(Xxx xxx) method(QueryOV ov)
》》QueryOV 类中定义一个list的成员变量

猜你喜欢

转载自blog.csdn.net/u013621398/article/details/109067216