spring boot系列02--Thymeleaf+Bootstrap构建页面

时间:2023-09-18 08:19:08

上一篇说了一下怎么构建spring boot 项目

接下来我们开始讲实际应用中需要用到的 先从页面说起

页面侧打算用Thymeleaf+Bootstrap来做

先共通模板页

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<body>
<!-- Header -->
<div th:fragment="header(title)">
<div>
<h2>Guaishushu</h2>
</div>
</div> <!-- Footer -->
<div th:fragment="footer" class="navbar-fixed-bottom">
<div class="container text-center">
Copyright © GuaiShuShu
</div> </div>
</body>
</html>

暂且定义两个共通 Header 和 Footer 之后还会再有 比如 menu 的

这里能说的就是th:fragment 开始说了 页面用 Thymeleaf+Bootstrap 来做

th就是Thymeleaf的缩写了 它用【th:】来告诉解析器这里是Thymeleaf的东西了

(关于Thymeleaf的东西这里打算是用哪儿写哪儿 不打算系统写 之后可能会系统写一个Thymeleaf的系列)

说一下代码里的两个 两个模板

th:fragment="header(title)   是表示 名叫 header 有参 的模板

th:fragment="footer" 这个就是 无参的了

接下来看看调用的地方 L21,L79

模板画面名(common)+模板名(header)+参数( 'login')

 <!DOCTYPE HTML>
<!-- thymeleaf 导入 -->
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>登录</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<script type="text/javascript" src="../static/js/jquery-3.2.1.min.js"></script>
<link rel="stylesheet" href="../static/css/bootstrap.min.css"
type="text/css"></link>
<script type="text/javascript">
$(function() {
})
</script>
<body>
<!-- common 的 定义好的 header模板 引用 参数 title -->
<!--
Bootstrap的容器Class使用
container :用于固定宽度并支持响应式布局的容器
container-fluid :用于 100% 宽度,占据全部视口(viewport)的容器。
-->
<div class="container" th:replace="common :: header('login') "></div>
<div class="container">
<!--Bootstrap
.row:行控制 一行有12列
.clo-md-4:列控制 表示 占 4列
.md-offfset-4:向右侧偏移4
-->
<div class="row">
<div class="col-md-4 col-md-offset-4">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Login</h3>
</div>
<div class="panel-body">
<form th:action="@{'/login'}" method="post" th:object="${userDto}">
<div class="form-group form-inline">
<label for="txtUserName" class="col-md-3 control-label"
style="text-align: right;">用户名</label>
<div class="col-md-9">
<input type="text" class="col-md-9 form-control" id="txtUserName"
placeholder="请输入用户名" th:field="*{userName}"
required="required" />
</div>
</div>
<div class="form-group form-inline" style="padding-top:45px">
<label for="txtUserPwd" class="col-sm-3 control-label"
style="text-align: right;">密码</label>
<div class="col-md-9">
<input type="password" class="col-sm-9 form-control" id="txtUserPwd"
placeholder="请输入密码" th:field="*{userPsw}" />
</div>
</div>
<div class="form-group">
<div class="col-md-offset-3 col-md-9">
<div class="checkbox">
<label> <input type="checkbox" />请记住我
</label>
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-3 col-md-5">
<button class="btn btn-primary btn-block" type="submit" name="action"
value="login">登录</button>
</div> </div>
<div class="alert alert-danger" th:if="${loginError}">
<strong>用户名或者用户密码不正确,请重新输入</strong>
</div>
</form>
</div>
</div>
</div>
</div>
</div> <!-- common 的 定义好的 fotter模板引用 无参 -->
<div class="container" th:replace="common :: footer"></div>
</body>
</head>
</html>

说一下thymeleaf使用的地方

L35 使用或者说定义了一个userDto对象

说使用是因为 这个对象你得重后台传给它否则报错

说定义 是因为 在当前from作用于域内  像L41那样去访问使用userDto这个对象内属性/方法

关于Bootstrap的部分 我基本都在代码的注释里写了

这里就不赘述了 看一下Controller

 package com.sts.springboot.controller;

 import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes; import com.sts.springboot.dto.UserDto; @Controller
public class LoginController { //初期化
@RequestMapping(value = "/login", method = RequestMethod.POST)
public String login(Model model) {
UserDto userDto = new UserDto();
model.addAttribute("userDto",userDto);
return "login";
} //登录
@RequestMapping(value = "/login",params="action=login", method = RequestMethod.POST)
public String login(@RequestParam(value = "userName") String userName,
@RequestParam(value = "userPsw") String userPsw,Model model,RedirectAttributes redirectAttributes ) {
UserDto userDto = new UserDto();
if(userName.equals(userPsw)) {
redirectAttributes.addFlashAttribute("userName",userName);
return "redirect:index";
} else {
model.addAttribute("loginError",true);
model.addAttribute("userDto",userDto);
return "login";
}
}
@RequestMapping("/index")
public String welcomeIndex(@ModelAttribute("userName") String userName,Model model) {
model.addAttribute("userName",userName);
return "index";
} }

简单说一下,先说参数部分

@RequestParam 使用来接收前台穿过来的参数 不用赘述 有一点 @RequestParam的value 是要和 页面上 th:field="*{userPsw}" 的保持一致的

Model model 这是 页面的对象返回时作为传参的载体使用

RedirectAttributes 是重定向传参用的 像L30那样 set进去 L39 接受 L40 set 到新页面的model里

然后

主要逻辑是 判断 用户名是否等于用户密码

是 重定向到 index 页面 显示

否 想页面 返回error 提示

贴一下index 的代码

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Insert title here</title>
</head>
<body>
<div class="container" th:replace="common :: header('login') "></div>
<div class="container">
欢迎,<span th:text="${userName}"></span>登录
</div>
<div class="container" th:replace="common :: footer"></div>
</body>
</html>

最后整体看一下效果

spring boot系列02--Thymeleaf+Bootstrap构建页面

GitHub:spring-boot-hello