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 中的Lock与RLock
    Python 自定义三方库
    Python 通过RSA实现license验证设备指纹与有效期
    Python 通过wmi获取Window服务器硬件信息
    Java List对象集合按对象属性分组、分组汇总、过滤等操作示例
    BrokenPipeError: [Errno 32] Broken pipe
    Python 通过dmidecode获取Linux服务器硬件信息
    Linux 解决E: Sub-process /usr/bin/dpkg returned an error code (1)错误
    Ubuntu 增加新用户并赋予root权限及免密的方法
    关于smali插桩
  • 原文地址:https://www.cnblogs.com/shouyaya/p/13040505.html
Copyright © 2011-2022 走看看