` ```
js附加代码:
``` /*[+ var msg = ‘This is a working application’; +]*/ ```
js移除代码:
``` /*[- */ var msg = ‘This is a non-working template’; /* -]*/ ```
### 6、内嵌变量
为了模板更加易用,Thymeleaf还提供了一系列Base对象、Utility对象(内置于Context中),可以通过#直接访问。
#### Base对象
- 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 objec
下面用一段代码来举例一些常用的方法:
``` /* * ====================================================================== * See javadoc API for class org.thymeleaf.context.IContext * ====================================================================== */ ${#ctx.locale} ${#ctx.variableNames} /* * ====================================================================== * See javadoc API for class org.thymeleaf.context.IWebContext * ====================================================================== */ ${#ctx.request} ${#ctx.response} ${#ctx.session} ${#ctx.servletContext}
/* * ============================================================================ * See javadoc API for class org.thymeleaf.context.WebRequestParamsVariablesMap * ============================================================================ */ ${param.foo} // Retrieves a String[] with the values of request parameter ‘foo’ ${param.size()} ${param.isEmpty()} ${param.containsKey(‘foo’)}
/* * ====================================================================== * See javadoc API for class org.thymeleaf.context.WebSessionVariablesMap * ====================================================================== */ ${session.foo} // Retrieves the session atttribute ‘foo’ ${session.size()} ${session.isEmpty()} ${session.containsKey(‘foo’)} ```
#### Utility对象
- dates : *java.util.Date的功能方法类。* - calendars : *类似#dates,面向java.util.Calendar* - numbers : *格式化数字的功能方法类* - strings : *字符串对象的功能类,contains,startWiths,prepending/appending等等。* - objects: *对objects的功能类操作。* - bools: *对布尔值求值的功能方法。* - arrays:*对数组的功能类方法。* - lists: *对lists功能类方法* - sets - maps …
下面用一段代码来举例一些常用的方法:
**dates**
``` /* * Format date with the specified pattern * Also works with arrays, lists or sets */ ${#dates.format(date, ‘dd/MMM/yyyy HH:mm’)} ${#dates.arrayFormat(datesArray, ‘dd/MMM/yyyy HH:mm’)} ${#dates.listFormat(datesList, ‘dd/MMM/yyyy HH:mm’)} ${#dates.setFormat(datesSet, ‘dd/MMM/yyyy HH:mm’)}
/* * Create a date (java.util.Date) object for the current date and time */ ${#dates.createNow()}
/* * Create a date (java.util.Date) object for the current date (time set to 00:00) */ ${#dates.createToday()} ```
**strings**
``` /* * Check whether a String is empty (or null). Performs a trim() operation before check * Also works with arrays, lists or sets */ ${#strings.isEmpty(name)} ${#strings.arrayIsEmpty(nameArr)} ${#strings.listIsEmpty(nameList)} ${#strings.setIsEmpty(nameSet)}
/* * Check whether a String starts or ends with a fragment * Also works with arrays, lists or sets */ ${#strings.startsWith(name,’Don’)} // also array*, list* and set* ${#strings.endsWith(name,endingFragment)} // also array*, list* and set*
/* * Compute length * Also works with arrays, lists or sets */ ${#strings.length(str)}
/* * Null-safe comparison and concatenation */ ${#strings.equals(str)} ${#strings.equalsIgnoreCase(str)} ${#strings.concat(str)} ${#strings.concatReplaceNulls(str)}
/* * Random */ ${#strings.randomAlphanumeric(count)} ```
## thymeleaf布局
使用thymeleaf布局非常的方便
定义代码片段
```
```
在页面任何地方引入:
```
```
th:include 和 th:replace区别,include只是加载,replace是替换
返回的HTML如下:
```
© 2016
```
下面是一个常用的后台页面布局,将整个页面分为头部,尾部、菜单栏、隐藏栏,点击菜单只改变content区域的页面
```
Header left sidebar footer
```
任何页面想使用这样的布局值只需要替换中见的 content模块即可
``` … ```
也可以在引用模版的时候传参
```
```
layout 是文件地址,如果有文件夹可以这样写 fileName/layout:htmlhead htmlhead 是指定义的代码片段 如 `th:fragment=”copy”`
|
还没有评论,来说两句吧...