前端js,jQ獲取後端(ModelAndView)的數據

一、前端js,jQ獲取後端(ModelAndView)的數據

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}]];
    //或(如果第一種方式不行使用第二種)
        console.log(single.name+"/"+single.age)
 </script>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章