页面中放入一个label,点击测试按钮后刷新label内容
<label id="test" th:text="${date}"/>
<button id="btn">测试</button>
在
$("#btn").click(function () {
$.ajax({
url:"/role/event-count",
type:"GET",
success:function(result){
$("#test").replaceWith(result);
}
});
});
后台代码接收
//测试ajax和thymeleaf配合
@RequestMapping(value="/event-count", method=RequestMethod.GET)
public String getEventCount(ModelMap map) {
// TODO: 在此处检索新值,以便将其添加到模型映射
SimpleDateFormat dateFormat = new SimpleDateFormat();
String s = dateFormat.format(new Date());
map.addAttribute("date", s);
// 将下方view改成自己需要传回的页面
return "/view :: #test";
}
结果:未点击测试按钮时
点击后:
显示发送的为ajax请求