springboot配置thymeleaf实现对在templates下html页面的访问

第一部分:导包

<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 组件库(官方文档)

好啦,希望能够帮助到大家!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

还不秃顶的计科生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值