springMVC整合thymeleaf

springMVC 整合thymeleaf

  1. 添加依赖

    <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="xml" cid="n5" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background: rgb(51, 51, 51); position: relative !important; padding: 10px 10px 10px 30px; width: inherit;">
    <dependency>
    <groupId>org.thymeleaf</groupId>
    <artifactId>thymeleaf</artifactId>
    <version>3.0.6.RELEASE</version>
    </dependency>

    <dependency>
    <groupId>org.thymeleaf</groupId>
    <artifactId>thymeleaf-spring4</artifactId>
    <version>3.0.6.RELEASE</version>
    </dependency></pre>

  2. 配置文件

    <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="xml" cid="n12" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background: rgb(51, 51, 51); position: relative !important; padding: 10px 10px 10px 30px; width: inherit;">
    <bean id="templateResolver" class="org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver">
    <property name="prefix" value="/WEB-INF/views/"/>
    <property name="suffix" value=".html"/>
    <property name="templateMode" value="HTML"/>
    <property name="cacheable" value="false"/>
    </bean>

    <bean id="templateEngine"
    class="org.thymeleaf.spring4.SpringTemplateEngine">
    <property name="templateResolver" ref="templateResolver"/>
    <property name="enableSpringELCompiler" value="true"/>
    </bean>

    <bean class="org.thymeleaf.spring4.view.ThymeleafViewResolver">
    <property name="templateEngine" ref="templateEngine"/>
    <property name="characterEncoding" value="utf-8"/>
    </bean></pre>

  3. 网页申明

    <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="html" cid="n18" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background: rgb(51, 51, 51); position: relative !important; padding: 10px 10px 10px 30px; width: inherit;"><!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:th="http://www.thymeleaf.org"></pre>

  4. 表达式符号

    1. "#"代表获取对象从 messages bundle 也就是消息资源本地化文件

      • 在${}中使用时,代表引用方法

      • <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="html" cid="n39" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background: rgb(51, 51, 51); position: relative !important; padding: 10px 10px 10px 30px; width: inherit;"><td th:text="${#dates.format(user.createTime,'yyyy-MM-dd')}">2012-12-10</td></pre>

    2. $ 表示从model里面获取

      • #、$这2个可以一起用 比如#{'system.'+${model.id}} 这相当于 #{system.01}的资源本地化文件中的system.01内容

      • 表示从当前对象中读取属性

        <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="html" cid="n56" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background: rgb(51, 51, 51); position: relative !important; padding: 10px 10px 10px 30px; width: inherit;"><div th:object="${session.user}">
        <p>Name: <span th:text="{firstName}">Sebastian</span>.</p>
        <p>Surname: <span th:text="
        {lastName}">Pepper</span>.</p>
        <p>Nationality: <span th:text="*{nationality}">Saturn</span>.</p>
        </div></pre>

    3. @{} 表示一个URL表达式

    4. ~{...} 表示引有页面的表达式

  5. 常用th标签

    • th:each="user: {users}" 相当于c:foreach 使用时候 奇偶行判断 <tr th:each="user,status:{users}" th:class="{status.odd}?'odd':'even'" > 显示行号 <td th:text="{status.index}+1">1</td>

    • th:href 重新生成url th:href="@{/list/details(id={user.id})} 生成:[http://domain.org/context/list/details?id=1](http://domain.org/context/list/details?id=1) 相对地址,使用"|"符号 th:href="|/list/detail?l={lid}|" 生成:http://domain.org/list/details?id=1 扩展 th:href="@{'/list/details/'+${user.id}} 生成 http://domain.org/context/list/details/1

    • th:text="{data}",将data的值替换该属性所在标签的body th:text="'my name is '+{user.name}"

    • th:with,定义变量 th:with="isEven=${prodStat.count}%2==0",定义多个变量可以用逗号分隔

    • th:attr,设置标签属性 多个属性可以用逗号分隔,比如th:attr="src=@{/image/aa.jpg},title=#{logo}"

    • th:if or th:unless,条件判断 支持布尔值,数字(非零为true),字符,字符串等

    • th:switch,th:case,选择语句

      • th:case="*"表示default case

      • 示例

      • <pre spellcheck="false" class="md-fences md-end-block ty-contain-cm modeLoaded" lang="html" cid="n181" mdtype="fences" style="box-sizing: border-box; overflow: visible; font-family: Monaco, Consolas, "Andale Mono", "DejaVu Sans Mono", monospace; margin-top: 0px; margin-bottom: 20px; font-size: 0.9rem; display: block; break-inside: avoid; text-align: left; white-space: normal; background: rgb(51, 51, 51); position: relative !important; padding: 10px 10px 10px 30px; width: inherit;"><div th:switch="${user.role}">
        <p th:case="'admin'">User is an administrator</p>
        <p th:case="#{roles.manager}">User is a manager</p>
        <p th:case="*">User is some other thing</p>
        </div></pre>

      th:fragment,th:include,th:substituteby:fragment为片段标记 指定一个模板内一部分代码为一个片段,然后在其它的页面中用th:include或

      th:substituteby进行包含。 片段格式 "templatename::fragname",指定模板内的指定片段 "templateName::[domselector]",指定模板的dom selector,被包含的模板内不需要th:fragment. ”templatename",包含整个模板 示例 th:include="footer::$(user.logined)?'logined':'notLogin'" 格式内可以为表达式 th:include和th:substituteby的区别 th:include 包含片段的内容到当前标签内 th:substituteby 用整个片段(内容和上一层)替换当前标签(不仅仅是标签内容) th:object 获取model中的对象

      <form action="#" style="box-sizing: border-box; margin-top: 0px; margin-bottom: 0px;">th:field 获取表单中绑定对象的字段 <input type="text" style="box-sizing: border-box !important; color: rgb(184, 191, 198); font-style: inherit; font-variant: inherit; font-weight: inherit; font-stretch: inherit; font-size: inherit; line-height: inherit; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; margin: 0px; background-color: var(--bg-color); padding-left: 4px; border: 1px solid transparent;">
      th:errors 显示页面中包含的错误* 显示username字段上的错误* 显示所有错误 其它请参考:thymeleaf所有标签属性.xlsx和thymeleaf-spring.pdf</form>

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。