0


Thymeleaf模版引擎初尝试

模版引擎虽然不能够实现代码与视图解耦,但是其适合于个人开发者使用,而且如果存在前后端项目中,前端大量请求后端时,模版引擎无疑也存在优势。

SpringBoot 整合步骤:

  1. 引入依赖
  2. 编写 yml 配置
  3. 编写 html 模版文件
  4. 编写 Controller 接口
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency>
spring:thymeleaf:enabled:true#开启thymeleaf视图解析encoding: utf-8#编码prefix: classpath:/templates/  #前缀cache:false#是否使用缓存mode: HTML  #严格的HTML语法模式suffix: .html  #后缀名
<!DOCTYPEhtml><!--标记 thymeleaf 语法--><htmllang="en"xmlns:th="http://www.thymeleaf.org"><head><metacharset="UTF-8"><title>Title</title></head><body><h1> Hello <spanth:text="${username}"></span></h1></body></html>
// 此处使用 @Controller注解 与 ModelAndView 进行视图选择、传参@ControllerpublicclassUser{@GetMapping("user")ModelAndViewget(){ModelAndView modelAndView =newModelAndView();
        modelAndView.setViewName("user");// 视图选择
        modelAndView.addObject("username","小明");// 传参return modelAndView;}}
标签: java spring 前端

本文转载自: https://blog.csdn.net/qq_35760825/article/details/128986086
版权归原作者 ThinkStu 所有, 如有侵权,请联系我们删除。

“Thymeleaf模版引擎初尝试”的评论:

还没有评论