maven+thymeleaf笔记-2

来源:互联网 发布:cip数据核字号在哪里 编辑:程序博客网 时间:2024/05/23 07:25

承接maven+thymeleaf笔记-1

简单表示式:变量表达式: ${…}选择变量表达式: *{…}信息表达式: #{…}URL连接表达式: @{…}文字类型:字符型: ‘one text’ , ‘Another one!’ ,…数值型: 0 , 34 , 3.0 , 12.3 ,…Boolean型: true , false空值: null文本字符串: one , sometext , main ,…字符串操作: 字符串连接: +文字替换: |The name is ${name}|数值型操作: 运算符: + , - , * , / , %负号: -Boolean操作: 运算符: and , or非运算符: ! , not比较相等算法: 比较: > , < , >= , <= ( gt , lt , ge , le )相等算法: == , != ( eq , ne )条件语句:If-then: (if) ? (then)If-then-else: (if) ? (then) : (else)Default: (value) ?: (defaultvalue)所有上面算法都可以随意组合和嵌套:

信息表达式:
在HomeController添加一个实体

        Product product = new Product();        product.setId(1);        product.setName("产品名称...你说是什么呢!");        product.setPrice("100");        ctx.setVariable("product", product);

在home.properties文件中留一个位置放信息表达式:{0}:便是留的一个占位符

home.welcome=Welcome to our grocery store, {0} (from default messages)!

通过在页面取home.welcome时利用一个(信息表达式)

<span th:text="#{home.welcome(${product.name})}"></span>

系统基本对象:

OGNL有以下基本内置对象#ctx : the context#object. vars: the context variables.#locale : the context locale.#httpServletRequest : (only in Web Contexts)theHttpServletRequest object.#httpSession : (only in Web Contexts) the HttpSession object. 

引用的方式:

<p><span th:text="${#locale.country}">US</span></p>

Thymeleaf提供的对象:

#dates : 为 java.util.Date对象提供工具方法,比如:格式化,提取年月日等.#calendars : 类似于#dates , 但是只针对java.util.Calendar对象.#numbers : 为数值型对象提供工具方法。#strings :为String 对象提供工具方法。如: contains, startsWith, prepending/appending等。#objects : 为object 对象提供常用的工具方法。#bools : 为boolean 对象提供常用的工具方法。#arrays : 为arrays 对象提供常用的工具方法。#lists :为lists对象提供常用的工具方法。#sets : 为sets对象提供常用的工具方法。#maps : 为maps对象提供常用的工具方法。#aggregates :为创造一个arrays 或者 collections聚集函数提供常用的工具方法。#messages : utility methods for obtaining externalized messages inside variables expressions, in the same way as they would be obtained using #{…} syntax?.#ids : 为可能需要循环的ID属性提供常用的工具方法。

案例:
在HomeController中添加:

//日期        ctx.setVariable("time", date);        //数字        ctx.setVariable("num1", 112.234);        ProductService ps = new ProductService();        List<Product> products = ps.getAllProduct();        //列表        ctx.setVariable("products", products);

home.html页面

<h3>Thymeleaf提供的对象</h3>    <!-- 日期格式化 -->     <p th:text="${#dates.format(time, 'yyyy-MM-dd HH:mm:ss')}"></p>     <!-- 此示例表示保留两位小数位,整数位自动; -->     <p th:text="${#numbers.formatDecimal(num1,0,2)}"></p>     <!-- 保留两位小数位,整数位保证5为,不足添加0 -->     <p th:text="${#numbers.formatDecimal(num1,5,2)}"></p>     <!-- 获取列表长度 -->     <p th:text="${#lists.size(products)}"></p>     <!-- url参数      当访问:http://localhost:8080/thymeleafNote1/?page=100 下面获取访问的参数page的值 -->     <p th:text="${#httpServletRequest.getParameter('page')}"></p>     <!-- 定义变量  变量的使用范围在标签内,下面的就是在div这个标签内 -->     <div th:with="value1=${#lists.size(products)},value2=${#dates.format(time, 'yyyy-MM-dd HH:mm:ss')}">        第一个变量的值:<span th:text="${value1}"></span><br>        第二个变量的值:<span th:text="${value2}"></span>     </div>    <p>参考:http://blog.csdn.net/zsl129/article/details/53007192</p>

