REST(Representational State Transfer)

 简述:

即表述性转移,是目前最流行的一种软件架构风格。它结构清晰、易于理解、有较好的扩展性。

    Spring REST 风格可以简单理解为:使用 URL 表示资源时,每个资源都用一个独一无二的 URL 来表示,并使用 HTTP 方法表示操作,即准确描述服务器对资源的处理动作(GET、POST、PUT、DELETE),实现资源的增删改查。

    * GET:表示获取资源

    * POST:表示新建资源

    * PUT:表示更新资源

    * DELETE:表示删除资源

资源操作:POST、DELETE、PUT、GET,使用不同方法对资源进行操作

  分别对应添加、删除、修改、查询

步骤:

    1.web.xml中添加HiddenHttpMethodFilter过滤器

将发送的请求通过该filter过滤后到达servlet,_method可以传递 put和delete请求

<filter>
    <filter-name>HiddenHttpMethodFilter</filter-name>
    <filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>HiddenHttpMethodFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

  2.增删改查

package com.openlab.controller;
import com.openlab.bean.Employee;
import com.openlab.dao.EmpDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


import java.util.List;
import java.util.Map;
@Controller
public class EmployeeController {
    @Autowired
    private EmpDao empDao;
    //查看所有员工信息
//    @RequestMapping(value = "/emp",method = RequestMethod.GET)
    @GetMapping("/emp")
    public String emplist(Map<String,Object>map){
        System.out.println(empDao+"#******************");
        List<Employee> list = empDao.findAll();
        map.put("list",list);


        return"emplist";
    }
//    @RequestMapping(value = "/emp",method = RequestMethod.POST)
    @PostMapping("/emp")
    public String empSave(Employee employee){
        empDao.save(employee);
        return "redirect:/emp";
    }
//    @RequestMapping(value = "/emp/{id}",method = RequestMethod.DELETE)
    @DeleteMapping("/emp/{id}")
    public String  empdelete(@PathVariable("id") Integer empid){
        System.out.println(empid+"***");
        empDao.delete(empid);
        return "redirect:/emp";
    }
//    @RequestMapping(value = "/emp/{id}",method = RequestMethod.GET)
    @GetMapping("/emp/{id}")
    public String emppreupdate(@PathVariable("id") Integer empid,Map<String,Object>map ){
        Employee employee = empDao.findById(empid);
        System.out.println(empid);
        map.put("emp",employee);
        return "empupdate";
    }
}
get请求和post请求可以直接发送
put和delete请求需要隐藏数据

3.jsp页面:

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2021/9/19
  Time: 14:07
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
员工信息展示页面
<%=request.getContextPath()%>
<a href="${pageContext.request.contextPath}/dept/deptlist">查看部门信息</a>
<%--展示所有员工信息--%>
<input type="button" value="添加" onclick="window.location.href='${pageContext.request.contextPath}/empadd'">
    <table>
         <tr>
             <td>员工编号</td>
             <td>员工姓名</td>
             <td>员工性别</td>
             <td>员工电话</td>
             <td>操作</td>
         </tr>
<c:forEach items="${list}" var="emp">
    <tr>
        <td>${emp.empid}</td>
        <td>${emp.empname}</td>
        <td>${emp.empsex}</td>
        <td>${emp.phone}</td>
        <td>
            <a href="${pageContext.request.contextPath}/emp/${emp.empid}">修改</a>
            <a href="javascript:void(0)" onclick="deleteData(${emp.empid});">删除</a>
        </td>
    </tr>
</c:forEach>
    </table>
<form method="post" action="" name="form">
    <input type="hidden" name="_method" value="delete"/>
</form>
<script>
    function deleteData(id){
        var formEl = document.getElementsByName("form")[0];
        formEl.action="${pageContext.request.contextPath}/emp/"+id;
        formEl.submit();
    }
</script>
</body>
</html>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值