Thymeleaf 基本用法总结 一、引用命名空间 在html中引入此命名空间,可避免编辑器出现html验证错误,虽然加不加命名空间对Thymeleaf的功能没有任何影响。 二、输出内容 2.1
Welcome to our grocery store!
说明: 1. th:text 用来将内容输出到所在标签的body中。 2. #{home.welcome} 用来引入数据home对象中的 welcome属性。 3. 可以用th:utext 用来显示“unescaped ” 的html内容。 2.2Today is: 13 February 2011
说明:${today} 用来引用 today 变量 三、访问对象 ${param.x} 返回名为x 的 request参数。(可能有多个值) ${session.x} 返回名为x的Session参数。 ${application.x} 返回名为 servlet context 的参数。 四、基本语法 4.1 #{home.welcome} -- 访问数据 4.2 #{home.welcome(${session.user.name})} -- 格式化数据 当 home.welcome 为 "abcdegf{0}" 类似这种内容时。(多个参数以逗句分隔)。 4.3 ${today} --- 访问变量 4.4 访问基本对象 #ctx: the context object. #vars: the context variables. #locale: the context locale. #request: (only in Web Contexts) the HttpServletRequest object. #response: (only in Web Contexts) the HttpServletResponse object. #session: (only in Web Contexts) the HttpSession object. #servletContext: (only in Web Contexts) the ServletContext object. 其它公共对象参考: http://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html#appendix-a-expression-basic-objects 4.5 日期的输出 13 May 2011 4.6 星号语法Name: Sebastian.
Surname: Pepper.
Nationality: Saturn.
Age: 27.
Some text here...
说明:thymeleaf 的处理模板内容的顺序与书写顺序无关,只能通过 __${expression}__ ,来将需要先一步计算出来后面 要用的变量指定为优化处理。 五、设置 Attribute 值 5.1 设置任何Attribute 的方法 --设置单个 --一次设置多个 5.2 设置一些内置的Attribute的方法