zoukankan      html  css  js  c++  java
  • RESTful使用方法

    1.什么是RESTful

     2.对应增删改查

     注意:HTTP协议中并无PUT和DELETE的操作因此需在web.xml添加HiddenHttpMethodFilter用于识别PUT和DELETE操作

       原理是:从POST操作中额外分出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>
    </web-app>

    并且使用PUT操作和DELETE操作时需在jsp页面添加隐藏的input标签,以表示PUT或DELETE

    如使用删除操作:

                                <form action="${pageContext.request.contextPath}/delete/${course.id}" method="post">
                                    <button class="btn btn-danger btn-sm delete_btn" type="submit">
                                        <input type="hidden" name="_method" value="DELETE"/>    <%-- 应添加此input标签 --%>
                                        <span class="glyphicon glyphicon-trash">删除</span>
                                    </button>
                                </form>

    3.controller的定义:

    package com.yzy.controller;
    
    
    import com.yzy.dao.courseDao;
    import com.yzy.entity.Course;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.*;
    import org.springframework.web.servlet.ModelAndView;
    
    import javax.annotation.Resource;
    
    @Controller
    public class courseController {
    
        @Resource(name = "courseDao")
        private courseDao courseDao;
    
        @PostMapping("/add")
        public String add(Course course){
            courseDao.add(course);
            return "redirect:/getAll";   //重定向回getAll
        }
    
        @GetMapping("/getAll")
        public ModelAndView getAll(){
            ModelAndView modelAndView=new ModelAndView();
            modelAndView.addObject("courses",courseDao.getAll());
            modelAndView.setViewName("index");
            return modelAndView;
        }
    
        @GetMapping("/getById/{id}")
        public ModelAndView updateBefore(@PathVariable int id){     //id从路径中的{id}获取
            ModelAndView modelAndView=new ModelAndView();
            modelAndView.addObject("course",courseDao.getById(id));
            modelAndView.setViewName("edit");
            return modelAndView;
        }
    
        @PutMapping("/update")
        public String update(Course course){
            courseDao.update(course);
            return "redirect:/getAll";
        }
    
        @DeleteMapping("/delete/{id}")
        public String delete(@PathVariable int id){
            courseDao.delete(id);
            return "redirect:/getAll";
        }
    }
    HiddenHttpMethodFilter
  • 相关阅读:
    Python写的简陋版一对一聊天工具,全双工
    Python函数的循环调用
    Python多进程
    正则表达式
    Python-urllib学习记录
    Android学习笔记8:使用HttpClient接口实现网络通信
    Java抽象类和接口的区别(好长时间没看这种文章了)
    Android笔记:bitmap转换与处理相关工具类,Bitmap与DrawAble与byte[]与InputStream之间的转换
    first work
    Android学习笔记7:使用SQLite方式存储数据
  • 原文地址:https://www.cnblogs.com/shouyaya/p/13040505.html
Copyright © 2011-2022 走看看