thymeleaf 使用

53 阅读1分钟

1、引入依赖

net.sourceforge.nekohtml nekohtml 1.9.22 org.springframework.boot spring-boot-starter-thymeleaf ognl ognl 3.2.1 2、配置文件 .yml

spring: thymeleaf: encoding: UTF-8 cache: false # 检查模板是否存在,然后再呈现 check-template-location: true prefix: classpath:/word/ suffix: .html mode: LEGACYHTML5 servlet: content-type: text/html 3代码

private final static TemplateEngine engine = new TemplateEngine(); @Override public void AISave(String materialCarBatchId, String compilationTemplateId, HttpServletResponse response) { Map<String, Object> dataMap = new HashMap<>(); dataMap.put("title", "测试"); Context context = new Context(); context.setVariables(dataMap); String html = engine.process("

", context);

   }

注意

如果引入下面的则只能根据2配置文件查找到项目内的模板

@Autowired private SpringTemplateEngine templateEngine; Context context = new Context(Locale.getDefault(), dataMap); String html = templateEngine.process("test.html", context);

附上 test.html 代码

Title

${title}

ID Name Age