• Springboot整合Thymeleaf


    1.Thymeleaf的依赖:

        <!-- thymeleaf的启动器 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
    

      一般是要升级版本的,请再添加:

      <properties>
      <thymeleaf.version>3.0.2.RELEASE</thymeleaf.version>
      <thymeleaf-layout-dialect.version>2.0.4</thymeleaf-layout-dialect.version>
      </properties>
    

    2.存放视图的目录

    目录位置:src/main/resources/templates

    templates:该目录是安全的。意味着该目录下的内容是不允许外界直接访问的

    3.语法

    th:text   在页面输出值

    th:value   可以将一个值放入到input标签的value中

    th:if  

    <span th:if="${msg}=='瑞文'">男</span>
    

    th:switch 

    	<div th:switch="${id}">
    	    <span th:case="2">2</span>
    	    <span th:case="4">4</span>
    	    <span th:case="6">6</span>
    	    <span th:case="7">7</span>
    	</div>
    

    th:each

    		<tr th:each="user,var : ${list}">
    			<td th:text="${user.userid}"></td>
    			<td th:text="${user.username}"></td>
    			<td th:text="${user.age}"></td>
    			<td th:text="${var.index}"></td>
    			<td th:text="${var.count}"></td>
    			<td th:text="${var.size}"></td>
    			<td th:text="${var.even}"></td>
    			<td th:text="${var.odd}"></td>
    			<td th:text="${var.first}"></td>
    			<td th:text="${var.last}"></td>
    		</tr>
    

      说明:user为list的赋值变量,不多说;var是list的状态变量,其中:

    1,index:当前迭代器的索引从0开始
    2,count:当前迭代对象的计数从1开始
    3,size:被迭代对象的长度
    4,even/odd:布尔值,当前循环是否是偶数/奇数从0开始
    5,first:布尔值,当前循环的是否是第一条,如果是返回true否则返回 false
    6,last:布尔值,当前循环的是否是最后一条,如果是则返回true否则返回false

    th:each迭代Map

    	<table border="1">
    		<tr>
    			<th>ID</th>
    			<th>Name</th>
    			<th>Age</th>
    		</tr>
    		<tr th:each="maps : ${map}">
    			<td th:text="${maps}"></td>
    		</tr>
    	</table>
    	<table border="1">
    		<tr>
    		    <th>key</th>
    			<th>ID</th>
    			<th>Name</th>
    			<th>Age</th>
    		</tr>
    		<tr th:each="maps : ${map}">
    			<td th:each="entry:${maps}" th:text="${entry.key}"></td>
    			<td th:each="entry:${maps}" th:text="${entry.value.userid}"></td>
    			<td th:each="entry:${maps}" th:text="${entry.value.username}"></td>
    			<td th:each="entry:${maps}" th:text="${entry.value.age}"></td>
    		</tr>
    	</table>

    4.内置对象

    语法:

    1,调用内置对象一定要用#
    2,大部分的内置对象都以s结尾 ,如strings、numbers、dates

     
    ${#strings.isEmpty(key)}
    判断字符串是否为空,如果为空返回 true,否则返回 false
    ${#strings.contains(msg,'T')}
    判断字符串是否包含指定的子串,如果包含返回 true,否则返回 false
    ${#strings.startsWith(msg,'a')}
    判断当前字符串是否以子串开头,如果是返回 true,否则返回 false
    ${#strings.endsWith(msg,'a')}
    判断当前字符串是否以子串结尾,如果是返回 true,否则返回 false
    ${#strings.length(msg)}
    返回字符串的长度
    ${#strings.indexOf(msg,'h')}
    查找子串的位置,并返回该子串的下标,如果没找到则返回-1
    ${#strings.substring(msg,13)}    ${#strings.substring(msg,13,15)}
    截取子串,用户与 jdk String 类下 SubString 方法相同
    ${#strings.toUpperCase(msg)}  ${#strings.toLowerCase(msg)}
    字符串转大小

    5.域对象操作

     
    request.setAttribute("req", "HttpServletRequest");
    <span th:text="${#httpServletRequest.getAttribute('req')}"></span>
    request.getSession().setAttribute("sess", "HttpSession");
    <span th:text="${session.sess}"></span>
    request.getSession().getServletContext().setAttribute("app","Application");
    <span th:text="${application.app}"></span>

    6.URL表达式

    绝对路径:<a th:href="@{http://www.baidu.com}">绝对路径</a>

    相对路径:

           相对于项目的相对路径:<a th:href="@{/show}">相对路径</a>

           相对于服务器路径的根:<a th:href="@{~/project2/resourcename}">相对于服务器的根</a>

    在url中实现传参:<a th:href="@{/show(id=1,name=zhagnsan)}">相对路径-传参</a>

    在url中通过restful风格进行参数传递:<a th:href="@{/path/{id}/show(id=1,name=zhagnsan)}">相对路径-传参-restful</a>

  • 相关阅读:
    vmstat
    linux内存机制
    TOP命令
    linux下查阅文件内容cat,more,less,tail
    linux stat命令
    linux修改主机名-IP
    alias
    linux软硬链接
    linux 常用find命令
    ubuntu下交叉编译imagemagick
  • 原文地址:https://www.cnblogs.com/xzmiyx/p/9890306.html
Copyright © 2020-2023  润新知