springboot使用模板引擎
springboot使用模板引擎
制作人:全心全意
模板引擎的目的:动态页面伪装静态化提高搜索引擎收录,小的管理平台一般才会使用JSP。
Spring Boot提供了默认配置的模板引擎主要有已下几种:
Thymeleaf
FreeMarker
Velocity
Groovy
Mustache
默认的模板配置路径为:src/main/resources/templates
使用FreeMarker模板引擎渲染web视图
引入依赖包
<dependency> <!-- 引入FreeMarker的依赖包 --> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency>
freemarker的配置
新建application.yml文件(resources中)
spring: http: encoding: force: true ### 模板引擎编码为UTF-8 charset: UTF-8 freemarker: allow-request-override: false cache: false check-template-location: true charset: UTF-8 content-type: text/html;charset=utf-8 expose-request-attributes: false expose-session-attributes: false expose-spring-macro-helpers: false ## 模板文件结尾 suffix: .ftl ## 模板文件目录 template-loader-path: classpath:/templates
在templates中创建ftl文件
<!DOCTYPE HTML>
<html>
<head lang="cn">
<meta charset="utf-8" />
<title>freemarker测试页</title>
</head>
<body>
${name}
<#if sex=="1">
男
<#elseif sex=="2">
女
<else>
其他
</#if>
<#list ulist as u>
${u}
</#list>
</body>
</html>
使用比较符号注意:
用符号代替:gt、gte、lt、lte
用括号:<#!if(x>y)>
页面处理方法
@RequestMapping("/freemarker")
public String freemarker(Map<Object, Object> resultMap) {
resultMap.put("name", "quanxinquanyi");
resultMap.put("sex", "1");
List<Object> ulist = new ArrayList<Object>();
ulist.add("张三");
ulist.add("lisi");
resultMap.put("ulist", ulist);
return "freemarker";
}
使用Thymeleaf模板引擎渲染web视图
Thymeleaf:一款用于渲染XML/XHTML/HTML5内容的模板引擎,类似JSP,FreeMarker等,它也可以轻易的与spring mvc等web框架进行集成作为web应用的模板引擎。
引入依赖包
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
新建application.yml文件(resources中)
spring: thymeleaf: #prefix:指定模板所在目录 prefix: classpath:/templates/ #check-tempate-location:检查模板路径是否存在 check-tempate-location: true #cache:是否缓存,开发模式设置为false,避免修改模板需重启服务器,线上为true,可以提高性能 cache: true suffix: .html encoding: UTF-8 mode: HTML5
在templates中创建html文件
<!DOCTYPE html>
<!-- 注意添加 -->
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<span th:text="${tt}"></span>
</body>
</html>
页面处理方法
package com.zq.main.controller;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class MyThymeleafController {
@RequestMapping("/myThymeleaf")
public String myThymeleaf(HttpServletRequest request) {
request.setAttribute("tt", "123");
return "myThymeleaf";
}
}
循环和判断的使用:
页面处理方法
@Controller
public class MyThymeleafController {
@RequestMapping("/myThymeleaf")
public String myThymeleaf(Map<String, Object> result) {
ArrayList<String> str1 = new ArrayList<>();
str1.add("123");
str1.add("456");
result.put("str1", str1);
return "myThymeleaf";
}
}
页面:
<!DOCTYPE html>
<!-- 注意添加 -->
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<ul th:each="st:${str1}">
<li th:text="${st}"></li>
<li th:if="${st == '456'}">出现456</li>
</ul>
</body>
</html>
Spring boot整合JSP(war项目)
Spring boot原生不支持JSP,需要导入依赖
注意:使用JSP,项目类型一定要为war类型,不可为jar类型,否则无法找到页面
引入依赖包
<dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> </dependency>
配置application.yml文件
spring: mvc: view: prefix: /WEB-INF/jsp/ suffix: .jsp
创建测试接口
package com.zq.springboot01;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class IndexController {
@RequestMapping("/jsp")
public String jsp1() {
return "index";
}
}

浙公网安备 33010602011771号