restful风格的增删改查操作

restFul风格:在超链接进行删除操作时,需要阻止超链接的跳转, 并创建一个表单,有隐藏输入框name为_method,value为DELETE, 与超链接的单击事件绑定,将超链接的href赋值给表单的action, 并submit().进行提交, 即可实现restful风格的删除操作

展示员工列表页面

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2021/7/20
  Time: 17:18
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>员工信息</title>
    <script type="text/javascript" src="static/jquery-1.7.2.js"></script>
    <script type="text/javascript">
      $(function () {
        //给超链接绑定单击事件
        $(".del").click(function () {
            var confirm1 = confirm("您确定要删除"+this.name+"吗?");
            if (confirm1){
                //将超链接中的地址值传递给form标签中的action
                $("form").attr("action",this.href).submit();
                //阻止超链接的默认跳转行为
                return false;
            }else {
                return false;
            }

        });

        });
    </script>
    <style type="text/css">
        table{
            border-collapse: collapse;
            border: blue 1px solid;

        }
        th,td{
            border-collapse: collapse;
            border: blue 1px solid;
        }
    </style>
</head>
<body>
            <table >
                <tr>
                    <th>ID</th>
                    <th>LASTNAME</th>
                    <th>EMAIL</th>
                    <th>GENDER</th>
                    <th>DEPARTMENTNAME</th>
                    <th>OPTIONAL <a href="emp">ADD</a></th>
                </tr>
                <c:forEach items="${emps}" var="emp">
                <tr>
                    <td>${emp.id}</td>
                    <td>${emp.lastName}</td>
                    <td>${emp.email}</td>
                    <td>${emp.gender==0?'女':'男'}</td>
                    <td>${emp.department.departmentName}</td>
                    <td>
                        <a href="emp/${emp.id}" >UPDATE</a>
                        <a class="del" href="emp/${emp.id}"  name="${emp.lastName}">DELETE</a>
                    </td>
                </tr>
                </c:forEach>

            </table>
<form action="" method="post" >
    <input type="hidden" name="_method" value="DELETE">
</form>
</body>
</html>

controller层: /

   * @return: {{@link null}}
     * @Description:  展示所有员工信息
     *
    */
    @RequestMapping(value = "/emps")
    public String  getAll(Map  map){
        Collection<Employee> emps = employeeDao.getAll();
        for (Employee emp : emps) {

            System.out.println(emp);
        }
        //向request作用域中添加值
         map.put("emps",emps);

        return  "list";
    }**
  //删除员工
    @RequestMapping(value = "/emp/{id}",method = RequestMethod.DELETE)
            //接收请求参数id 并赋值给id
    public  String delete(@PathVariable("id")Integer id){
        //通过id删除员工
        employeeDao.delete(id);
        System.out.println("delete方法执行");
        //最后跳转到员工列表页面
        return "redirect:/emps";
    }

编辑员工信息页面(实现添加与修改两个操作,通过引入springMVCform标签可以实现许多功能:如自动回显数据,通过command的键值对)

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2021/7/20
  Time: 17:53
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>修改员工信息</title>
</head>
<body>
<form:form action="${pageContext.servletContext.contextPath}/emp" method="post" modelAttribute="emp">
<%--        由于添加与修改只有两行代码不同 , 因此, 可以通过判断域数据中emp的id是否为空,来判断是添加操作, 还是
            修改操作, 如果为空, 则是添加操作, 反之,则为修改操作
--%>
    <c:if test="${ not empty emp.id}">
    <input  type="hidden" name="id" value="${emp.id}">
    <input  type="hidden" name="_method" value="PUT">
</c:if>


        <table align="center">
                <tr><th colspan="2">
                    <c:if test="${empty emp.id}">添加员工信息</c:if>
                    <c:if test="${not  empty emp.id}">修改员工信息</c:if>

                </th></tr>

                <tr>
                    <td>LASTNAME</td>
                    <td>

                    <form:input path="lastName"></form:input>
                </tr>
                <tr>
                    <td>EMAIL</td>
                    <td><form:input path="email"/></td>
                </tr>
                <tr>
                    <td>GENDER</td>
                    <td>
                        <%--该标签可以自动遍历--%>
                        <form:radiobuttons path="gender" items="${genders}"></form:radiobuttons>
                    </td>
                </tr>
                <tr>
                    <td>DEPARTMENTNAME</td>
                    <td>

                            <form:select path="department.id" items="${depts}" itemLabel="departmentName" itemValue="id"></form:select>

                    </td>
                </tr>
                <tr>
                    <td colspan="2">
                        <c:if test="${empty emp.id}"><input type="submit" value="ADD"></c:if>
                        <c:if test="${not  empty emp.id}"><input type="submit" value="UPDATE"></c:if>

                    </td>
                </tr>


        </table>
            </form:form>
</body>
</html>

**加粗样式:controller层://跳转到添加员工页面

 @RequestMapping(value = "/emp",method = RequestMethod.GET)
    public  String  toAdd(Map<String,Object> map){
        //获取所有的部门信息
        Collection<Department> depts = departmentDao.getDepartments();
    //将所有部门信息的集合存储到域数据中
        map.put("depts",depts);
        //创建存储性别gender的信息
        Map<String,String> genders=new HashMap<>();
        genders.put("0","女");
        genders.put("1","男");
        //将性别信息存储到域数据中
        map.put("genders",genders);
        //form标签有自动回显的功能,会在页面中能够默认获取request作用域中command属性的值
        /*map.put("command",new Employee() );*/
        //若在<form:form> 设置了modelAttribute,就可以自定义回显对象的属性名
        map.put("emp",new Employee());

        return "edit";
    }
//完成添加员工操作
 @RequestMapping(value = "/emp",method = RequestMethod.POST)
    public  String  addEmp(Employee employee){
        employeeDao.save(employee);
        //重定向:展示所有员工信息
        return "redirect:/emps";
    }
//获取要修改的数据, 并回显到修改页面
@RequestMapping(value = "emp/{id}",method = RequestMethod.GET)
public  String  toUpdate(@PathVariable("id") Integer id,Map<String,Object> map){
    //根据要修改的id或者要修改的员工
     Employee emp = employeeDao.get(id);
     System.out.println(emp.getDepartment().getId());
     //创建存储性别gender的信息
     Map<String,String> genders=new HashMap<>();
     genders.put("0","女");
     genders.put("1","男");
     //2.将该员工放入request域中
     map.put("emp",emp);
     Collection<Department> depts = departmentDao.getDepartments();
     //将所有部门信息也放在request作用域中, 便于选择部门
     map.put("depts",depts);
     map.put("genders",genders);
     return  "edit";
 }

//修改员工信息, 方法为:put

  @RequestMapping(value = "/emp",method = RequestMethod.PUT)
     //形参获取修改的对象数据
    public String  update(Employee employee){
        //将对象保存值数据库
        employeeDao.save(employee);
        //修改完成后, 重定向到员工列表页面
        return "redirect:/emps";
     }

**

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值