• Spring MVC,绑定数组、集合


    假定你已经具备开发环境,读过前几篇文章,如:Spring MVC,绑定默认数据

    一、绑定数组

    1.1 创建前台页面,位于:WEB-INFjsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>用户列表</title>
    </head>
    <body>
        <form action="${pageContext.request.contextPath }/deleteUsers"
               method="post">
            <table width="20%" border=1>
                <tr>
                    <td>选择</td>
                    <td>用户名</td>
                </tr>
                <tr>
                    <td><input name="ids" value="1" type="checkbox"></td>
                    <td>tom</td>
                </tr>
                <tr>
                    <td><input name="ids" value="2" type="checkbox"></td>
                    <td>jack</td>
                </tr>
                <tr>
                    <td><input name="ids" value="3" type="checkbox"></td>
                    <td>lucy</td>
                </tr>
            </table>
            <input type="submit" value="删除"/>
        </form>
    </body>
    </html>

    1.2 创建后台控制器

    package com.itheima.controller;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    @Controller
    public class UserController {
        /**
         * 向用户列表页面跳转
         */
        @RequestMapping("/toUser")
        public String selectUsers( ) {
            return "user";
        }
        /**
         * 接收批量删除用户的方法
         */
        @RequestMapping("/deleteUsers")
        public String deleteUsers(Integer[] ids) {
            if(ids !=null){
                for (Integer id : ids) {            
        // 使用输出语句模拟已经删除了用户
        System.out.println("删除了id为"+id+"的用户!");
                }    
            }else{
                System.out.println("ids=null");
            }
            return "success";
        }
    }

    1.3 测试,打开页面:http://localhost:8080/ssm/toUser

     提交,后台显示:

    二、绑定集合

    2.1 创建前台页面,位于:WEB-INFjsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>修改用户</title>
    </head>
    <body>
        <form action="${pageContext.request.contextPath }/editUsers"
               method="post" id='formid'>
            <table width="30%" border=1>
                <tr>
                    <td>选择</td>
                    <td>用户名</td>
                </tr>
                <tr>
                  <td>
                    <input name="users[0].id" value="1" type="checkbox" />
                  </td>
                  <td>
                  <input name="users[0].username" value="tome" type="text" />
                  </td>
                </tr>
                <tr>
                  <td>
                    <input name="users[1].id" value="2" type="checkbox" />
                  </td>
                  <td>
                  <input name="users[1].username" value="jack" type="text" />
                  </td>
                </tr>
            </table>
            <input type="submit" value="修改" />
        </form>
    </body>
    </html>

    2.2 创建后台控制器

    package com.itheima.controller;
    
    import java.util.List;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    import com.itheima.po.User;
    import com.itheima.po.UserVO;
    
    @Controller
    public class UserController {
        /**
         * 向用户列表页面跳转
         */
        @RequestMapping("/toUser")
        public String selectUsers() {
            return "user";
        }
    
        /**
         * 接收批量删除用户的方法
         */
        @RequestMapping("/deleteUsers")
        public String deleteUsers(Integer[] ids) {
            if (ids != null) {
                for (Integer id : ids) {
                    // 使用输出语句模拟已经删除了用户
                    System.out.println("删除了id为" + id + "的用户!");
                }
            } else {
                System.out.println("ids=null");
            }
            return "success";
        }
    
        /**
         * 向用户批量修改页面跳转
         */
        @RequestMapping("/toUserEdit")
        public String toUserEdit() {
            return "user_edit";
        }
    
        /**
         * 接收批量修改用户的方法
         */
        @RequestMapping("/editUsers")
        public String editUsers(UserVO userList) {
            // 将所有用户数据封装到集合中
            List<User> users = userList.getUsers();
            // 循环输出所有用户信息
            for (User user : users) {
                // 如果接收的用户id不为空,则表示对该用户进行了修改
                if (user.getId() != null) {
                    System.out.println("修改了id为" + user.getId() + "的用户名为:" + user.getUsername());
                }
            }
            return "success";
        }
    }

    2.3 相关实体类(两个)

    package com.itheima.po;
    import java.util.List;
    /**
     * 用户持久化类
     */
    public class User {
        private Integer id;                 // 用户编号
        private String username;           // 用户姓名
        private String address;            // 用户地址
        private List<Orders> ordersList; //用户关联的订单
        public Integer getId() {
            return id;
        }
        public void setId(Integer id) {
            this.id = id;
        }
        public String getUsername() {
            return username;
        }
        public void setUsername(String username) {
            this.username = username;
        }
        public String getAddress() {
            return address;
        }
        public void setAddress(String address) {
            this.address = address;
        }
        public List<Orders> getOrdersList() {
            return ordersList;
        }
        public void setOrdersList(List<Orders> ordersList) {
            this.ordersList = ordersList;
        }
        @Override
        public String toString() {
            return "User [id=" + id + ", username=" + username + ", address="
                    + address + ", ordersList=" + ordersList + "]";
        }
    }
    package com.itheima.po;
    import java.util.List;
    import com.itheima.po.User;
    /**
     * 用户包装类
     */
    public class UserVO {
        private List<User> users;
        public List<User> getUsers() {
            return users;
        }
        public void setUsers(List<User> users) {
            this.users = users;
        }
    }

    2.4 测试,打开页面:http://localhost:8080/ssm/toUserEdit

     勾选核选框(必须),修改名称,提交,后台显示:

  • 相关阅读:
    C#嵌套类
    C#8.0接口默认实现特性
    asp.net Server.Transfer
    clickjacking 攻击
    frame标签和frameset
    javascript打开窗口
    Linux 之 LNMP服务器搭建-PHP
    Linux 之 LNMP服务器搭建-前期准备
    Linux 之 LNMP服务器搭建-Nginx
    Linux 之 Samba服务器
  • 原文地址:https://www.cnblogs.com/nayitian/p/15366165.html
Copyright © 2020-2023  润新知