第一部分:导包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
第二部分:编写简单的页面html页面
(1)功能选择界面Choice.html
http://localhost:8080/Choice
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>按钮示例</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-image: url("http://www.deskcar.com/desktop/else/2009331235330/37.jpg");
background-size: cover;
}
button {
font-size: 16px;
padding: 10px 20px;
margin: 0 10px;
border: none;
background-color: #f0f0f0;
cursor: pointer;
}
</style>
</head>
<body>
<button th:onclick="'location.href=\'/login\''">按钮1</button>
<button th:onclick="'location.href=\'/pages1\''">按钮2</button>
<button th:onclick="'location.href=\'/pages2\''">按钮3</button>
</body>
</html>
(2)三个选项
①登录(按钮1)login.html
http://localhost:8080/login
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
/* 设置整个页面的样式,使其具有居中效果 */
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh; /* 使 body 占满整个视口高度 */
margin: 0;
padding: 0;
background-color: #f0f0f0; /* 或者其他背景颜色 */
}
/* 设置表单的样式 */
form {
text-align: center; /* 使得表单内的文本居中 */
}
</style>
</head>
<body>
<form action="#" method="post">
<p>账号:<input type="text" name="username" value="admin"/></p>
<p>密码:<input type="text" name="password" value="123456"/></p>
<p><input type="submit" value="登录"/></p>
</form>
</body>
</html>
②按钮2(页面1)pages1.html
http://localhost:8080/pages1
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Quick Start - Layui</title>
<link href="//cdnjs.cloudflare.com/ajax/libs/layui/2.9.14/css/layui.css" rel="stylesheet">
</head>
<body>
<!-- HTML Content -->
<script src="//cdnjs.cloudflare.com/ajax/libs/layui/2.9.14/layui.js"></script>
<script>
// Usage
layui.use(function(){
var layer = layui.layer;
// Welcome
layer.msg('hello,pages1', {icon: 6});
});
</script>
</body>
</html>
③按钮3(页面2)pages2.html
http://localhost:8080/pages2
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Quick Start - Layui</title>
<link href="//cdnjs.cloudflare.com/ajax/libs/layui/2.9.14/css/layui.css" rel="stylesheet">
</head>
<body>
<!-- HTML Content -->
<script src="//cdnjs.cloudflare.com/ajax/libs/layui/2.9.14/layui.js"></script>
<script>
// Usage
layui.use(function(){
var layer = layui.layer;
// Welcome
layer.msg('hello,page2', {icon: 6});
});
</script>
</body>
</html>
第三部分:Controller层(不要用@Restcontroller,要用@Controller)
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller//注意点:这里别用@RestController,用了你的请求会给你返回个json的格式
public class PageController {
@RequestMapping({"/Choice"})
public String Choice() {
return "/Choice";
}
@RequestMapping({"/login"})
public String login() {
return "/login";
}
@RequestMapping({"/pages1"})
public String pages1() {
return "/pages1";
}
@RequestMapping({"/pages2"})
public String pages2() {
return "/pages2";
}
}
第四部分:源文件获取
通过百度网盘分享的文件:springboot配置thymeleaf实现对在templates下...
链接:https://pan.baidu.com/s/1MQePjCvsjtOb4Q5gglun1g?pwd=9u7h
提取码:9u7h
--来自百度网盘超级会员V5的分享
备注:
Html配件网站:Layui - 极简模块化前端 UI 组件库(官方文档)
好啦,希望能够帮助到大家!