SpringBoot+SpringMVC+Mybatis+tyhmeleaf+LayUI模板引擎的编辑和增加功能
时间: 2024-01-17 14:02:42 浏览: 169
SpringBoot集成Mybatis以及thymeleaf模板Demo
SpringBoot+SpringMVC+Mybatis+Thymeleaf+LayUI模板引擎可以用于开发Web应用程序,实现编辑和增加功能。下面是一个示例代码:
1. 编辑功能:
```java
@Controller
@RequestMapping("/edit")
public class EditController {
@Autowired
private UserService userService;
@GetMapping("/{id}")
public String editUser(@PathVariable("id") int id, Model model) {
User user = userService.getUserById(id);
model.addAttribute("user", user);
return "edit";
}
@PostMapping("/{id}")
public String saveUser(@PathVariable("id") int id, @ModelAttribute User user) {
userService.updateUser(user);
return "redirect:/user/" + id;
}
}
```
2. 增加功能:
```java
@Controller
@RequestMapping("/add")
public class AddController {
@Autowired
private UserService userService;
@GetMapping
public String addUser(Model model) {
model.addAttribute("user", new User());
return "add";
}
@PostMapping
public String saveUser(@ModelAttribute User user) {
userService.addUser(user);
return "redirect:/user";
}
}
```
在Thymeleaf模板中,可以使用LayUI模板引擎来实现页面的美化和交互效果。例如,编辑页面的模板代码如下:
```html
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Edit User</title>
<link rel="stylesheet" th:href="@{/layui/css/layui.css}">
</head>
<body>
<form class="layui-form" th:object="${user}" th:action="@{/edit/{id}(id=${user.id})}" method="post">
<div class="layui-form-item">
<label class="layui-form-label">Name</label>
<div class="layui-input-block">
<input type="text" th:field="*{name}" class="layui-input">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">Age</label>
<div class="layui-input-block">
<input type="text" th:field="*{age}" class="layui-input">
</div>
</div>
<div class="layui-form-item">
<div class="layui-input-block">
<button class="layui-btn" lay-submit lay-filter="formDemo">Save</button>
</div>
</div>
</form>
<script src="/layui/layui.js"></script>
</body>
</html>
```
在上述代码中,使用了Thymeleaf的表达式语法来绑定表单数据和提交地址。同时,引入了LayUI的样式和脚本文件,实现了表单的美化和提交按钮的交互效果。
阅读全文