2、SpringBoot整合Freemarker
1、在pom.xml文件中添加如下
<!-- freemarker启动器的坐标 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
2、编写视图
SpringBoot要求模板形式的视图层技术的文件必须放到src/main/resources/目录下的一个名称必须为templates的目录**(src/main/resources/templates)**
<html>
<head>
<title>freemarker</title>
<meta charset="utf-8"></meta>
</head>
<body>
<table border="1">
<tr>
<th>id</th>
<th>name</th>
<th>age</th>
</tr>
<#list list as user>
<tr>
<th>${user.id}</th>
<th>${user.name}</th>
<th>${user.age}</th>
</tr>
</#list>
</table>
</body>
</html>
配置文件
spring.freemarker.suffix=.ftl
spring.freemarker.template-loader-path=classpath:/templates/
controller和实体类参考整合jsp技术
3、SpringBoot整合Thymeleaf(重点)
1、在pom.xml文件中添加
<!-- thymeleaf启动器的坐标 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
2、创建存放视目录
位置:src/main/resources/templates
templates:该目录是安全的,意味着该目录下的内容是不允许外界直接访问的。
3、Thymeleaf的基本使用
3.1、Thymeleaf的特点
Thymeleaf是通过特定语法对html的标记做渲染。
编写Controller
/**
* Thymeleaf的基本使用
* @author 24957
*
*/
@Controller
public class ThymeleafController {
@RequestMapping("/show")
public String thymeleaf(Model model) {
model.addAttribute("msg", "pj的Thymeleaf");
return "index";
}
}
编写HTML
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Thymeleaf</title>
</head>
<body>
<span th:text="Hello " ></span>
<span th:text="${msg}"></span>
</body>
</html>
编写启动类
/**
* 启动类
* @author 24957
*
*/
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
4、Thymeleaf的语法详解
4.1变量输出与字符串操作
th:text(在页面中输出值)
th:value(可以将一个值放入input标签的value中)
Thymeleaf内置对象
调用内置对象一定要用#
大部分内置对象一般都以s结尾,例:strings、numbers、dates……
${#strings.isEmpty(key)}(判断字符串是否为空,为空返回true,否则返回false)
${#strings.contains(msg,‘T’)}(判断字符串是否包含指定的子串,包含返回true,不包含返回false)
${#strings.startsWith(msg,‘T’)}(判读字符串是否以子串开头)
${#strings.endsWith(msg,‘T’)}(判读字符串是否以子串结尾)
${#strings.length(msg)}(返回字符串长度)
${#strings.indexOf(msg,‘T’)}(查找子串位置,并返回子串坐标,如果没找到返回-1)
${#strings.substring(msg,9)}(字符串截取从下标为9的开始到结束)
${#strings.substring(msg,9,12)}(字符串截取,从下标为9的开始到下标为12的结束)
${#strings.toUpperCase(msg)}(转大写)
${#strings.toLowerCase(msg)}(转小写 )