前段页面代码
<tr th:each="book : ${books}">
<td th:text="${book.author}"></td>
<td th:text="${book.title}"></td>
<td th:text="${book.url}"></td>
<script th:inline="javascript">//一定要加上这句
function localRefresh() {
// 装载局部刷新返回的页面
$('#table_refresh2').load("/local/"+[[${book.ID}]]);//这里直接可以使用thymeleaf中的变量,通过内联样式
}
</script>
</tr>
最主要就是加上thymeleaf与js结合的语句,常见的thymeleaf内联语句有三种
<script th:inline="javascript"></script>
用于js的
<span th:text="${session.user.name}">Sebastian</span>
用于普通html,任意位置都可以。默认也可以省略不写,thymeleaf也是支持的。
<body th:inline="text">
<p>Hello, [[${session.user.name}]]!</p>
</body>
直接用于父标签,子标签可以直接使用
后端代码示例
@Controller
@RequestMapping("/refresh")
public class IndexController {
@RequestMapping
public String globalRefresh(Model model) {
List<Map<String,String>> lists = new ArrayList<>();
Map<String,String> map = new HashMap<>();
map.put("author", "曹雪芹");
map.put("title", "《红楼梦》");
map.put("url", "www.baidu.com");
map.put("ID","2");
lists.add(map);
model.addAttribute("books", lists);
return "test";
}
}