SpringBoot中thymeleaf的使用
程序员文章站
2024-02-29 17:44:16
...
SpringBoot中thymeleaf的使用
一、导入依赖(pom.xml)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
二、添加配置(application.yml)
spring:
thymeleaf:
enabled: true #开启thymeleaf视图解析
encoding: utf-8 #编码
prefix: classpath:/templates/html/ #前缀
cache: false #是否使用缓存
mode: HTML #严格的HTML语法模式
suffix: .html #后缀名
三、将模板文件放入资源目录下
四、关于html页面内语法的使用
<!--注意:引入thymeleaf的名称空间-->
<html lang="en" xmlns:th="http://www.thymeleaf.org">
1.对象值:(写在标签上)
<span class="" th:text="${base.name}"></span>
2.时间的格式化:
<span class="" th:text="${#dates.format(base.date,'yyyy-MM-dd')}"></span>
3.数组的遍历取值:
<table border="0" cellspacing="0">
<tr th:each="medical : ${medicals}">
<td th:text="${medical.a}"></td>
<td th:text="${medical.b}"></td>
</tr>
</table>
4.复选框的取值判断
<input style="display: inline-block;" disabled="disabled" type="checkbox"
th:checked="!(${baseinfo.sex} == 0)">男
5.判断
<div th:if="${medicals} == null"> </div>
6.在当前页面引入其它html页面
<div th:include="FemaleMaritalHistory::marital_history"></div>
FemaleMaritalHistory:文件名
marital_history:别名
在被引入的html的页面需在声明
<body>
<div th:fragment="marital_history">
xxxxxxxxx
</div>
</body>
五、html工具类
/**
* 模版加载
* @param modelFile 模版文件
* @param newFile 新文件
* @param newFilePath 文件路径
* @param context
* @return
* @throws HtmlException
*/
public static String loadModule(String modelFile, String newFile, String newFilePath, Context context) throws HtmlException {
ClassLoaderTemplateResolver resolver = new ClassLoaderTemplateResolver();
resolver.setTemplateMode("HTML5");
resolver.setPrefix("templates/html"+ File.separator);
resolver.setSuffix(".html");
resolver.setCharacterEncoding("UTF-8");
TemplateEngine templateEngine = new TemplateEngine();
templateEngine.setTemplateResolver(resolver);
FileWriter fileWriter = null;
//是否存在文件夹
boolean hasDir = true;
try{
//文件夹判断
hasDir = FileUtils.isDirExists(new File(newFilePath));
String path = newFilePath + File.separator+ newFile;
fileWriter = new FileWriter(path);
templateEngine.process(modelFile,context,fileWriter);
fileWriter.flush();
return path;
}catch (Exception e){
if(!hasDir){
FileUtils.deleteAll(new File(newFilePath));
}
throw new HtmlException(modelFile+"模版文件生成失败!",e);
}finally {
if(null != fileWriter){
try {
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
六、使用生成html
Context context = new Context();
CycleInfo cycleInfo = new CycleInfo();
cycleInfo.setDate("2020-01-23");
context.setVariable("cycleInfo",cycleInfo);
loadModule("CycleMedicalHistory.html","test_CycleMedicalHistory.html","_html",context);
第一个参数:模板文件名
第二个参数:生成的html名
第三个参数:文件存放地址
第四个参数:数据内容
七、文件地址(拓展内容)
1.静态资源文件访问声明
import com.dfinfo.sivf.utils.GetPatientLocation;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebMvcConfiguration implements WebMvcConfigurer {
/**
* 静态资源配置
*
* @param registry
*/
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
try {
registry.addResourceHandler("/history/**").addResourceLocations("file:"+ GetLocation.location());
} catch (Exception ex) {
}
}
}
2.文件地址路径获取
import java.io.File;
public class GetPatientLocation {
/**
* 获取病例的存放路径
* @return
*/
public static String getPatientLocation(){
String path = System.getProperty("user.dir").replace("bin","webapps");
String location = path + File.separator +"_html"+ File.separator ;
return location;
}
}
Linux环境下是Tomcat的webapps的_html文件夹;
windows环境下是项目所在文件夹下;