thymeleaf常用属性

作者:ITPSC
出处:http://www.cnblogs.com/hjwublog/


th:action

定义后台控制器路径,类似<form>标签的action属性。

例如:

<form id="login-form" th:action="@{/login}">...</form>

th:each

对象遍历,功能类似jstl中的<c:forEach>标签。

例如:

public class StudentRequestBean {

private List<Student> students;

...

}

public class Student implements Serializable{

private String firstName;

private String school;

...}

@RequestMapping(value = "/addStudent", method = RequestMethod.POST)

public String addStudent(@ModelAttribute(value = "stuReqBean") 

StudentRequestBean stuReqBean,ModelMap model) {...}
<form id="login-form" th:action="@{/addStudent}" 

th:object="${stuReqBean}" method="POST">

<div class="student" th:each="stuIter,rowStat:${stuReqBean.students}">

<input type="text" class="firstName" value="" 

th:field="*{students[__${rowStat.index}__].firstName}"></input>

<input type="text" class="school" value="" 

th:field="*{students[__${rowStat.index}__].school}"></input>

...

</div>

</form>

上面的例子中通过选择表达式*{}既能将表单绑定到后台的StudentRequestBean中的集合属性students,也能将Servlet上下文中的StudentRequestBean中的List类型的students变量回显,回显时通过th:each进行遍历。

注意1:绑定集合属性元素下标的用法*{students[__${rowStat.index}__].firstName}

注意2:如果List<Student> students为null,页面将无法显示表单,后台必须给students初始化一个值,即:

List<Student > stus = new ArrayList<Student >();

stus .add(new Student ());

StudentRequestBean.setStudents(stus );

注意3:stuIter代表students的迭代器

th:field

常用于表单字段绑定。通常与th:object一起使用。 属性绑定、集合绑定。

如:

public class LoginBean implements Serializable{...

private String username;

private List<User> user;

...}

public class User implements Serializable{...

private String username;;

...}

@RequestMapping(value = "/login", method = RequestMethod.POST)

public String login(@ModelAttribute(value = "loginBean") LoginBean loginBean,ModelMap model) {..}
<form id="login-form" th:action="@{/login}" th:object="${loginBean}">...

<input type="text" value="" th:field="*{username}"></input>

<input type="text" value="" th:field="*{user[0].username}"></input>

</form>

th:href

定义超链接,类似<a>标签的href 属性。value形式为@{/logout}

例如:

<a th:href="@{/logout}" class="signOut"></a>

th:id

div id声明,类似html标签中的id属性。

例如:

<div class="student" th:id = "stu+(${rowStat.index}+1)"></div>

th:if

条件判断。

例如:

<div th:if="${rowStat.index} == 0">... do something ...</div>

th:include

见th:fragment

th:fragment

声明定义该属性的div为模板片段,常用与头文件、页尾文件的引入。常与th:include,th:replace一起使用。

例如:

声明模板片段/WEBINF/templates/footer. html

<div th: fragment=" copy" >

? 2011 The Good Thymes Virtual Grocery

</div>

引入模板片段

<div th: include=" /templates/footer : : copy" ></div>

<div th: replace=" /templates/footer : : copy" ></div>

th:object

用于表单数据对象绑定,将表单绑定到后台controller的一个JavaBean参数。常与th:field一起使用进行表单数据绑定。

例如:

public class LoginBean implements Serializable{...}

@RequestMapping(value = "/login", method = RequestMethod.POST)

public String login(@ModelAttribute(value = "loginBean") LoginBean loginBean,ModelMap model) {...}
<form id="login-form" th:action="@{/login}" th:object="${loginBean}">...</form>

th:src

用于外部资源引入,类似于<script>标签的src属性,常与@{}一起使用。

例如:

<script th:src="@{/resources/js/jquery/jquery.json-2.4.min.js}"

th:replace

见th:fragment

th:text

文本显示。

例如:

<td class="text" th:text="${username}" ></td>

th:value

用于标签复制,类似<option>标签的value属性。

例如:

<option th:value="Adult">Adult</option>

<input  id="msg" type="hidden" th:value="${msg}" />
时间: 2024-08-10 00:04:28

thymeleaf常用属性的相关文章

Thymeleaf常用语法:HTML属性设置

