Js和Thymeleaf如何獲取model中的值

簡述

在大多數的項目架構中,使用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>
相關文章
相關標籤/搜索