SpringBoot----RESTful案例

package com.uncle.controller;

import com.uncle.bean.Guest;
import com.uncle.service.GuestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @program: SpringBoot-study
 * @description: 嘉宾的控制层
 * @author: 步尔斯特
 * @create: 2021-03-03 14:22
 **/
@Controller
@RequestMapping("/guest")
public class GuestController {

    @Autowired
    private GuestService guestService;

//    @RequestMapping(method = RequestMethod.GET)
    @GetMapping
    public String list(Model model) {
        List<Guest> list = guestService.list();
        model.addAttribute("list", list);
        return "list";
    }

    @GetMapping("/toAdd")
    public String toAdd() {
        return "add";
    }

    @PostMapping
    public String add(Guest guest) {
        guestService.add(guest);
        return "redirect:/guest";
    }

    @GetMapping("/toUpdate/{name}")
    public String toUpdate(Model model,@PathVariable("name") String name) {
        Guest guest = guestService.getName(name);
        System.out.println(guest);
        model.addAttribute("guest", guest);
        return "update";
    }

    @PutMapping
    public String update(Guest guest) {
        guestService.update(guest);
        return "redirect:/guest";
    }

    @DeleteMapping("{name}")
    public String delete(@PathVariable("name") String name) {
        guestService.delete(name);
        return "redirect:/guest";
    }
}

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>展示页面</title>
    <link rel="stylesheet" th:href="@{/bootstrap.css}">
    <script type="text/javascript" th:src="@{/webjars/jquery/3.6.0/jquery.js}"></script>
</head>
<body class="container">
<h3>中餐厅嘉宾如下</h3>
<br/>

<table class="table table-hover">
    <thead>
        <tr>
            <th>名字</th>
            <th>角色</th>
        </tr>
    </thead>
    <tbody>
        <tr th:each="guest:${list}">
            <td th:text="${guest.name}">name</td>
            <td th:text="${guest.role}">role</td>
            <td class="col-sm-2">
                <a th:href="@{/guest/toUpdate/}+${guest.name}">编辑</a>
            </td>
            <td class="col-sm-2">
<!--                <a th:href="@{/guest/delete(name=${guest.name})}">删除</a>-->
                <button th:attr="del_url=@{/guest/}+${guest.name}" name="del_button">删除</button>
            </td>
        </tr>
    </tbody>
</table>
<div class="form-group">
    <div class="col-sm-2 control-label">
        <a href="/guest/toAdd" th:href="@{/guest/toAdd}" class="btn btn-info">添加</a>
    </div>
</div>
<!--删除按钮借助的表单-->
<form method="post" id="del_form">
    <input type="hidden" name="_method" value="delete">
</form>
<script>
    $(function () {
        $("button[name='del_button']").click(function () {
            $("#del_form").prop("action",$(this).attr("del_url")).submit();
        });
    });
</script>
</body>
</html>
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" th:href="@{/bootstrap.css}">
</head>
<body class="container">
<h3>
    添加嘉宾
</h3>

<form th:action="@{/guest}" method="post" class="form-horizontal">
    <div class="form-group">
        <label class="col-sm-2 control-label">名字</label>
        <div class="col-sm-5">
            <input type="text" id="name" name="name">
        </div>
    </div>
    <div class="form-group">
        <label class="col-sm-2 control-label">角色</label>
        <div class="col-sm-5">
            <input type="text" id="role" name="role">
        </div>
    </div>
    <div class="form-group">
        <div class="col-sm-offset-2 col-sm-5">
            <input type="submit" value="确认添加" class="btn btn-info">
        </div>
    </div>
</form>
</body>
</html>
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>修改角色</title>
    <link rel="stylesheet" th:href="@{/bootstrap.css}">
</head>
<body class="container">
<h3>
    修改角色
</h3>
<form th:action="@{/guest}" th:object="${guest}" method="post" class="form-horizontal">

    <input type="hidden" name="_method" value="put">

    <div class="form-group">
        <label class="col-sm-2 control-label" >名字</label>
        <div class="col-sm-5">

            <div class="col-sm-5">
                <input type="text" id="name" name="name"
                       th:value="*{name}" class="form-control" readonly>
            </div>

        </div>
    </div>
    <div class="form-group">
        <label class="col-sm-2 control-label" >角色</label>
            <div class="col-sm-5">
                <input type="text" id="role" name="role"
                       th:value="*{role}" class="form-control">
            </div>
    </div>
    <div class="form-group">
        <div class="col-sm-offset-2 col-sm-5">
            <input type="submit" value="确认修改" class="btn btn-info">
        </div>
    </div>
</form>

</body>
</html>

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值