`
somefuture
  • 浏览: 1087421 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Thymeleaf的内置属性

阅读更多

Thymeleaf是另一个Java视图模板引擎,使用上和FreeMarker各有千秋,不了解的可以从其他博文里学习一下。我这里主要记录一下它的内置属性。

 本文不是Thymeleaf入门教程,也不是对其标签进行全面讲解只对其属性等价标签进行记录,以为辞典。

 

Thymeleaf提供了一个标签th:attr,可以把多个DOM标签用逗号分隔后写进去:

<img src="../../images/gtvglogo.png"
th:attr="src=@{/images/gtvglogo.png},title=#{logo},alt=#{logo}" />

 这个例子里给<img>标签设置了三个属性标签:src、title、alt。<img>自带的src会被Thymeleaf处理后扔掉而使用自己的。

 

这个标签不太优雅,不仅写起来紊乱,读起来也凌乱。所以很少用,一般用其他的代替:

 

<img src="../../images/gtvglogo.png"
th:src="@{/images/gtvglogo.png}" th:title="#{logo}" th:alt="#{logo}" />

 作为th:attr的实例用法,Thymeleaf提供了几乎全部标签的Thymeleaf等价标签:

th:abbr th:accept th:accept-charset
th:accesskey th:action th:align
th:alt th:archive th:audio
th:autocomplete th:axis th:background
th:bgcolor th:border th:cellpadding
th:cellspacing th:challenge th:charset
th:cite th:class th:classid
th:codebase th:codetype th:cols
th:colspan th:compact th:content
th:contenteditable th:contextmenu th:data
th:datetime th:dir th:draggable
th:dropzone th:enctype th:for
th:form th:formaction th:formenctype
th:formmethod th:formtarget th:frame
th:frameborder th:headers th:height
th:high th:href th:hreflang
th:hspace th:http-equiv th:icon
th:id th:keytype th:kind
th:label th:lang th:list
th:longdesc th:low th:manifest
th:marginheight th:marginwidth th:max
th:maxlength th:media th:method
th:min th:name th:optimum
th:pattern th:placeholder th:poster
th:preload th:radiogroup th:rel
th:rev th:rows th:rowspan
th:rules th:sandbox th:scheme
th:scope th:scrolling th:size
th:sizes th:span th:spellcheck
th:src th:srclang th:standby
th:start th:step th:style
th:summary th:tabindex th:target
th:title th:type th:usemap
th:value th:valuetype th:vspace
th:width th:wrap th:xmlbase
th:xmllang th:xmlspace  

 比如:

<form action="subscribe.html" th:action="@{/subscribe}">
<a href="product/list.html" th:href="@{/product/list}">Product List</a>

 这里使用了th:action和th:href标签。

 

Thymeleaf还提供了两个合成标签:

th:alt-title th:lang-xmllang

 用于同时设置两个属性,比如;

<img src="../../images/gtvglogo.png"
th:src="@{/images/gtvglogo.png}" th:alt-title="#{logo}" />

 还有两个CSS标签:

 

 th:classappend  th:styleappend

意思显而易见,用法如下:

<tr th:each="prod : ${prods}" class="row" th:classappend="${prodStat.odd}? 'odd'">

 

 

对于判断性的标签,比如checked

<input type="checkbox" name="option1" checked="checked" />

 只能使用checked作为其值,即使使用true都不好使。Thymeleaf提供了这些标签的等价标签,值可以是判断语句,不为真会删除该标签:

 

th:async th:autofocus th:autoplay
th:checked th:controls th:declare
th:default th:defer th:disabled
th:formnovalidate th:hidden th:ismap
th:loop th:multiple th:novalidate
th:nowrap th:open th:pubdate
th:readonly th:required th:reversed
th:scoped th:seamless th:selected

比如:

<input type="checkbox" name="active" th:checked="${user.active}" />

 

 

3
2
分享到:
评论

相关推荐

    Thymeleaf+Java Demo演示

    8. **标签库**:Thymeleaf有丰富的内置标签库,如`spring`标签库,可以方便地处理Spring MVC中的模型属性和表单验证。 这个"Thymeleaf+Java Demo演示"可能包含了以下步骤和内容: 1. **环境配置**:安装Thymeleaf...

    thymeleaf_3.0.5_中文参考手册.pdf

    Thymeleaf标准方言是指Thymeleaf的默认方言,它包含了用于模板表达式的语法和内置对象。Thymeleaf也允许创建自己的方言,以便在其上进行扩展。 Thymeleaf通过一种简单的语法来定义模板,使得模板可以被浏览器直接...

    eclipse的thymeleaf插件

    安装Thymeleaf插件的方法通常有两种:通过Eclipse的内置Marketplace搜索并安装,或者下载`.zip`文件后手动导入。安装完成后,重启Eclipse,就可以在HTML文件中享受Thymeleaf的专属支持了。 总的来说,Eclipse的...

    Thymeleaf常用功能.rar

    - **Thymeleaf表达式**(Thymeleaf Expressions):包括属性表达式(`th:`属性)、文本表达式(`[[...]]`)和注释表达式(`&lt;!--/*...*/--&gt;`)。它们可以用来访问模型对象、执行运算、处理字符串等。 - **上下文...

    thymeleaf使用模板

    开发过程中,Thymeleaf提供了一个内置服务器,可以在浏览器中实时预览模板效果,无需启动整个Spring Boot应用。 通过上述知识点,我们可以更好地理解和使用Thymeleaf模板引擎,构建出高效、灵活的Spring Boot应用...

    springboot+thymeleaf项目

    通过内置的Tomcat服务器和自动配置特性,开发者可以快速启动一个运行的Web应用。Spring Initializr是创建Spring Boot项目的一个便捷工具,通过选择依赖项,它能自动生成项目结构。 Thymeleaf则是一个强大的模板引擎...

    ems_thymeleaf.zip

    这里`${greeting}`是SpEL(Spring Expression Language)表达式,它会从模型对象中查找`greeting`属性的值。 Thymeleaf还支持国际化(i18n),你可以创建资源文件(如`messages.properties`或`messages_zh_...

    SpringBoot中整合Thymeleaf示例代码

    - 使用Thymeleaf的内置指令,如`th:switch`、`th:case`进行条件判断,`th:include`、`th:fragment`实现模板复用。 - 可以通过`th:object`指定一个对象,然后使用点号`.`访问其属性,如`${user.name}`。 6. **...

    thymeleaf参考手册

    13. **安全特性**:Thymeleaf有防止XSS攻击的内置机制,如自动转义输出,确保Web应用的安全。 这个“thymeleaf_3.0.5_中文参考手册.pdf”文件应包含以上所有内容的详细解释,包括每个特性的用法示例和最佳实践,是...

    thymeleaf_3.0.5_中英文参考手册.zip

    11. **表达式对象**:Thymeleaf提供了一些内置的对象,如`#ctx`(上下文对象)、`#vars`(变量对象)、`#dates`(日期/时间工具)等,方便在模板中进行计算和操作。 通过阅读“thymeleaf_3.0.5_中英文参考手册”的...

    thymeleaf 使用手册

    2. **配置 Thymeleaf**:通常情况下,Spring Boot 会自动配置 Thymeleaf,但如果需要自定义设置,可以通过 `spring.thymeleaf.*` 属性进行配置。 3. **创建模板文件**:模板文件通常保存在项目的特定目录下,如 `src...

    SpringBoot+thymeleaf

    5. **Model Attribute**:在控制器中,可以通过`model.addAttribute()`向模型添加属性,这些属性可以在Thymeleaf模板中访问。 6. **MVC配置**:虽然SpringBoot默认配置了Thymeleaf,但有时候可能需要自定义配置,...

    thymeleaf_3.0.5_中文参考手册

    7. **标准和Spring标准方言**:Thymeleaf有多个内置的方言,如标准方言(提供基本功能)和Spring标准方言(集成Spring MVC),后者提供了模型属性绑定、数据验证等特性。 8. **条件注解**:Thymeleaf允许使用`@th:...

    Thymeleaf简介.docx

    Thymeleaf作为一种强大的模板引擎,不仅提供了丰富的表达式语法和内置功能,还具备优秀的前后端协作体验。对于那些希望在项目中融合静态原型与动态数据展示的开发者来说,Thymeleaf无疑是一个非常理想的选择。

    SpringBoot整合Jpa和Thymeleaf实现CRUD

    1. **SpringBoot2**: SpringBoot是Spring框架的简化版,它内置了Tomcat服务器、自动配置功能和起步依赖,大大减少了项目的初始化工作。SpringBoot2带来了更多改进和新特性,例如更好的Actuator监控、健康检查和...

    springboot-thymeleaf素材.rar

    - Thymeleaf的`th:action`和`th:object`属性用于指定表单提交的URL和数据对象。 - 使用ModelAndView或Model对象在控制器和视图间传递数据。 6. **响应式设计**:考虑到不同设备的屏幕尺寸,可能包含响应式布局,...

    springboot+thymeleaf简单界面搭建.zip

    Thymeleaf支持在页面间传递参数,这通常通过URL查询参数、HTTP请求头或模型属性实现。例如,当用户点击一个链接或提交表单时,Thymeleaf可以将数据绑定到请求参数中,然后在目标页面上解码并显示这些参数。 6. **...

    spring boot+thymeleaf+mybatis+mysql

    然后,创建一个`application.properties`或`application.yml`文件,配置Spring Boot的属性,包括服务器端口、Thymeleaf模板路径、MyBatis的配置文件路径、数据库连接信息等。 2. 配置Thymeleaf:Thymeleaf模板文件...

Global site tag (gtag.js) - Google Analytics