zoukankan      html  css  js  c++  java
  • SpringMVC框架04——RESTful入门

    1、RESTful的基本概念

    REST(Representational State Transfer)表述性状态转移,REST并不是一种创新技术,它指的是一组架构约束条件和原则,符合REST的约束条件和原则的架构,就称它为RESTful架构。
    RESTful具体来讲就是HTTP协议的四种形式表示四种基本操作:
    GET(获取资源)、POST(新建资源)、PUT(修改资源)、DELETE(删除资源)

    2、RESTful架构的特点

    统一了客户端访问资源的接口
    url更加简洁,易于理解,便于扩展
    有利于不同系统之间的资源共享

    3、RESTful开发风格示例

    • 查询课程,method='get',http://localhost:8080/id
    • 添加课程,method='post',http://localhost:8080/course
    • 删除课程,method='delete',http://localhost:8080/id
    • 修改课程,method='put',http://localhost:8080/course

    4、RESTful的代码实现

    4.1、配置过滤器

    在web.xml配置文件中编辑代码:

    <!--设置HTTP请求方式-->
    <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>

    4.2、创建实体类

    示例代码:

    public class Course {
        private int id;
        private String name;
        private double price;
    
        //getter,setter方法
        
    }

    4.3、创建DAO

    示例代码:

    @Repository
    public class CourseDao {
    
        //模拟数据库存储
        private Map<Integer, Course> courseMap = new HashMap<>();
    
        /**
         * 添加课程
         */
        public void add(Course course){
            courseMap.put(course.getId(),course);
        }
    
        /**
         * 查询所有课程
         */
        public Collection<Course> getAll(){
            return courseMap.values();
        }
    
        /**
         * 通过ID查询课程
         */
        public Course getById(int id){
            return courseMap.get(id);
        }
    
        /**
         * 修改课程
         */
        public void update(Course course){
            courseMap.put(course.getId(),course);
        }
    
        /**
         * 删除课程
         */
        public void delete(int id){
            courseMap.remove(id);
        }
    
    }

    4.4、POST添加数据

    Controller类中的业务方法:

    @Controller
    public class CourseController {
    
        @Autowired
        private CourseDao courseDao;
    
        /**
         * 添加课程
         */
        @PostMapping("/add")
        public String add(Course course){
            courseDao.add(course);
            System.out.println(course);
            return "redirect:/getAll";
        }
    }

    add.jsp页面

    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <%@ page isELIgnored="false" %>
    <html>
    <head>
        <title>添加课程</title>
    </head>
    <body>
    <h1>添加课程</h1>
    <hr>
    <form action="${pageContext.request.contextPath}/add" method="post">
        <p>
            课程编号:<input type="text" name="id">
        </p>
        <p>
            课程名称:<input type="text" name="name">
        </p>
        <p>
            课程价格:<input type="text" name="price">
        </p>
        <p>
            <input type="submit" value="提交">
        </p>
    </form>
    </body>
    </html>

    4.5、GET查询数据

    Controller类中的业务方法:

    /**
     * 查询所有课程
     */
    @GetMapping("/getAll")
    public String getAll(Model model){
        model.addAttribute("courses",courseDao.getAll());
        System.out.println("getAll.......");
        return "index";
    }
    
    /**
     * 根据ID查询课程
     */
    @GetMapping("/getById/{id}")
    public String getById(@PathVariable("id") int id,Model model){
        model.addAttribute("course",courseDao.getById(id));
        return "edit";
    }

    index.jsp页面,用于展示数据:

    <table border="1" width="80%">
        <tr>
            <th>课程编号</th>
            <th>课程名称</th>
            <th>课程价格</th>
            <th>编辑</th>
        </tr>
        <c:forEach items="${courses}" var="course">
            <tr>
                <td>${course.id}</td>
                <td>${course.name}</td>
                <td>${course.price}</td>
                <td>
                    <form action="${pageContext.request.contextPath}/getById/${course.id}" method="get">
                        <input type="submit" value="编辑">
                    </form>
                    &nbsp;&nbsp;
                    <form action="${pageContext.request.contextPath}/delete/${course.id}" method="post">
                        <input type="hidden" name="_method" value="DELETE">
                        <input type="submit" value="删除">
                    </form>
                </td>
            </tr>
        </c:forEach>
    </table>

    4.6、PUT修改数据

    Controller类中的业务方法:

    /**
     * 修改课程
     */
    @PutMapping("/update")
    public String update(Course course){
        courseDao.update(course);
        return "redirect:/getAll";
    }

    edit.jsp页面

    <form action="${pageContext.request.contextPath}/update" method="post">
        <p>
            课程编号:<input type="text" name="id" value="${course.id}">
        </p>
        <p>
            课程名称:<input type="text" name="name" value="${course.name}">
        </p>
        <p>
            课程价格:<input type="text" name="price" value="${course.price}">
        </p>
        <p>
            <input type="hidden" name="_method" value="PUT">
            <input type="submit" value="提交">
        </p>
    </form>

    4.7、DELETE删除数据

    Controller类中的业务方法:

    /**
     * 删除课程
     */
    @DeleteMapping("/delete/{id}")
    public String delete(@PathVariable("id") int id){
        courseDao.delete(id);
        return "redirect:/getAll";
    }

    index.jsp页面中的删除按钮:

    <form action="${pageContext.request.contextPath}/delete/${course.id}" method="post">
      <input type="hidden" name="_method" value="DELETE">
      <input type="submit" value="删除">
    </form>
  • 相关阅读:
    JavaScript 电话手机号码正则表达式
    查找和删除sqlserver数据库中的重复记录
    配置文件app.config
    数据类型 ntext 和 varchar 在 equal to 运算符中不兼容的错误信息
    用jscript处理repeater生成的表格, 实现分页打印
    我在Repeater控件中有CheckBox控件,我怎么能选中CheckBox控件后,Repeater控件重新绑定一下?
    种方式遍历repeater中的CheckBox全选
    Repeater导出为excel格式
    给Repeater、Datalist和Datagrid增加自动编号
    js中对datagrid ,repeater的checkbox进行全选反选
  • 原文地址:https://www.cnblogs.com/jpwz/p/10540067.html
Copyright © 2011-2022 走看看