Restful的详细介绍~

简介: Restful的详细介绍~

RESTFUL简介:

Restful是我们看待服务器的一种方式,我们都知道Java一切皆对象,因此在Java中,我们可以将所有的内容都看成对象,而在这里,RESTFUL是我们看待服务器的一种方式,我们可将服务器中的所有内容看成资源,"一切皆资源",每个资源是服务器上一个可命名的抽象概念,例如一张图片,一个文件,一首歌,数据库中的一张表等等,这些都可以是资源,那么我们该如何表示这个资源呢?在Restful中,我们可通过一个名词进行表示,其实也就是该资源的名字,就比如,在现实生活中,我们需要去一个班级寻找一个学生,那么就需要通过其名字,名字就是学生作为资源的名称,因此在服务器中,我们寻找一个资源,也是需要通过其名称进行寻找

RESTFUL的模拟实现:

在服务器中,需要某一个资源,我们可通过其名称,但同一个资源又有不同的操作方式,那么该如何表示对同一个资源的不同操作方式呢? 在Restful中,我们可通过四个表示操作方式的动词,GET[查询资源,获取资源],POST[新建资源],PUT[修改资源],DELETE[删除资源],REST风格与传统方式不同的是,它提倡URL地址使用统一的风格设计,从前到后各个单词使用斜杠分开,不使用传统的问号键值对方式携带请求参数,而是将要发送给服务器的数据作为URL地址的一部分,以保证整体风格的一致性,如下所示为传统方式与REST风格的对比:

使用RESTFUL实现对信息的操作的准备工作

第一步:创建新的maven项目–>指定打包的方式,并且导入依赖

 <packaging>war</packaging>
    <dependencies>
        <!-- SpringMVC-->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>5.3.1</version>
        </dependency>
        <!-- 日志-->
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.2.3</version>
        </dependency>
        <!-- ServletAPI-->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
        </dependency>
        <!-- Spring和Thymeleaf整合包-->
        <dependency>
            <groupId>org.thymeleaf</groupId>
            <artifactId>thymeleaf-spring5</artifactId>
            <version>3.0.12.RELEASE</version>
        </dependency>
    </dependencies>

第二步:在项目结构中,添加web模块,如下所示:

第三步:在web.xml文件中设置编码过滤器和前端控制器

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">
    <!--设置Spring的编码过滤器 -->
    <filter>
        <filter-name>CharacterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <!--过滤器将强制使用指定的字符编码,无论请求中是否指定了其他编码-->
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <!--设置SpringMVC的前端控制器 -->
    <servlet>
        <servlet-name>SpringMVC</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:restful.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>SpringMVC</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>

注意:如果web.xml文件中含有多个过滤器,那么编码过滤器需要放在最前面,原因是编码过滤器设置的是编码,而在设置编码之前,我们不能获取任何的请求参数,否则会导致设置的编码无效果

第四步:创建控制层Controller

第五步:在resources目录下创建XML文件,并配置,如下所示

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <context:component-scan base-package="Controller" ></context:component-scan>
    <!-- 配置Thymeleaf视图解析器-->
    <bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
        <property name="order" value="1"/>
        <property name="characterEncoding" value="UTF-8"/>
        <property name="templateEngine">
            <bean class="org.thymeleaf.spring5.SpringTemplateEngine">
                <property name="templateResolver">
                    <bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
                        <!-- 视图前缀 -->
                        <property name="prefix" value="/WEB-INF/templates/"/>
                        <!-- 视图后缀 -->
                        <property name="suffix" value=".html"/>
                        <property name="templateMode" value="HTML5"/>
                        <property name="characterEncoding" value="UTF-8"/>
                    </bean>
                </property>
            </bean>
        </property>
    </bean>
    <mvc:annotation-driven/>
    <mvc:view-controller path="/" view-name="index"></mvc:view-controller>
</beans>

第六步:在WEB-INF目录下,创建templates子目录,并且创建index.html和success.html

index.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>index</title>
</head>
<body>
<h1>欢迎进入首页!</h1>
</body>
</html>

success.html:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Success</title>
</head>
<body>
<h1>Success.html</h1>
</body>
</html>

使用RESTFUL实现对信息的查询操作:

创建控制器方法:

package Controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class RestfulTestController {
    @RequestMapping(value = "/user", method = RequestMethod.GET)
    public String getUser() {
        System.out.println("查询所有的用户信息-->/user->get");
        return "success";
    }
}

index.html文件中添加如下超链接:

<a th:href="@{/user}">查询用户信息</a>

