• 17_springboot Restful风格


    1.listStudent.jsp 把方法编程RESTFUL的

    注意各个功能的情况

    <%@ page language="java" contentType="text/html; charset=UTF-8"
             pageEncoding="UTF-8"%>
    
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
    
    <script src="https://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
    
    
        <script type="text/javascript">
            /*将post method 改变为delete*/
            $(function(){
                $(".delete").click(function(){
                    var href=$(this).attr("href");
                    $("#formdelete").attr("action",href).submit();
                    return false;
                })
            })
        </script>
    
    <div align="center">
    
    </div>
    
    <div style="500px;margin:20px auto;text-align: center">
        <table align='center' border='1' cellspacing='0'>
            <tr>
                <td>stuId</td>
                <td>name</td>
                <td>sex</td>
                <td>编辑</td>
                <td>删除</td>
            </tr>
            <c:forEach items="${page.list}" var="s" varStatus="abc">
                <tr>
                    <td>${s.stuId}</td>
                    <td>${s.name}</td>
                    <td>${s.sex}</td>
                    <td><a href="listStudents/${s.stuId}">编辑</a></td>
                    <td><a class="delete" href="listStudents/${s.stuId}">删除</a></td>
                </tr>
            </c:forEach>
    
        </table>
        <br>
        <div>
            <a href="?start=1">[首  页]</a>
            <a href="?start=${page.pageNum-1}">[上一页]</a>
            <a href="?start=${page.pageNum+1}">[下一页]</a>
            <a href="?start=${page.pages}">[末  页]</a>
        </div>
        <br>
        <form action="listStudents" method="post">
    
            name: <input name="name"> <br>
            sex:  <input name="sex"><br>
            <button type="submit">提交</button>
    
        </form>
        <form id="formdelete" action="" method="POST" >
            <input type="hidden" name="_method" value="DELETE">
        </form>
    </div>
    

    2.editStudent.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
             pageEncoding="UTF-8" isELIgnored="false"%>
    
    <div style="margin:0px auto; 500px">
    
        <form action="/listStudents/${s.stuId}" method="post">
            <input type="hidden" name="_method", value="PUT">
            name: <input name="name" value="${s.name}"> <br>
            sex:  <input name="sex" value="${s.sex}"><br>
    
            <input name="stuId" type="hidden" value="${s.stuId}">
            <button type="submit">提交</button>
    
        </form>
    </div>
    

    3.StudentController

    @Controller
    public class StudentController {
        @Autowired
        StudentMapper studentMapper;
    
        @RequestMapping("/listStudents")
        public String listStudent(Model m, @RequestParam(value = "start", defaultValue = "0") int start,
                                  @RequestParam(value = "size", defaultValue = "5") int size)throws Exception{
            PageHelper.startPage(start, size, "stuid desc");
            List<Student> stu = studentMapper.findAll();
            PageInfo<Student> page = new PageInfo<>(stu);
            m.addAttribute("page",page);
            return "listStudent";
        }
    
        @PostMapping("/listStudents")
        public String addStudent(Student s) throws Exception{
            studentMapper.save(s);
            return "redirect:/listStudents";
        }
    
        @DeleteMapping("/listStudents/{stuId}")
        public String deleteStudent(Student s) throws Exception {
            studentMapper.delete(s.getStuId());
            return "redirect:/listStudents";
        }
        @PutMapping("/listStudents/{stuId}")
        public String updateStudent(Student s) throws Exception {
            studentMapper.update(s);
            return "redirect:/listStudents";
        }
    
        @GetMapping("/listStudents/{stuId}")
        public String editStudent(@PathVariable("stuId") int stuId,Model m) throws Exception {
            Student s= studentMapper.get(stuId);
            m.addAttribute("s", s);
            return "editStudent";
        }
    }
    

    4.配置文件application.properties增加

    #使用restful支持
    spring.mvc.hiddenmethod.filter.enabled=true
    

    5.运行http://127.0.0.1:8080/listStudents,成功进入

  • 相关阅读:
    Java:多线程<一>
    Java:Exception
    Java: 内部类
    Ubuntu安装jdk
    ubuntu搜狗拼音安装
    录音-树莓派USB摄像头话筒
    leetcode 最小栈
    leetcode 编辑距离 动态规划
    leetcode 最小覆盖字串
    leetcode 最长上升子序列 动态规划
  • 原文地址:https://www.cnblogs.com/NaoDaiYouDianDa/p/13992482.html
Copyright © 2020-2023  润新知