简述
在大多数的项目架构中,使用SPringBoot发布微服务,前端采用Thymeleaf做为Html模版,使用Jquery做为动态脚本,那么Thymeleaf和Jquery是如何获取Model中的数据呢?javascript
Jquery获取Model中的数据
方法1:将model中的值赋给hidden,而后Js获取隐藏域的值。
后台的实现:前端
@RequestMapping("/QEditorMod1") public String QEditorMod1(ModelMap model){ model.addAttribute("staff_name","cxx" ); return "questionEditorTemplate/QEditorMod1"; }
前端值的获取java
//将值赋给hidden域 <input type="hidden" th:value="${staff_name}" id="staff_name2"/> //Js 获取hidden的隐藏域 var staff_name2=$("#staff_name2").val();
Thymeleaf 获取model中的值
二、访问model中的数据架构
//经过“${}”访问model中的属性 <div class="panel-body"> <span th:text="${singlePerson.name}"></span> </div>
三、在javascript中访问model 目前没有发现此种方法的应用场景app
<script th:inline="javascript"> var single = [[${singlePerson}]]; console.log(single.name+"/"+single.age) </script>