如何用th:attr标签在thymeleaf模板中设置属性-原标题:How To Set Attributes in Thymeleaf Template using th:attr?

接着来

原文地址:http://www.javabeat.net/set-attributes-thymeleaf-template-using-thattr/

下面是主题:

如何用th:attr标签在模板中设置属性?

在前面的文章中,我们已经说过了 hello world exampleexpression language表达式语言。这个指导文档说明了在处理过程中,如何在模板中如何设置属性。这个特性对于创建模板很有用。thymeleaf提供了非常简洁的标记——通过在properties文件或context变量中动态地添加属性。

1. 安装应用

和上一次一样,还是使用hello world的例来改。

2. 用th:attr创建模板

如果你看过之前指导文档中的模板,模板中的th:attr已经被模板引擎替换成了动态的值。有一点值得注意的是,有很多属性和很多特性来支持设置属性值。这里我介绍一个非常简单的例子。

attributes.html

<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-4.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
	xmlns:th="http://www.thymeleaf.org">
<head>
<title>How to set Attribute Values!!</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p><b>Use th:attr</b></p>
  <fieldset>
    <input type="text" name="email" th:attr="class=#{input.class}"/>
    <input type="submit" value="Subscribe me!" th:attr="value=#{input.submit}"/>
  </fieldset>
  <a href="register.html" th:attr="href=@{/LoginRegister.html}">Register Here</a>
<p><b>Use Specific Attributes</b></p>
  <fieldset>
    <input type="text" name="email" th:class="#{input.class}"/>
    <input type="submit" value="Subscribe me!" th:value="#{input.submit}"/>
  </fieldset>
  <a href="register.html" th:href="@{/LoginRegister.html}">Register Here</a>
<p><b>Append Using th:attrappend</b></p>
  <fieldset>
    <input type="text" name="email" class="input" th:attrappend="class=' ' + #{input.class}"/>
    <input type="submit" value="Subscribe me!" th:attr="value=#{input.submit}"/>
  </fieldset>
  <a href="register.html" th:attr="href=@{/LoginRegister.html}">Register Here</a>

</body>
</html>

3. 创建带有thymeleaf上下文和模板的servlet



ThymeleafSetAttributeExample.java

package javabeat.net.thymeleaf;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.TreeSet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.WebContext;
import org.thymeleaf.templateresolver.ServletContextTemplateResolver;

public class ThymeleafSetAttributeExample extends HttpServlet{
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
    	ServletContextTemplateResolver templateResolver = new ServletContextTemplateResolver();
        // XHTML is the default mode, but we will set it anyway for better understanding of code
        templateResolver.setTemplateMode("XHTML");
        templateResolver.setPrefix("/WEB-INF/");
        templateResolver.setSuffix(".html");
        templateResolver.setCacheTTLMs(3600000L);
        TemplateEngine templateEngine = new TemplateEngine();
        templateEngine.setTemplateResolver(templateResolver);
        WebContext ctx = new WebContext(req, resp, getServletConfig().getServletContext(), req.getLocale());

        // This will be prefixed with /WEB-INF/ and suffixed with .html
        templateEngine.process("attribute", ctx, resp.getWriter());
        resp.setContentType("text/html;charset=UTF-8");
        resp.setHeader("Pragma", "no-cache");
        resp.setHeader("Cache-Control", "no-cache");
        resp.setDateHeader("Expires", 1000);
	}
}

4. 创建properties文件

attributes_en.properties

input.class=inputstyle
input.submit=Form Submit
input.href=LoginRegister.html


5. 运行应用

你会看到下面这个效果

set-attribute-thymeleaf




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值