• 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
  • 相关阅读:
    XUartPs_SetFifoThreshold
    函数指针 与 指针函数 的 区别
    zynq SPI 参数配置
    20199118《网络攻防实践》第三周作业
    maven项目将第三方jar包打入自己的jar中
    springboot项目简单的实现操作多数据库源
    WebSocket简单的应用
    怎么使用bat脚本更改本地受保护的hosts文件
    Spring Boot配置ssl证书启用HTTPS协议
    控制animation暂停:animation-play-state
  • 原文地址:https://www.cnblogs.com/shouyaya/p/13040505.html
Copyright © 2020-2023  润新知