![](https://img.haomeiwen.com/i14992118/3f1ee1cef2e34d05.png)
image.png
WebRestfulApplication
package com.duing;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class WebRestfulApplication {
//启动springboot入口类
public static void main(String[] args) {
SpringApplication.run(WebRestfulApplication.class, args);
}
}
Guest
import lombok.AllArgsConstructor;
import lombok.Data;
/**
* 实体类 代表嘉宾
*/
@Data @AllArgsConstructor
public class Guest {
private String name;
private String role;
}
GuestController
import com.duing.bean.Guest;
import com.duing.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;
/**
* 业务控制类 返回视图
*
* @RequestMapping("/guest")
* 放在类上面,代表类里面的方法是以此开头的
*/
@Controller
@RequestMapping("/guest")
public class GuestController {
@Autowired
private GuestService guestService;
// @RequestMapping(method = RequestMethod.GET)
@GetMapping
public String list(Model model){
List<Guest> guestList = guestService.list();
model.addAttribute("guestList",guestList);
return "list";
}
@GetMapping("/toAdd")
public String toAdd(){
return "add";
}
@PostMapping
public String add(Guest guest){
guestService.add(guest);
return "redirect:/guest";
}
/**
* 将/guest/toUpdate/{name}格式的url映射到此方法
* 其中的name属性值 通过注解 @PathVariable("name")映射到方法的属性中
* 其中的“name” 代表去url中查找的属性值
* 直译的意思就是 从路径中获取name的值
*
* @param model
* @param name
* @return
*/
@GetMapping("/toUpdate/{name}")
public String toUpdate(Model model,@PathVariable("name") String name){
Guest guest = guestService.get(name);
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";
}
}
GuestService
package com.duing.service;
import com.duing.bean.Guest;
import java.util.List;
public interface GuestService {
List<Guest> list();
void add(Guest guest);
void update(Guest guest);
Guest get(String name);
void delete(String name);
}
GuestServiceImpl
package com.duing.service;
import com.duing.bean.Guest;
import com.duing.dao.GuestDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class GuestServiceImpl implements GuestService {
@Autowired
private GuestDao guestDao;
@Override
public List<Guest> list() {
return guestDao.list();
}
@Override
public void add(Guest guest) {
guestDao.add(guest);
}
@Override
public void update(Guest guest) {
guestDao.update(guest);
}
@Override
public Guest get(String name) {
return guestDao.get(name);
}
@Override
public void delete(String name) {
guestDao.delete(name);
}
}
GuestDao
package com.duing.dao;
import com.duing.bean.Guest;
import org.springframework.stereotype.Repository;
import java.util.ArrayList;
import java.util.List;
@Repository
public class GuestDao {
static List<Guest> guestList = new ArrayList<>();
static {
guestList.add(new Guest("黄晓明","店长"));
guestList.add(new Guest("秦海璐","财务"));
guestList.add(new Guest("林述巍","总厨"));
guestList.add(new Guest("王俊凯","经理"));
guestList.add(new Guest("杨紫","前台"));
}
public List<Guest> list(){
return guestList;
}
public void add(Guest guest){
guestList.add(guest);
}
public void update(Guest newGuest){
Guest oldGuest = get(newGuest.getName());
oldGuest.setRole(newGuest.getRole());
}
public Guest get(String name){
for(Guest guest:guestList){
if(guest.getName().equals(name)){
return guest;
}
}
return new Guest("","");
}
public void delete(String name){
guestList.remove(get(name));
}
}
前端页面
add.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http:www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>添加嘉宾</title>
<link rel="stylesheet" href="bootstrap.css" th:href="@{/bootstrap.css}">
</head>
<body class="container">
<h3>添加嘉宾</h3>
<br>
<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" class="form-control">
</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" class="form-control">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<input type="submit" value="提交" class="btn btn-info">
</div>
</div>
</form>
</body>
</html>
list.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}">
<script type="text/javascript" th:src="@{/webjars/jquery/3.4.1/jquery.js}"></script>
</head>
<body class="container">
<h2>中餐厅嘉宾如下</h2>
<br>
<table class="table table-hover">
<thead>
<tr>
<th>名字</th>
<th>角色</th>
</tr>
</thead>
<tbody>
<tr th:each="guest:${guestList}">
<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>
update.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>
<br>
<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">
<input type="text" id="name" name="name"
th:value="*{name}" class="form-control" readonly>
</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-10">
<input type="submit" value="提交" class="btn btn-info">
</div>
</div>
</form>
</body>
</html>
网友评论