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的配置方式有很多,它们的优先级如下所示(优 ...
随机推荐
- linux 后台运行命令 nohup命令
转载:http://if.ustc.edu.cn/~ygwu/blog/archives/000538.html 2005年04月18日 简单而有用的nohup命令在UNIX/LINUX中,普通进程用 ...
- oracle中schema指的是什么?
看来有的人还是对schema的真正含义不太理解,现在我再次整理了一下,希望对大家有所帮助. 我们先来看一下他们的定义:A schema is a collection of database obje ...
- 对checkbox 的checked的一些总结
在做一个jquery树形结构的复选框选择的效果. 遇到的问题: 1.jquery复选框判断是否被选中 $(check).attr("checked"),可能提示为undefied: ...
- $.getJSON( )的使用方法简介
JSON(JavaScript Object Notation)即JavaScript对象表示法,是一种轻量级的数据交换格式.它非常便于编程人员对数据的处理,也便于机器对数据的解析和生成,应用非常广泛 ...
- 关于ImageLoader的详细介绍
转载请注明本文出自xiaanming的博客(http://blog.csdn.net/xiaanming/article/details/26810303),请尊重他人的辛勤劳动成果,谢谢! 相信大家 ...
- [转]解决ubuntu下面putty不能连接RS232串口(USB2COM线)
http://m.blog.csdn.net/blog/wuyanrobert_tjsd/33045255 最后还是sudo了,感觉注销后没有加入dialout组
- ubuntu下内核源码树的建立
参考的博文: http://www.360doc.com/content/12/0604/12/8890849_215794364.shtml http://www.cnblogs.com/pd520 ...
- app转让遇到的坑
家人共享的一部分 首先我们要符合app转让的一些基本规定,填写正确的信息去申请转让.(google会有很多正确的转让步骤),这里我就不多写出来了. 当接收到接受app的时候会出现一些想不到的问题. 其 ...
- 如何修改WAMP中mysql默认空密码 以及修改时报错的处理方法
WAMP安装好后,mysql密码是为空的,那么要如何修改呢?其实很简单,通过几条指令就行了,下面我就一步步来操作. 首先,通过WAMP打开mysql控制台. 提示输入密码,因为现在是空,所以直接按回车 ...
- js 中混乱this
1.在HTML元素事件属性中inline方式使用this关键字: <div onclick=" // 可以在里面使用this ">division element&l ...