浏览器中运行如下所示:

控制台输出:

使用RESTFUL实现对信息的条件查询操作:

创建控制器方法:

package Controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class RestfulTestController {
    @RequestMapping(value = "/user/{id}",method = RequestMethod.GET)
    public String getUserId(@PathVariable("id") Integer id){
        System.out.println("通过id查询用户信息-->/user/"+id+"->get");
        return "success";
    }
}

index.html文件中添加如下超链接:

<a th:href="@{/user/1}">查询id为1的用户信息</a>

浏览器中运行如下所示:

控制台输出:

使用RESTFUL实现对信息的添加操作:

创建控制器方法:

package Controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
public class RestfulTestController {
    @RequestMapping(value = "/user",method = RequestMethod.POST)
    public String insertUser(){
        System.out.println("添加用户信息--->/user->post");
        return "success";
    }
}

index.html文件中添加如下超链接:

<form th:action="@{/user}" method="post">
    <input type="submit" value="添加用户信息">
</form>

浏览器中运行如下所示:

控制台输出:

注意:浏览器目前只能发送get和post请求,若要发送put和delete请求,需要在web.xml中配置一个过滤器HiddenHttpMethodFilter处理隐藏的 HTTP 方法,如下所示,在web.xml文件中设置请求方式的过滤器


隐藏的 HTTP 方法是一种在 HTML 表单中使用 POST 方法来模拟其他 HTTP 方法(如 PUT、DELETE、PATCH)的技术。通过在表单中添加一个name为 _method 的隐藏字段(type=hidden),并将其value设置为要模拟的 HTTP 方法(如:delete/put),可以实现对应的操作。通过配置隐藏 HTTP 方法过滤器,可以在 Spring MVC 应用程序中使用 POST 方法来模拟其他 HTTP 方法,以便支持 RESTful 风格的请求。

<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>

使用RESTFUL实现对信息的修改操作:

在index.html中添加form表单实现修改操作

<form th:action="@{/user}" method="post">
    <input type="hidden" name="_method" value="put">
    <input type="submit" value="修改用户信息">
</form>

编写控制器方法

 @RequestMapping(value = "/user",method = RequestMethod.PUT)
    public String updateUser(){
        System.out.println("修改用户信息--->/user->put");
        return "success";
    }

重新部署项目,运行结果如下所示:

控制台输出如下所示:

使用RESTFUL实现对信息的删除操作:

在index.html中添加form表单实现删除操作

<form th:action="@{/user}" method="post">
    <input type="hidden" name="_method" value="delete">
    <input type="submit" value="删除用户信息">
</form>

编写控制器方法

@RequestMapping(value = "/user",method = RequestMethod.DELETE)
    public String deleteUser(){
        System.out.println("删除用户信息--->/user->delete");
        return "success";
    }

重新部署项目,运行结果如下所示:

控制台输出如下所示:

Restful案例准备工作:

第一步:创建实体类

