SPRING + THYMELEAF 配置
1、使用的是Spring EL而不是Ognl。
2、访问上下文的Bean用${@myBean.doSomething()}
3、th:field,th:errors,th:errorclass用于form processing。
4、要采用SpringTemplateEngine。
5、基本配置:
class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
<property name="prefix" value="/WEB-INF/templates/" />
<property name="suffix" value=".html" />
<property name="templateMode" value="HTML5" />
</bean>
<bean id="templateEngine"
class="org.thymeleaf.spring3.SpringTemplateEngine">
<property name="templateResolver" ref="templateResolver" />
</bean>
<property name="templateEngine" ref="templateEngine" />
<property name="order" value="1" />
<property name="viewNames" value="*.html,*.xhtml" />
</bean>
6、被@ModelAttribute注释的方法会在此controller每个方法执行前被执行,如果@ModelAttribute注释的方法有返回值,则表示在model中存放隐含名称的属性对象,比如返回Account,则相当于model.addAttribute("account",account),如果@ModelAttribute(value="aname")注释方法,则表示在model中增加aname的属性值。@ModelAttribute注释一个方法的参数则表示从model中或者从Form表单或者url中获取。
7、 @RequestMapping("/hello")public void novoid() { },返回视图为前缀+/hello+后缀,当方法返回Map,ModelMap等时都是相当于Request.setAttribute()。
8、<td th:text="${{sb.datePlanted}}">13/01/2011</td>双括号表示自动使用转换,常用于格式转换。
9、<td th:text="${#strings.arrayJoin(#messages.arrayMsg(#strings.arrayPrepend(sb.features,'seedstarter.feature.')),', ')}">Electric Heating, Turf</td>,首先将数组feathers都加上前缀,然后利用messages翻译国际化,最终组合成一个字符串。
10、用th:object指定command object,比如:<form action="#" th:action="@{/save}" th:object="${person}" method="post">,两点限制,第一object只能是model 的直接attribute,不能使${person.baseInfo},第二,th:object的子级标签内不能再使用th:object。inputField使用:<input type="text" th:field="*{datePlanted}" />。
12、checkbox标签:
<label th:for="${#ids.next('covered')}" th:text="#{seedstarter.covered}">Covered</label>
<input type="checkbox" th:field="*{covered}" />
</div>
checkbox array:
<li th:each="feat : ${allFeatures}">
<input type="checkbox" th:field="*{features}" th:value="${feat}" />
<label th:for="${#ids.prev('features')}" th:text="#{${'seedstarter.feature.' + feat}}">Heating</label>
</li>
</ul>
13、radios:
<li th:each="ty : ${allTypes}">
<input type="radio" th:field="*{type}" th:value="${ty}" />
<label th:for="${#ids.prev('type')}" th:text="#{${'seedstarter.type.' + ty}}">Wireframe</label>
</li>
</ul>
14、dropdownlist or select。
<option th:each="type : ${allTypes}"
th:value="${type}"
th:text="#{${'seedstarter.type.' + type}}">Wireframe</option>
</select>
15、预处理:<select th:field="*{rows[__${rowStat.index}__].variety}">而不使用<select th:field="*{rows[rowStat.index].variety}">,因为spring el不会计算数组索引中的变量或者表达式。
16、错误显示:<input type="text" th:field="*{datePlanted}" th:class="${#fields.hasErrors('datePlanted')}? fieldError" />
<li th:each="err : ${#fields.errors('datePlanted')}" th:text="${err}" />
</ul>
<p th:if="${#fields.hasErrors('datePlanted')}" th:errors="*{datePlanted}">Incorrect date</p>
<li th:each="err : ${#fields.errors('*')}" th:text="${err}">Input is incorrect</li>
</ul>
全局错误:
<li th:each="err : ${#fields.errors('global')}" th:text="${err}">Input is incorrect</li>
</ul>
在form外显示错误:
<div th:errors="${myForm.date}">...</div>
<div th:errors="${myForm.*}">...</div>
<div th:if="${#fields.hasErrors('${myForm}')}">...</div>
<div th:if="${#fields.hasErrors('${myForm.date}')}">...</div>
<div th:if="${#fields.hasErrors('${myForm.*}')}">...</div>
<form th:object="${myForm}">
...
</form>
17、利用功能类转换:#conversions.convert(Object,Class),#conversions.convert(Object,String)
18、渲染模板的片段,常用于ajax,返回一部分文本做替换使用。
在ViewBean中指定片段:
<property name="templateName" value="index" />
<property name="fragmentSpec">
<bean class="org.thymeleaf.standard.fragment.StandardDOMSelectorFragmentSpec"
c:selectorExpression="content" />
</property>
</bean>
public String showContentPart() {
...
return "content-part";//返回上面定义的bean名称。
}
c:selectorExpression="#content" :完全基于html dom selector,无需th:fragment。
在controller中指定片段:
public String showContentPart() {
...
return "index :: content";
}
还可以返回带参数的片段:
public String showContentPart() {
...
return "index :: #content ('myvalue')";
}
SPRING + THYMELEAF 配置的更多相关文章
- spring boot 下 thymeleaf 配置
1. thymeleaf 配置参数 [参考文章]:spring-boot-starter-thymeleaf 避坑指南 #<!-- 关闭thymeleaf缓存 开发时使用 否则没有实时画面--& ...
- Spring boot Thymeleaf 配置
第一步:pom.xml加入依赖 <!-- HTML templates--> <dependency> <groupId>org.springframework.b ...
- spring mvc 和spring security配置 spring-servlet.xml和spring-security.xml设置
spring-servlet.xml配置 <?xml version="1.0" encoding="UTF-8"?> <beans xmln ...
- springbooot2 thymeleaf 配置以及加载资源文件。Cannot find template location: classpath:/templates/ (please add some templates or check your Thymeleaf configuration)
最近在学习springbooot2 和 thymeleaf 程序文件 application.properties文件配置: #thymeleaf spring.thymeleaf.prefix=cl ...
- spring boot 之 spring security 配置
Spring Security简介 之前项目都是用shiro,但是时过境迁,spring security变得越来越流行.spring security的前身是Acegi, acegi 我也玩过,那都 ...
- spring boot 配置 freemarker
1.springboot 中自带的页面渲染工具为thymeleaf 还有freemarker 这两种模板引擎 简单比较下两者不同, 1.1freemaker 优点 freemarker 不足:thym ...
- springboot+maven+thymeleaf配置实战demo
本案例使用thymeleaf,与springboot配置使用.thymeleaf是一种模板语言,可以动态或者静态显示文本内容. 1 .项目结构 2.构建springboot项目 通过idea的new ...
- IDEA SpringBoot +thymeleaf配置
1.pom添加以下依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactI ...
- Spring Boot 配置大全
Spring Boot 允许通过外部配置让你在不同的环境使用同一应用程序的代码,简单说就是可以通过配置文件来注入属性或者修改默认的配置. SpringBoot的配置方式有很多,它们的优先级如下所示(优 ...
随机推荐
- Python’s SQLAlchemy vs Other ORMs[转发 5] PonyORM
PonyORM PonyORM allows you to query the database using Python generators. These generators are trans ...
- springmvc单文件上传
1.创建上传页面 <form action="first.do" method="post" enctype="multipart/form-d ...
- C:上台阶
总时间限制: 1000ms 内存限制: 65536kB描述楼梯有n(100 > n > 0)阶台阶,上楼时可以一步上1阶,也可以一步上2阶,也可以一步上3阶,编程计算共有多少种不同的走法. ...
- Objective-C( Foundation框架 一 字符串)
Objective-C 中核心处理字符串的类是 NSString 与 NSMutableString ,这两个类最大的区别就是NSString 创建赋值以后该字符串的内容与长度不能在动态的更改,除非重 ...
- 转:在支持ARC工程中编译不支持ARC的文件
转:http://blog.csdn.net/duxinfeng2010/article/details/8709697 实践总结:-fno-objc-arc 设置 解决了 旧代码中存在 releas ...
- JQ判断屏幕宽度
<script> if (screen.width < 768){....} </script>
- PHP的三种输出方式
(1)echo 是PHP语句,没有返回值,用于输出一个或多个字符串 (2)print() 是函数,可以有返回值,只能打印出简单类型变量的值,例如int.string (3)print_r() 是函数, ...
- python基础教程-第二章-列表和元组
本章将引入一个新的概念,:数据结构.数据结构是通过某种方式(例如对元素进行编号)组织在 一起的数据元素的集合,这些数据元素可以是数字或者字符,甚至可以是其他数据结构.在python中,最基本的数据结构 ...
- java web图片上传和文件上传
图片上传和文件上传本质上是一样的,图片本身也是文件.文件上传就是将图片上传到服务器,方式虽然有很多,但底层的实现都是文件的读写操作. 注意事项 1.form表单一定要写属性enctype=" ...
- 通过top命令发现plymouthd进程cpu负载达到近100% 解决办法
最近几天一直遇到服务器cpu100%, 通过top命令发现plymouthd进程cpu负载达到近100% 解决方法:打开 /boot/grub/menu.lst , 去掉 “rhgb quiet”这两 ...