zoukankan      html  css  js  c++  java
  • 静态资源和模板引擎

    静态资源和模板引擎

    静态资源默认存放在resources下的public, resources, static目录, 而且同名优先级比较resources>static>public

    在静态资源目录中的index.html会成为首页

    templates目录下的所有页面只能通过controller来跳转, 相当于WEB-INF, 需要模板引擎提供视图解析器来驱动


    模板引擎thymeleaf

    1. 导入依赖

      <dependency>
          <groupId>org.thymeleaf</groupId>
          <artifactId>thymeleaf-spring5</artifactId>
      </dependency>
      <dependency>
          <groupId>org.thymeleaf.extras</groupId>
          <artifactId>thymeleaf-extras-java8time</artifactId>
      </dependency>
      
    2. controller

      @RequestMapping("/test")
      public String index(Model model) {
          model.addAttribute("msg", "<h1>hello thymeleaf</h1>");
          model.addAttribute("users", Arrays.asList("野比大雄", "野原新之助"));
          return "test";
      }
      
    3. test.html

      <!DOCTYPE html>
      <!--加入约束-->
      <html lang="en" xmlns:th="http://www.thymeleaf.org">
      <head>
          <meta charset="UTF-8">
          <title>Title</title>
      </head>
      <body>
      <!--不会转义-->
      <div th:text="${msg}"></div>
      <!--会转义-->
      <div th:utext="${msg}"></div>
      <hr>
      
      <!--从users中遍历出所有的元素-->
      <h1 th:each="user:${users}" th:text="${user}"></h1>
      <!--行内写法-->
      <h1 th:each="user:${users}">[[${user}]]</h1>
      
      </body>
      </html>
      

    标准表达式语法

    • 简单表达式 (simple expressions)

      • 变量表达式 ${...}
      • 选择变量表达式 *{...}
      • 消息表达式 #{...}
      • 链接url表达式 @{...}
    • 字面量

      • 文本 'one text','another one!',...
      • 数值 0,34,3.0,12.3,...
      • 布尔类型 true false
      • 空 null
      • 文本字符 one,sometext,main
    • 文本操作

      • 字符串连接 +
      • 字符串连接 |The name is ${name}|
    • 算术运算

      • 二元运算符 +, - , * , / , %
      • 负号(一元运算符) -
    • 布尔操作

      • 二元操作符 and,or
      • 非(一元操作符) !,not
    • 关系操作符

      • > , < , >= , <= (gt , lt , ge , le)
      • == , != (eq, ne)
    • 条件判断

      • if-then : (if) ? (then)
      • if-then-else : (if) ? (then) : (else)
      • default : (value) ?: (defaultvalue)
  • 相关阅读:
    简单排序(冒泡、选择、插入)
    配置Tomcat数据源
    使用spring的邮件发送功能
    安装Tomcat
    Spring-MongoDB简单操作
    cisco ASA ios升级或恢复
    ASA 用TFTP 备份配置方法
    DELL MD3200i存储控制器解锁方法
    IBM ServerGuide引导盘全系列下载网址
    ASA5520远程配置 telnet,ssh
  • 原文地址:https://www.cnblogs.com/pinked/p/12340094.html
Copyright © 2011-2022 走看看