1.在SpringBoot使用 thymeleaf 模板引擎
小提示:静态资源文件都放在 resources 文件夹目录下,其中 templates 放置的是 html 文件,而 static 放置的是 js、css、img 等静态资源文件。目录结构如下:
1.1在pom.xml引入,会自动导入依赖
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-java8time</artifactId>
</dependency>
1.2关于模板引擎 thymeleaf 用法
1.2.1 在标签 html 上面加上 xmlns:th="http://www.thymeleaf.org" 才可以使用,th的语法
<html lang="en" xmlns:th="http://www.thymeleaf.org">
</html>
1.2.2 具体语法的使用
要链接相关的本地静态资源的时候:其中 static 表示静态资源可以直接省略不写
<link th:href="@{/css/bootstrap.min.css}" rel="stylesheet">
如果消息为空则不显示:
<p th:text="${msg}" th:if="${not #strings.isEmpty(msg)}"></P>
变量的显示
<a>[[${session.loginUser}]]</a>
2. SpringBoot扩展SpringMVC
2.1 在与运行文件 DemoApplication 文件同级的文件下面建立一个叫 config 的包,然后在 config 包里面建立一个 MyMvcConfig 的类:目录如下
2.2 里面具体的代码如下:使用工具 IDEA 的快捷键 ctrl+s 可以快速重写相关的类。
package com.example.demo.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
// 扩展 MVC
// 增加视图层
@Configuration // 这个注解用于定义配置类
public class MyMvcConfig implements WebMvcConfigurer {
// 试图 映射
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("index");
registry.addViewController("/index.html").setViewName("index");
registry.addViewController("/main.html").setViewName("dashboard");
}
// 注册bean 重写里面的拦截器方法 说明要拦截的东西
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new LoginHandlerInterceptor())
.addPathPatterns("/**") // 把所有的都拦截掉
.excludePathPatterns("/index.html",
"/", "/login","/css/**", "/js/**", "/img/**"); // 排除括号里面的
}
}
2.3 对登入进行拦截,防止在地址栏乱输入也能进入系统
相关代码如下:
// 重新上线 HandlerInterceptor 这个类
public class LoginHandlerInterceptor implements HandlerInterceptor {
// 重写这三个方法 HttpServletRequest HttpServletResponse 为 servlet 内置的
@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler) throws Exception {
// 1. 拿到用户请求的信息,return true 表示放行 retun false 表示拦截
// 2. 登入成功之后,应该有用户的 session (token) 就是用户的标志
Object loginUser = request.getSession().getAttribute("loginUser");
if(loginUser == null){
request.setAttribute("msg","请先登入");
// 将请求进行转发操作
request.getRequestDispatcher("/index.html").forward(request,resopnse);
return false;
}else{
// 否则进行放行
return true;
}
}
}
3. 关于登入
controller层的代码如下:
// 被@Controller标记的类实际上就是个SpringMVC Controller对象,它是一个控制器类
public class LoginController {
@RequestMapper("/login")
public String login(@RequestParam("username"), @RequestParam("password"),
Model model, HttpSession session){
// 具体的业务代码 其中 StringUtils 使用 th 模板引擎的工具类
if(StringUtils.isEmpty(username) && "123".equals(password)){
// 登入时随便把 loginUser 这个登入的标志也存入到 session里面
session.setAttribute("loginUser", username);
// 重定向到配置好的首页
return "redirect:/main.html";
}else{
// 如果 用户名为空 密码不是 123 的话就提示,并且停留在首页
model.addAttribite("msg", "用户名或者密码错误");
return "index";
}
}
}
前台的代码如下:
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<body>
<!-- 下面是一个表单的 要注意的是:一定要写 name="username" -->
<form th:action="@{/login}">
<input type="text" name="username" />
<input type="password" name="password" />
<button type="submit"></button>
</form>
</body>
</html>