接着来
原文地址:http://www.javabeat.net/set-attributes-thymeleaf-template-using-thattr/
下面是主题:
如何用th:attr标签在模板中设置属性?
在前面的文章中,我们已经说过了 hello world example和expression 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. 运行应用
你会看到下面这个效果