spingmvc学习总结——8——批量增加和删除

springmvc如何实现批量增加和删除呢?
首先看需求,一个jsp页面,可以选择多个学生进行删除:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<%
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/";
    pageContext.setAttribute("basePath", basePath);
%>
<body>
    <form action="${basePath }emp/deteteAll.action" method="post">
        <table border="1" align="center">
            <tr>
                <th>编号</th>
                <th>姓名</th>
            </tr>
            <tr>
                <td><input type="checkbox" name = "id" value="1"></td>
                <td>张三1</td>
            </tr>
            <tr>
                <td><input type="checkbox" name = "id" value="2"></td>
                <td>张三2</td>
            </tr>
            <tr>
                <td><input type="checkbox" name = "id" value="3"></td>
                <td>张三3</td>
            </tr>
            <tr>
                <td><input type="checkbox" name = "id" value="4"></td>
                <td>张三4</td>
            </tr>
            <tr>
                <td><input type="checkbox" name = "id" value="5"></td>
                <td>张三5</td>
            </tr>
            <tr>
                <td><input type="checkbox" name = "id" value="6"></td>
                <td>张三6</td>
            </tr>
            <tr>
                <td><input type="submit" value="删除"></td>
                <td></td>
            </tr>
        </table>
    </form>
</body>
</html>

当我们提交后,springmvc如何获取所有选中的元素?
看一下我们的Action:

@Controller
@RequestMapping(value = "/emp")
public class EmpAction {
    @RequestMapping(value = "/deteteAll",method=RequestMethod.POST)
    public String deteteAll(Model model,int[] id) {
        model.addAttribute("message", "批量删除员工成功");
        System.out.println("需要删除的员工元素");
        for (int i : id) {
            System.out.print(i+" ,  ");
        }
        return "/jsp/ok.jsp";
    }
}

我们只需要在业务方法中,添加数组属性,但是该数组的名字和jsp页面中的name属性的值要一致,这样才会获取选中的checkbox的value值。
这里说的是数组,那如果是一个List集合呢?
创建一个JaveBean,,:

/**
 * 员工
 * @author user
 *
 */
public class Emp {
    private String username;
    private Double salary;
    public Emp() {
        // TODO Auto-generated constructor stub
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public Double getSalary() {
        return salary;
    }
    public void setSalary(Double salary) {
        this.salary = salary;
    }

}

在创建一个bean用来封装多个员工:

/**
 * 封装多个员工
 * @author user
 */

import java.util.ArrayList;
import java.util.List;

public class Bean {
    private List<Emp> empList = new ArrayList<Emp>();
    public Bean() {
        // TODO Auto-generated constructor stub
    }
    public List<Emp> getEmpList() {
        return empList;
    }

    public void setEmpList(List<Emp> empList) {
        this.empList = empList;
    }
}

在我们的action的业务方法中传入封装了多个Emp的Bean参数。

/**
 * 员工Actio批量增加
 * 单例
 * @author user
 *
 */
@Controller
@RequestMapping(value = "/emp")
public class EmpAction {
    /*
     * 批量增加
     */
    @RequestMapping(value = "/addAll",method=RequestMethod.POST)
    public String deteteAll(Model model,Bean bean) {
        model.addAttribute("message", "批量增加员工成功");
        System.out.println(bean.getEmpList().size());
        for (Emp emp : bean.getEmpList()) {
            System.out.println(emp.getUsername()+"  "+emp.getSalary());
        }
        return "/jsp/ok.jsp";
    }
}

然后看一下我们的jsp页面如何写:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<%
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/";
    pageContext.setAttribute("basePath", basePath);
%>
<body>
    <form action="${basePath}emp/addAll.action" method="POST">
        <table border="2" align="center">
            <caption><h2>批量注册员工</h2></caption>
            <tr>
                <td><input type="text" name="empList[0].username" value="哈哈"/></td>
                <td><input type="text" name="empList[0].salary" value="7000"/></td>
            </tr>
            <tr>
                <td><input type="text" name="empList[1].username" value="呵呵"/></td>
                <td><input type="text" name="empList[1].salary" value="7500"/></td>
            </tr>
            <tr>
                <td><input type="text" name="empList[2].username" value="班长"/></td>
                <td><input type="text" name="empList[2].salary" value="8000"/></td>
            </tr>
            <tr>
                <td><input type="text" name="empList[3].username" value="键状哥"/></td>
                <td><input type="text" name="empList[3].salary" value="8000"/></td>
            </tr>
            <tr>
                <td><input type="text" name="empList[4].username" value="绿同学"/></td>
                <td><input type="text" name="empList[4].salary" value="9000"/></td>
            </tr>
            <tr>
                <td colspan="2" align="center">
                    <input type="submit" value="批量注册"/>
                </td>
            </tr>
        </table>
    </form>
</body>
</html>

这里要注意他的name属性值,empList和我们的Bean中的list属性一样
.username和.salary和Emp中的属性一样,这样springmvc才会自动为list集合赋值。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值