package Pojo;
public class Employee {
    private Integer id;
    private String lastName;
    private Integer gender;
    public Integer getId() {
        return id;
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    public Integer getGender() {
        return gender;
    }
    public void setGender(Integer gender) {
        this.gender = gender;
    }
    @Override
    public String toString() {
        return "Employee{" +
                "id=" + id +
                ", lastName='" + lastName + '\'' +
                ", gender=" + gender +
                '}';
    }
    public Employee(Integer id, String lastName, Integer gender) {
        this.id = id;
        this.lastName = lastName;
        this.gender = gender;
    }
    public Employee() {
    }
}

第二步:创建实体类DAO层

package Dao;
import Pojo.Employee;
import org.springframework.stereotype.Repository;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
@Repository
public class EmployeeDao {
    private  static Map<Integer,Employee> employeeMap=null;
    //初始化数据
    static {
        employeeMap=new HashMap<Integer,Employee>();
        employeeMap.put(1001,new Employee(1001,"E-AA",1));
        employeeMap.put(1002,new Employee(1002,"E-BB",1));
        employeeMap.put(1003,new Employee(1003,"E-CC",0));
        employeeMap.put(1004,new Employee(1001,"E-DD",0));
        employeeMap.put(1005,new Employee(1005,"E-EE",1));
    }
    private static Integer initID=1006;
    public void save(Employee employee){
      //设置当前的用户id自增
        if(employee.getId()==null){
            employee.setId(initID++);
        }
        employeeMap.put(employee.getId(),employee);
    }
    //获得用户的所有信息
    public Collection<Employee> getAll(){
        return employeeMap.values();
    }
    //根据id获取用户信息
    public Employee get(Integer id){
        return employeeMap.get(id);
    }
    //根据id删除用户信息
    public void delete(Integer id){
        employeeMap.remove(id);
    }
}

第三步:创建实体类控制层

package Controller;
import org.springframework.stereotype.Controller;
@Controller
public class EmployeeController {
}

实现列表功能:

package Controller;
import Dao.EmployeeDao;
import Pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import java.util.Collection;
@Controller
public class EmployeeController {
    @Autowired
    //注意:如果这里的employeeDao报错,那么有两个原因:1:DAO层未添加@Repository注解 2:DAO层的类未被XML文件扫描到
    private EmployeeDao employeeDao;
    @RequestMapping(value = "/employee",method = RequestMethod.GET)
    public String getAllEmployee(Model model ){
        //获取所有的用户信息
        Collection<Employee> employeeCollection=employeeDao.getAll();
        //将所有员工的信息在请求域中共享
     model.addAttribute("employeeCollection",employeeCollection);
        return "employee_list";
    }
}

employee_list.html:


在 Thymeleaf 中,th:each 是一个迭代器属性,用于循环遍历集合或数组。它的语法是 th:each="item : ${collection}",

其中 item 是每次迭代的元素,${collection} 是要遍历的集合或数组,将每个元素赋值给名为 item 的变量。通常,这个循环语句会嵌套在 HTML 的表格(<table>)中,以便在每次迭代中生成表格的行(<tr>)

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>employee_list</title>
</head>
<body>
<table>
  <tr>
  <th colspan="5">employee list</th>
  </tr>
  <tr>
    <th>id</th>
    <th>lastName</th>
    <th>email</th>
    <th>gender</th>
    <th>options</th>
  </tr>
  <!--由于表格中的信息需要我们从集合中一个个拿取,因此需要使用到迭代器来遍历 -->
  <tr th:each="employee:${employeeCollection}">
    <td th:text="${employee.id}"></td>
    <td th:text="${employee.LastName}"></td>
    <td th:text="${employee.email}"></td>
    <td th:text="${employee.gender}"></td>
    <td>
      <a href="">delete</a>
      <a href="">update</a>
    </td>
  </tr>
</table>
</body>
</html>

index.html:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>index</title>
</head>
<body>
<h1>欢迎进入首页!</h1>
<a th:href="@{/employee}">查询所有的员工信息 </a>
</body>
</html>

重新部署项目,运行如下所示:

RestFul处理静态资源:

什么是静态资源?

静态资源是指在Web应用程序中不经常变化的文件,如HTML、CSS、JavaScript、图像、字体文件等。这些文件在服务器上存储为静态文件,并在客户端请求时直接返回给浏览器,不需要经过服务器端的处理。


静态资源通常用于呈现网页的外观和功能,如布局、样式、交互行为等。它们不包含动态生成的内容,而是在开发过程中预先创建和维护的文件。


与动态资源相比,静态资源的特点是内容相对稳定,不需要经常变化。由于不需要服务器端的处理,静态资源的响应速度通常更快,可以有效地减轻服务器的负载。


在Web开发中,静态资源通常存储在服务器的特定目录下,并通过URL路径来访问。例如,一个CSS文件可以通过<link>标签的href属性引入,一个JavaScript文件可以通过<script>标签的src属性引入。

在工程中使用静态资源:

第一步:将静态资源放置当前目录的webapp下,注意不要放在WEB-INF目录下了,如下所示:

第二步:

在html文件的<head>标签中通过<link>标签引入该静态资源,如下所示:

<!--注意:这里的路径不需要加webapp,因为webapp只是用来存放静态资源的,具体的路径看target中的目录,如下所示,target中是以static直接开头的 -->
<link rel="stylesheet" th:href="@{/static/css/index_work.css}">

第三步:在XML文件中添加如下所示两个标签,否则静态资源不会被应用的到当前项目,并且打开浏览器的检查页面会出现404错误

<mvc:annotation-driven/>
<mvc:default-servlet-handler/>

原因是:如果当前工程和tomcat配置了同样的资源,那么以工程的为准,由于当前工程的web.xml配置的前端控制器DispatcherServlet的url-pattern是/,tomcat的web.xml配置的DefaultServlet的url-pattern也是/,此时,浏览器发送的请求会优先被DispatcherServlet进行处理,但是DispatcherServlet无法处理静态资源,这就导致我们引入的静态资源发生404错误


解决方法为我们需要同时配置<mvc:default-servlet-handler/>和<mvc:annotation-driven/>,这表示浏览器发送的请求会先被DispatcherServlet处理,无法处理再交给DefaultServlet处理


引入静态资源后,我们需要先对当前项目进行清理,再对其重新进行打包,防止target中没有该静态资源


此时部署项目,显示如下所示:

实现添加功能:

第一步:配置关于添加功能的视图控制器,如下所示:

<mvc:view-controller path="/to/add" view-name="employee_add"></mvc:view-controller>

第二步创建对应的视图:添加页面的视图employee_add.html

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
  <meta charset="UTF-8">
  <title>add employee</title>
  <link rel="stylesheet" th:href="@{/static/css/index_work.css}">
</head>
<body>
<form th:action="@{/employee}" method="post">
<table>
  <tr>
    <th colspan="2">add employee</th>
  </tr>
  <tr>
    <td>lastName</td>
    <td>
      <input type="text" name="lastName">
    </td>
  </tr>
  <tr>
    <td>email</td>
    <td>
      <input type="text" name="email">
    </td>
  </tr>
  <tr>
    <td>gender</td>
    <td>
      <input type="radio" name="gender" value="1">male
      <input type="radio" name="gender" value="0">famale
    </td>
  </tr>
  <tr>
    <td colspan="2">
      <input type="submit" value="add">
    </td>
  </tr>
</table>
</form>
</body>
</html>

第三步:实现点击添加功能即跳转到该页面,如下所示:在employee_list.html中进行如下修改

第二步,编写控制器方法处理添加的请求

//虽然这里的路径具体信息也是/employee,但是二者的请求方式不同,而我们在index.html中是以超链接也就是get方式对请求进行处理,因此会匹配到查询的控制器方法
@RequestMapping(value = "/employee",method = RequestMethod.POST)
    public String addEmployee(Employee employee){//直接获取实体类对象
        employeeDao.save(employee);
        //使用户看到添加完成之后的页面,因此继续访问列表功能
        return "redirect:/employee";
    }

实现修改功能:

修改功能为什么不使用视图控制器呢?

原因是视图控制器只适用于简单的请求处理场景,对于复杂的业务逻辑,可能还需要编写自定义的控制器来处理请求,对于上述的添加功能,我们只需要跳转到添加用户信息的页面即可,而修改功能,业务逻辑就复杂了一些,我们需要先根据id获取需要修改的用户信息,再进行修改


第一步:创建处理修改用户信息的控制器方法

@RequestMapping(value = "/employee",method = RequestMethod.PUT)
    public String updateEmployee(Employee employee){
        employeeDao.save(employee);
        return "redirect:/employee";
    }

第二步:在employee_list.html中添加有关修改功能的超链接

<!--正确写法 -->
 <a th:href="@{'/employee/'+${employee.id}}">update</a>
 <!--错误写法:${employee.id}是请求参数,不能直接以路径的形式进行传输 -->
<!-- <a th:href="@{/employee/${employee.id}}">update</a>-->

第二步,编写控制器方法处理修改的请求

@RequestMapping(value = "/employee/{id}",method = RequestMethod.GET)
    public String toupdateEmployee(@PathVariable("id") Integer id,Model model){//修改功能
        Employee employee= employeeDao.get(id);
        model.addAttribute("employee",employee);
        //跳转到上文获取到的id的修改页面
        return "employee_update";
    }

第三步:创建修改功能的视图

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
  <meta charset="UTF-8">
  <title>update employee</title>
  <link rel="stylesheet" th:href="@{/static/css/index_work.css}">
</head>
<body>
<form th:action="@{/employee}" method="post">
  <input type="hidden" name="_method" value="put">
  <input type="hidden" name="id" th:value="${employee.id}">
  <table>
    <tr>
      <th colspan="2">add employee</th>
    </tr>
    <tr>
      <td>lastName</td>
      <td>
        <input type="text" name="lastName" th:value="${employee.lastName}">
      </td>
    </tr>
    <tr>
      <td>email</td>
      <td>
        <input type="text" name="email" th:value="${employee.email}">
      </td>
    </tr>
    <tr>
      <td>gender</td>
      <td>
        <input type="radio" name="gender" value="1" th:field="${employee.gender}">male
        <input type="radio" name="gender" value="0" th:field="${employee.gender}">famale
      </td>
    </tr>
    <tr>
      <td colspan="2">
        <input type="submit" value="update">
      </td>
    </tr>
  </table>
</form>
</body>
</html>

实现删除功能:

删除功能的麻烦之处在于,需要使用超链接来控制表单的提交,因为删除功能要发送的请求是delete,因此我们需要一个表单,将其请求方式设置为post,再传递请求参数为_method,其值为delete,让其表单提交,我们才能发送delete请求


但是不能如下所示这样写

<a th:href="@{'/employee/'+${employee.id}}">delete</a>

原因是当前的超链接功能相同,那么都会跳转到修改的页面,因此需要重新设置请求方式为delete的表单

<form method="post">
        <input type="hidden" name="_method" value="delete">
</form>

点击该超链接,控制该表单的提交,把当前的href属性赋值给下面表单的action属性

<script type="text/javascript" th:src="@{/static/js/vue.js}"></script>
<script type="text/javascript">
        var vue=new Vue({
            el:"#app",
            methods:{
                deleteEmployee(){
                    //获取form表单
                    var form=document.getElementsByTagName("form")[0];
                    //将超链接的href属性值赋值给form表单的action属性,event.target表示当前触发时间的标签
                    form.action=event.target.href;
                    //表单提交
                    form.submit();
                    //阻止超链接的默认行为
                    event.preventDefault();
                }
            }
        });
</script>

注意:超链接都有默认行为点击超链接一定会将请求发送到当前href所设置的请求地址,当前我们是通过表单提交的,我们不能让超链接执行其默认行为,因此需要通过preventDefault()去阻止其行为。

employee_list.html完整代码如下所示:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
  <meta charset="UTF-8">
  <title>employee_list</title>
  <link rel="stylesheet" th:href="@{/static/css/index_work.css}">
</head>
<body>
<div id="app">
<table >
  <tr>
    <th colspan="5">employee list</th>
  </tr>
  <tr>
    <th>id</th>
    <th>lastName</th>
    <th>email</th>
    <th>gender</th>
    <th>options(<a th:href="@{/to/add}">add</a>)</th>
  </tr>
  <tr th:each="employee:${employeeCollection}">
    <td th:text="${employee.id}"></td>
    <td th:text="${employee.LastName}"></td>
    <td th:text="${employee.email}"></td>
    <td th:text="${employee.gender}"></td>
    <td>
        <a th:href="@{'/employee/'+${employee.id}}">update</a>
        <a @click="deleteEmployee()" th:href="@{'/employee/'+${employee.id}}">delete</a>
     </td>
   </tr>
 </table>
<form method="post">
        <input type="hidden" name="_method" value="delete">
</form>
</div>
<script type="text/javascript" th:src="@{/static/js/vue.js}"></script>
<script type="text/javascript">
        var vue=new Vue({
            el:"#app",
            methods:{
                deleteEmployee(){
                    //获取form表单
                    var form=document.getElementsByTagName("form")[0];
                    //将超链接的href属性值赋值给form表单的action属性,event.target表示当前触发时间的标签
                    form.action=event.target.href;
                    //表单提交
                    form.submit();
                    //阻止超链接的默认行为
                    event.preventDefault();
                }
            }
        });
</script>
 </body>
 </html>

编写其对应的控制器方法:

@RequestMapping(value = "/employee/{id}", method =RequestMethod.DELETE)
    public String deleteEmployee(@PathVariable("id") Integer id){
        employeeDao.delete(id);
        return "redirect:/employee";
    }
相关文章
|
19天前
|
存储 JSON 自然语言处理
ES Restful操作
ES Restful操作
|
2月前
|
XML 安全 数据库
RESTful架构
什么是RESTful架构
|
3月前
|
XML JSON Java
什么是RESTful
什么是RESTful
|
3月前
|
XML JSON Apache
理解RESTful架构
理解RESTful架构
|
6月前
|
JSON 缓存 数据格式
55SpringMVC - RESTful支持
55SpringMVC - RESTful支持
14 0
|
XML JSON API
Restfule
+ 无状态 + 面向资源,即访问地址时使用的是名词形式 + 使用HTTP动词
|
JSON 缓存 API
6.2 Restful
讲解HTTP开发中Restful风格
|
Java 网络架构
RESTful的详解
RESTful 也称为REST(英文:Representational State Transfer)即表现层状态传递,它是一种软件架构风格或设计风格,而不是一个标准。
147 0
RESTful的详解
|
XML JSON 前端开发
RESTful 架构到底是什么?
RESTful 架构到底是什么?
162 0
RESTful 架构到底是什么?
|
SQL JSON API
Restful
Restful
148 0