Thymeleaf模板

Thymeleaf就是html页面

导入pom依赖

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-thymeleaf</artifactId>
  4. </dependency>

Spring Boot官方文档建议在开发时将缓 存关闭,那就在application.yml文件中加入:

正式环境还是要将缓存开启的

实体类

  1. User
  1. package com.javaxl.springboot01.entity;
  2.  
  3. import lombok.Data;
  4.  
  5. /**
  6. * @author XuFanQi
  7. * @site
  8. * @company
  9. * @create 2019-11-26 15:37
  10. */
  11. @Data
  12. public class User {
  13. private Integer uid;
  14. private String uname;
  15. private String pwd;
  16.  
  17. public User(Integer uid, String uname, String pwd) {
  18. this.uid = uid;
  19. this.uname = uname;
  20. this.pwd = pwd;
  21. }
  22.  
  23. public User() {
  24. }
  25. }
  1. Controller
  1. userController
  1. package com.javaxl.springboot01.controller;
  2.  
  3. import com.javaxl.springboot01.entity.User;
  4. import org.springframework.stereotype.Controller;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6.  
  7. import javax.servlet.http.HttpServletRequest;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10.  
  11. /**
  12. * @author XuFanQi
  13. * @site
  14. * @company
  15. * @create 2019-11-26 15:39
  16. */
  17. @Controller
  18. @RequestMapping("/thymeleaf")
  19. public class userController {
  20.  
  21. @RequestMapping("/list")
  22. public String hello(HttpServletRequest request){
  23. /**
  24. * 1.获取单个值
  25. * 2.能够在html页面竞争遍历展示
  26. * 3.如何在HTML页面转义代码块
  27. */
  28. request.setAttribute("msg","传输单个字符串! ! !");
  29. List<User> userList = new ArrayList<>();
  30. userList.add(new User(1,"zs","123456"));
  31. userList.add(new User(2,"ls","1234567"));
  32. userList.add(new User(3,"ww","1234568"));
  33. request.setAttribute("userList",userList);
  34. request.setAttribute("htmlStr","<span style='color:red'>转义html代码块</span>");
  35.  
  36. return "list";
  37. }
  38. }

list.html页面

