数据展示
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> </body> </html>
普通的Java字符串
model.addAttribute("javaString", "simple java string");
<p th:text="${javaString}" />
HTML代码字符串
model.addAttribute("labelString","<span style='color:red'>Jerry</span>");
<p th:utext="${labelString}" />
Java对象
Demo demo = new Demo("弦乐", 12, new Date()); model.addAttribute("demo", demo);
<div>显示对象:方式一</div> <div> <p th:text="'字符串:'+${demo.stringField}" /> <p th:text="'整数:'+${demo.intField}" /> <p th:text="'日期:'+${#dates.format(demo.dateField,'yyyy-MM-dd')}" /> </div> <br /> <div>显示对象:方式二</div> <div th:object="${demo}"> <p th:text="'字符串:'+*{stringField}" /> <p th:text="'整数:'+*{intField}" /> <p th:text="'日期:'+*{#dates.format(dateField,'yyyy-MM-dd')}" /> </div>
List<String>
List<String> stringList = new ArrayList<>(4); stringList.add("huang0"); stringList.add("chen"); stringList.add("hello"); stringList.add("xian"); model.addAttribute("stringList",stringList);
<div>遍历List-str</div> <table border="1"> <thead> <th>value</th><th>odd</th><th>even</th><th>count</th><th>current</th><th>first</th><th>index</th><th>last</th><th>size</th> </thead> <tbody> <tr th:each="s,l:${stringList}" > <td th:text="${s}"/> <td th:text="${l.odd}"/> <td th:text="${l.even}"/> <td th:text="${l.count}"/> <td th:text="${l.current}"/> <td th:text="${l.first}"/> <td th:text="${l.index}"/> <td th:text="${l.last}"/> <td th:text="${l.size}"/> </tr> </tbody> </table>
List<Object>
List<Demo> demoList = new ArrayList<>(3); demoList.add(demo); demoList.add(demo); model.addAttribute("demoList", demoList);
<div>遍历List-obj</div> <table border="1"> <thead> <th>stringField</th><th>intField</th><th>stringField</th> <th>odd</th><th>even</th><th>count</th><th>current</th><th>first</th><th>index</th><th>last</th><th>size</th> </thead> <tbody> <tr th:each="d,l:${demoList}" > <td th:text="${d.stringField}"/> <td th:text="${d.intField}"/> <td th:text="*{#dates.format(d.dateField,'yyyy-MM-dd')}"/> <td th:text="${l.odd}"/> <td th:text="${l.even}"/> <td th:text="${l.count}"/> <td th:text="${l.current}"/> <td th:text="${l.first}"/> <td th:text="${l.index}"/> <td th:text="${l.last}"/> <td th:text="${l.size}"/> </tr> </tbody> </table>
Set<String>
Set<String> stringSet = new HashSet<>(); stringSet.add("huang0"); stringSet.add("chen"); stringSet.add("hello"); stringSet.add("xian"); model.addAttribute("stringSet", stringSet);
<div>遍历Set-str</div> <table border="1"> <thead> <th>value</th><th>odd</th><th>even</th><th>count</th><th>current</th><th>first</th><th>index</th><th>last</th><th>size</th> </thead> <tbody> <tr th:each="s,l:${stringSet}" > <td th:text="${s}"/> <td th:text="${l.odd}"/> <td th:text="${l.even}"/> <td th:text="${l.count}"/> <td th:text="${l.current}"/> <td th:text="${l.first}"/> <td th:text="${l.index}"/> <td th:text="${l.last}"/> <td th:text="${l.size}"/> </tr> </tbody> </table>
Set<Object>
Set<Demo> demoSet = new HashSet<>(); demoSet.add(demo); demoSet.add(new Demo("hello", 22, new Date()));
model.addAttribute("demoSet", demoSet);
<div>遍历Set-obj</div> <table border="1"> <thead> <th>stringField</th><th>intField</th><th>stringField</th> <th>odd</th><th>even</th><th>count</th><th>current</th><th>first</th><th>index</th><th>last</th><th>size</th> </thead> <tbody> <tr th:each="d,l:${demoSet}" > <td th:text="${d.stringField}"/> <td th:text="${d.intField}"/> <td th:text="*{#dates.format(d.dateField,'yyyy-MM-dd')}"/> <td th:text="${l.odd}"/> <td th:text="${l.even}"/> <td th:text="${l.count}"/> <td th:text="${l.current}"/> <td th:text="${l.first}"/> <td th:text="${l.index}"/> <td th:text="${l.last}"/> <td th:text="${l.size}"/> </tr> </tbody> </table>
Map<String, String>
Map<String, String> stringMap = new HashMap<>(3); stringMap.put("name", "world"); stringMap.put("sex", "1"); stringMap.put("age", "12"); model.addAttribute("stringMap", stringMap);
<div>遍历Map-str-str</div> <table border="1"> <thead> <th>key</th><th>value</th> <th>odd</th><th>even</th><th>count</th><th>current</th><th>first</th><th>index</th><th>last</th><th>size</th> </thead> <tbody> <tr th:each="e,l:${stringMap}" > <td th:text="${e.key}"/> <td th:text="${e.value}"/> <td th:text="${l.odd}"/> <td th:text="${l.even}"/> <td th:text="${l.count}"/> <td th:text="${l.current}"/> <td th:text="${l.first}"/> <td th:text="${l.index}"/> <td th:text="${l.last}"/> <td th:text="${l.size}"/> </tr> </tbody> </table>
Map<String, Object>
Map<String, Demo> demoMap = new HashMap<>(2); demoMap.put("demo01", demo); demoMap.put("demo02", new Demo("hello", 22, new Date())); model.addAttribute("demoMap", demoMap);
<div>遍历Map-str-obj</div> <table border="1"> <thead> <th>key</th><th>stringField</th><th>intField</th><th>stringField</th> <th>odd</th><th>even</th><th>count</th><th>current</th><th>first</th><th>index</th><th>last</th><th>size</th> </thead> <tbody> <tr th:each="e,l:${demoMap}" > <td th:text="${e.key}"/> <td th:text="${e.value.stringField}"/> <td th:text="${e.value.intField}"/> <td th:text="*{#dates.format(e.value.dateField,'yyyy-MM-dd')}"/> <td th:text="${l.odd}"/> <td th:text="${l.even}"/> <td th:text="${l.count}"/> <td th:text="${l.current}"/> <td th:text="${l.first}"/> <td th:text="${l.index}"/> <td th:text="${l.last}"/> <td th:text="${l.size}"/> </tr> </tbody> </table>