Js和Thymeleaf如何获取model中的值

一、Jquery获取Model中的数据

1、将model中的值赋给hidden,然后Js获取隐藏域的值。

后台的实现:

@RequestMapping("/QEditorMod1")
    public String QEditorMod1(ModelMap model){
        model.addAttribute("staff_name","cxx" ); return "questionEditorTemplate/QEditorMod1"; } 

前端值的获取

//将值赋给hidden域
<input type="hidden" th:value="${staff_name}" id="staff_name2"/> //Js 获取hidden的隐藏域 var staff_name2=$("#staff_name2").val(); 

二、Thymeleaf 获取model中的值

2、访问model中的数据

//通过“${}”访问model中的属性

<div class="panel-body">
         <span th:text="${singlePerson.name}"></span> </div> 

3、在javascript中访问model 目前没有发现此种方法的应用场景

<script th:inline="javascript">
        var single = [[${singlePerson}]];
    //或(如果第一种方式不行使用第二种)
    //var single = '[[${singlePerson.name}]]'; console.log(single.name+"/"+single.age) </script>

来源1https://my.oschina.net/u/3421984/blog/1604188/
来源2https://blog.csdn.net/qq_29072049/article/details/88642146

猜你喜欢

转载自www.cnblogs.com/wwct/p/12143084.html