(注意:

  1. <html xmlns:th="http://www.thymeleaf.org">

  1. <!DOCTYPE html>
  2. <html xmlns:th="http://www.thymeleaf.org">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>thymeleaf模板介绍</title>
  6. </head>
  7. <body>
  8. <div th:text = "${msg}"></div>
  9. <table width="60%" border="1">
  10. <tr>
  11. <td>ID</td>
  12. <td>Uname</td>
  13. <td>Pwd</td>
  14. </tr>
  15. <tr th:each="u : ${userList}">
  16. <td th:text="${u.uid}"></td>
  17. <td th:text="${u.uname}"></td>
  18. <td th:text="${u.pwd}"></td>
  19. </tr>
  20. </table>
  21. <div th:utext="${htmlStr}"></div>
  22.  
  23. </body>
  24. </html>

浏览器访问

Freemarker模板

首先导入pom依赖

  1. <dependency>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-freemarker</artifactId>
  4. </dependency>
  5.  
  6. <!--可以不加,但是做项目的时候可能会用-->
  7. <resources>
  8. <!--解决mybatis-generator-maven-plugin运行时没有将XxxMapper.xml文件放入target文件夹的问题-->
  9. <resource>
  10. <directory>src/main/java</directory>
  11. <includes>
  12. <include>**/*.xml</include>
  13. </includes>
  14. </resource>
  15. <!--freemarker模板也读取需要注释标红地方-->
  16. <resource>
  17. <directory>src/main/resources</directory>
  18. <includes>
  19. <!--<include>*.properties</include>-->
  20. <!--<include>*.xml</include>-->
  21. <!--<include>*.yml</include>-->
  22. </includes>
  23. </resource>
  24. </resources>

配置application.yml

  1. server:
  2. servlet:
  3. context-path: /spr
  4. port: 80
  5.  
  6. user:
  7. uname: zs
  8. pwd: 123456
  9. age: 18
  10. sex: "男"
  11. addr: "北京"
  12. spring:
  13. thymeleaf:
  14. cache: false
  15. freemarker:
  16. # 设置模板后缀名
  17. suffix: .ftl
  18. # 设置文档类型
  19. content-type: text/html
  20. # 设置页面编码格式
  21. charset: UTF-8
  22. # 设置页面缓存
  23. cache: false
  24. # 设置ftl文件路径,默认是/templates,为演示效果添加role
  25. template-loader-path: classpath:/templates/freemarker
  26. mvc:
  27. static-path-pattern: /static/**

配置freemarker.ftl

前台页面

list.ftl

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. </head>
  7. <body>
  8. <h2>取值</h2>
  9. <h3>提供默认值</h3>
  10. welcome 【${name!'未知'}】 to freemarker!
  11.  
  12. <h3>exists用在逻辑判断</h3>
  13. <#if name?exists>
  14. ${name}
  15. </#if>
  16.  
  17. <h2>条件</h2>
  18. <#if sex=='girl'>

  19. <#elseif sex=='boy'>

  20. <#else>
  21. 保密
  22. </#if>
  23.  
  24. <h2>循环</h2>
  25. <table border="1px" width="600px">
  26. <thead>
  27. <tr>
  28. <td>ID</td>
  29. <td>角色名</td>
  30. <td>描述</td>
  31. </tr>
  32. </thead>
  33. <tbody>
  34. <#list roles as role>
  35. <tr>
  36. <td>${role.rid}</td>
  37. <td>${role.roleName}</td>
  38. <td>${role.desc}</td>
  39. </tr>
  40. </#list>
  41. </tbody>
  42. </table>
  43.  
  44. <h2>include</h2>
  45. <#include 'foot.ftl'>
  46.  
  47. <h2>局部变量(assign)/全局变量(global)</h2>
  48. <#assign ctx1>
  49. ${springMacroRequestContext.contextPath}
  50. </#assign>
  51.  
  52. <#global ctx2>
  53. ${springMacroRequestContext.contextPath}
  54. </#global>
  55.  
  56. ss
  57. ${ctx1}和${ctx2}
  58. ss
  59.  
  60. </body>
  61. </html>

foot.ftl

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. </head>
  7. <body>
  8. 版权
  9. </body>
  10. </html>

实体类

  1. Role
  1. package com.javaxl.springboot01.entity;
  2.  
  3. import lombok.Data;
  4.  
  5. /**
  6. * @author XuFanQi
  7. * @site
  8. * @company
  9. * @create 2019-11-26 16:53
  10. */
  11. @Data
  12. public class Role {
  13. private Integer rid;
  14. private String roleName;
  15. private String desc;
  16.  
  17. public Role(Integer rid, String roleName, String desc) {
  18. this.rid = rid;
  19. this.roleName = roleName;
  20. this.desc = desc;
  21. }
  22.  
  23. public Role() {
  24. }
  25.  
  26. // public Integer getRid() {
  27. // return rid;
  28. // }
  29. //
  30. // public void setRid(Integer rid) {
  31. // this.rid = rid;
  32. // }
  33. //
  34. // public String getRoleName() {
  35. // return roleName;
  36. // }
  37. //
  38. // public void setRoleName(String roleName) {
  39. // this.roleName = roleName;
  40. // }
  41. //
  42. // public String getDesc() {
  43. // return desc;
  44. // }
  45. //
  46. // public void setDesc(String desc) {
  47. // this.desc = desc;
  48. // }
  49. }
  1. Controller
  1. RoleController
  1. package com.javaxl.springboot01.controller;
  2.  
  3. import com.javaxl.springboot01.entity.Role;
  4. import org.springframework.stereotype.Controller;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import org.springframework.web.servlet.ModelAndView;
  7.  
  8. import java.util.ArrayList;
  9. import java.util.List;
  10.  
  11. /**
  12. * @author XuFanQi
  13. * @site
  14. * @company
  15. * @create 2019-11-26 16:49
  16. */
  17. @Controller
  18. @RequestMapping("/freemarker")
  19. public class RoleController {
  20. @RequestMapping("/role/list")
  21. public ModelAndView roleList(){
  22. ModelAndView mav = new ModelAndView();
  23. mav.setViewName("/list");
  24.  
  25. mav.addObject("name",null);
  26. mav.addObject("sex","gay");
  27. List list = new ArrayList();
  28. list.add(new Role(1,"老师","教书育人"));
  29. list.add(new Role(2,"学生","知识改变命运"));
  30. mav.addObject("roles",list);
  31.  
  32. return mav;
  33. }
  34.  
  35. @RequestMapping("toLogin")
  36. public String toLogin(){
  37. return "login";
  38. }
  39. }

浏览器访问

springboot模板(Freemarker与Thymeleaf)的更多相关文章

  1. springboot之freemarker 和thymeleaf模板web开发

    Spring Boot 推荐使用Thymeleaf.FreeMarker.Velocity.Groovy.Mustache等模板引擎.不建议使用JSP. 一.Spring Boot 中使用Thymel ...

  2. SpringBoot集成freemarker和thymeleaf模板

    1.在MAVEN工程POM.XML中引入依赖架包 <!-- 引入 freemarker 模板依赖 --> <dependency> <groupId>org.spr ...

  3. SpringBoot学习笔记(4)----SpringBoot中freemarker、thymeleaf的使用

    1. freemarker引擎的使用 如果你使用的是idea或者eclipse中安装了sts插件,那么在新建项目时就可以直接指定试图模板 如图: 勾选freeMarker,此时springboot项目 ...

  4. SpringBoot集成Freemarker与Thymeleaf

    一:概括 pom.xml添加依赖 配置application.yml HTML页面使用表达式 二:Freemarker模板引擎 1.添加依赖 <!-- ftl模板引擎 --> <de ...

  5. spring boot ----> 常用模板freemarker和thymeleaf

    ===========================freemarker=================================== freemarker 官网:https://freem ...

  6. springboot配置server相关配置&整合模板引擎Freemarker、thymeleaf&thymeleaf基本用法&thymeleaf 获取项目路径 contextPath 与取session中信息

    1.Springboot配置server相关配置(包括默认tomcat的相关配置) 下面的配置也都是模板,需要的时候在application.properties配置即可 ############## ...

  7. springboot集成模板引擎freemarker和thymeleaf

    freemarkder和thymeleaf都是java的模板引擎,这里只介绍这两种模板引擎如何在sprongboot中配置: 1. freemarkder 1.1 在pom.xml中添加依赖包 < ...

  8. 170703、springboot编程之模板使用(thymeleaf、freemarker)

    官方不推荐集成jsp,关于使用jsp模板我这里就不赘述,如果有需要的,请自行百度! thymeleaf的使用 1.在pom中增加thymeleaf支持 <dependency> <g ...

  9. Springboot模板(thymeleaf、freemarker模板)

    目的: 1.thymeleaf模板 2.Freemarker模板 thymeleaf模板 thymeleaf 的优点: 支持html5标准,页面无须部署到servlet开发到服务器上,直接通过浏览器就 ...

随机推荐

  1. DWR日志 在log4j.xml配置

    一.日志 DWR依赖 Apache Commons Logging,可以使用log4j实现日志记录功能. 1.1 日志简介 和其他日志框架一样,当设置低等级的日志时所有高于此等级的日志也将会打印出来. ...

  2. hdu-6071 Lazy Running

    In HDU, you have to run along the campus for 24 times, or you will fail in PE. According to the rule ...

  3. mysql批量更新数据(性能优化) 第一种方式

    首先想到的是,一条一条更新的速度太慢了,然后就想批量更新,一次更新N条数据.实践是检验真理的唯一标准,不一会儿,代码就敲完了,重新试了一下,效果依旧不理想.啊哦,真是要崩溃!后面又想到了利用异步,我一 ...

  4. 关于 BenchmarkDotNet

    using BenchmarkDotNet.Attributes; using BenchmarkDotNet.Order; using System.Reflection; namespace Be ...

  5. 一小段 Dapper 的简单示例

    关于 Dapper 的介绍,请参考:Dapper - 一款轻量级对象关系映射(ORM)组件,DotNet 下 using System; using System.Collections.Generi ...

  6. 如何通过 Freemark 优雅地生成那些花里胡哨的复杂样式 Excel 文件?

    欢迎关注个人微信公众号: 小哈学Java, 文末分享阿里 P8 高级架构师吐血总结的 <Java 核心知识整理&面试.pdf>资源链接!! 个人网站: https://www.ex ...

  7. python 手机app数据爬取

    目录 一:爬取主要流程简述 二:抓包工具Charles 1.Charles的使用 2.安装 (1)安装链接 (2)须知 (3)安装后 3.证书配置 (1)证书配置说明 (2)windows系统安装证书 ...

  8. Java编程基础——常量变量和数据类型

    Java编程基础——常量变量和数据类型 摘要:本文介绍了Java编程语言的常量变量和数据类型. 常量变量 常量的定义 一块内存中的数据存储空间,里面的数据不可以更改. 变量的定义 一块内存中的数据存储 ...

  9. 【转】Python学习---超详细字符串用法大全,好文推荐!

    来自:Python编程与实战(微信号:pthon1024),作者:Jerryning 没有办法转,整个复制下来了 本文要点 字符串拼接 拆分含有多种分隔符的字符串 判读字符串a是否以字符串b开头或结尾 ...

  10. ORM:对象关系映射

    一.简单操作 定义:面向对象和关系型数据库的一种映射,通过操作对象的方式操作数据 对应关系: 类对应数据表 对象对应数据行(记录) 属性对应字段 导入:from app01 import models ...