选择表达式用法(*{ })

<!-- 下面用*{}表示都是 ${product}的属性-->    <div th:object="${product}">        <p th:text="*{name}"></p>    </div>    <!-- 与上面是等价的 -->    <p th:text="${product.name}"></p>

URL

URL链接有以下几种类型:   绝对地址,如http://www.thymeleaf.org相对地址 相对页面地址.如:/user/login.html服务器相对地址如:~/billing/processInvoice(部署在同服务器,不同域名的地址)让我们来使用th:href属性:<!-- Will produce 'http://localhost:8080/gtvg/order/details?orderId=3' (plus rewriting) --><a href="details.html"th:href="@{http://localhost:8080/gtvg/order/details(orderId=${o.id})}">view</a><!-- Will produce '/gtvg/order/details?orderId=3' (plus rewriting) --><a href="details.html" th:href="@{/order/details(orderId=${o.id})}">view</a><!-- Will produce '/gtvg/order/3/details' (plus rewriting) --><a href="details.html" th:href="@{/order/{orderId}/details(orderId=${o.id})}">view</a>th:href属性修饰符:它将计算并替换使用href链接URL 值,并放入的href属性中。我们可以使用URL参数的表达式(比如在orderId=${o.id} )如果需要多个参数,这些将由逗号分隔,比如:@{/order/process(execId=${execId},execType=’FAST’)}变量也允许URL路径中使用,比如:@{/order/{orderId}/details(orderId=${orderId})}URL中以”/“开头的路径(比如/order/details)将会加上服务器地址和域名。形成完整的URLth:href中可以直接使用静态地址。URL可以用复杂的表达式:<a th:href="@{${url}(orderId=${o.id})}">view</a><a th:href="@{'/details/'+${user.login}(orderId=${o.id})}">view</a>

基本类型操作

字符型

<p>文本文字可以用单引号来包含。需要转义的话可以用\’转义</p><p th:text="'曹雪坤的字符型'"></p>

数值型

<p th:text="2013"></p><p th:text="2013+45"></p>

布尔值

<div>        得到布尔值<p th:text="${product.price} == 1200"></p>        布尔值为true显示标签中的代码<p  th:if="${product.price} == 100">boolean为true显示</p>        布尔值为false显示标签中的代码<p th:unless="${product.price} == 1200">ssssss</p>    </div>        null型        <p th:if="null == null">null类型</p>

注:

<div th:if="${user.isAdmin()} == false"> :是thymeleaf解析<div th:if="${user.isAdmin() == false}">:是ognl解析

文本间的连接
注:${…}表达式可以被放在|….|之间,但是不能放在’….’之间

    <p th:text="'文本间的连接:'+${product.price}+${product.name}"></p>    <p th:text="|'文本间的连接:'${product.price}${product.name}|"></p>    <p th:text="'文本间的连接:'+|${product.price}---------${product.name}|"></p>

算术表达式:

<div th:with="isEven=${product.price} % 3 ">        算术表达式的值:<span th:text="${isEven}"></span></div>

比较

<p th:if="${product.price} &ge; 1">100 &ge; 1</p><p>可以用别名替代 gt ( > ), lt ( < ), ge ( >= ), le ( <= ), not ( ! ), eq ( == ),neq / ne ( != ).</p>

条件表达式:
${product.price} > 1 ? ‘class1’:’class2’
案例:

<p th:class="${product.price} &gt; 1 ? 'class1':'class2'">th:class 为一个标签添加class属性,点击我</p><script type="text/javascript">        /* $(".class1").click(function(){            alert();        }); */        var classs =  document.getElementsByClassName("class1");        classs[0].onclick=function(){            alert("<p th:class=\"${product.price} &gt; 1 ? 'class1':'class2'\">th:class 为一个标签添加class属性,点击我</p>");        }    </script>

:th:class为一个标签添加class属性

原创粉丝点击