使用Thymeleaf的属性来设置HTML属性.(1)使用th:attr属性可以修改原来HTML节点的属性:(2)th:attr属性可以同时设置多个属性:(3)每一个HTML属性都有对应的Thymeleaf属性,如th:attr="value='值'"可换为th:value="值"(4)HTML的type为checkbox.readonly.required.disabled的,Thymeleaf属性可写为th:checked="true/false&qu

window对象的常用属性,常用方法

window对象的常用属性: window.self 返回当前窗口的引用 window.parent   返回当前窗体的父窗体对象 window.top 返回当前窗体最顶层的父窗体的引用 window.outerwidth       返回当前窗口的外部宽 window.outerheight  返回当前窗口的外部高 window.innerwidth       返回当前窗口的可显示区域宽 window.innerheight  返回当前窗口的可显示区域高 提示:通过直接在Chrome控制台中

样式常用属性

笔记信息 复习: 表单作用: 从使用的角度上说:html提供了一个输入内容的途径. 从服务器的角度:提供了一个收集信息的途径. 以便客户端和服务器进行交互. 例:注册页面,上传文件. 3种常见元素:input select textarea Input的十种常见类型: text,password,radio,checkbox,submit,reset,button,image,hidden,file Radio中name应该保持相同,以确保在单选按钮中的元素完成互斥. Checkbox中name

UIScrollView 的常用属性和方法

1.继承关系:UIView -> UIResponder -> NSObject2.代理:UIScrollViewDelegate// *2.1 一但偏移量发生变化就会调用  - (void)scrollViewDidScroll:(UIScrollView *)scrollView;// *2.2 将要缩放时就return谁   2.2.1 由于要缩放内容,我们需要设置最大和最小的缩放比例    scrollView.minimumZoomScale = 1.0f;    scrollVie

CoreAnimation 核心动画 的一些常用属性 和 方法

1.常用属性: frame   bounds   center   alpha    Transition 过渡    transform 动画效果 2.常用方法: +(void)setAnimationDelegate:(id)delegate; +(void)setAnimationWillStartSelector:(SEL)selector; 当动画结束的时候,执行delegate对象的selector,并且把beginAnimations:context:中传入的参数传进selecto

SVG DOM常用属性和方法介绍

将以Adobe SVG Viewer提供的属性和方法为准,因为不同解析器对JavaScript以及相关的属性和方法支持的程度不同,有些方法和属性是某个解析器所特有的.SVG支持DOM2标准. 12.2.1  文档初始化相关 evt属性 evt表示事件本身,可以通过evt获取与当前事件相关的信息,用户可以在script中定义响应函数,进行相应的处理.它与普通JavaScript脚本中的event基本相同,只不过在普通JavaScript的脚本中简写成“e”. ownerDocument属性 通过引

UITableView的常用属性和cell的内存优化

UITableView的常用属性: 分割线颜色设置: 1> 设置separatorStyle: 分割线的颜色 方法:tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine; 2> 设置separatorColor 不用系统枚举的值,自己设定颜色的值 24bitRGB R:8bit G:8bit B:8bit =========== 32bitRGBA R:8bit G:8bit B:8bit A:8bit 表示透明度

Android 控件布局常用属性

<!--单个控件经常用到android:id -- 为控件指定相应的IDandroid:text -- 指定控件当中显示的文字,需要注意的是,这里尽量使用strings.xml文件当中的字符串android:grivity -- 指定控件的基本位置,比如说居中,居右等位置android:textSize -- 指定控件当中字体的大小android:background -- 指定该控件所使用的背景色,RGB命名法 android:width -- 指定控件的宽度android:height --

013.泛型、窗体常用属性

泛型: *是C#语言2.0和通用语言运行时的新特性*利用参数化类型将类型抽象化,从而实现更为灵活的服用 优点:*缓解了代码膨胀的情况*提供了一个强类型的编程模型,类型安全*值类型不再需要装箱操作*性能的到提高*代码可读性好 1.泛型方法 既可以放在普通类中也可以定义在泛型类中 访问修饰符[可选修饰符]返回类型 方法名<参数化类型>(参数列表){} 方法名(参数)://推断类型 根据给的类型推断出来他的参数化类型用的应该是什么方法名<类型>(参数): 类型参数:习惯上用T或者T开头的