springMVC中通過ModelAndView進行后臺與頁面的數(shù)據(jù)交互,那么如何在頁面中獲取ModelAndView綁定的值呢?
1、在JSP中通過EL表達式進行獲取(比較常用)
后臺:
1
2
|
ModelAndView model = new ModelAndView(); model.addObject("name","Jims"); |
JSP:在JSP中直接使用${name }進行獲取
1
|
姓名:${name } |
2、通過JSP內(nèi)置對象進行獲取
后臺:
1
2
|
ModelAndView model = new ModelAndView(); model.addObject("name","Jims"); |
前臺:
1
|
<% String name = request.getAttribute("name"); %> |
以上兩種方式是在JSP中獲取ModelAndView綁定的值,那么如何在頁面的JS中獲取ModelAndView綁定的值呢?
1、最麻煩的一種:
后臺:
1
2
|
ModelAndView model = new ModelAndView(); model.addObject("name","Jims"); |
JSP:
1
|
< input type = "hidden" value = "${name }" id = "method1" > |
JS:
1
|
var name = $("#method1").val(); |
2、和第一種類似:
后臺:
1
2
|
ModelAndView model = new ModelAndView(); model.addObject("name","Jims"); |
JSP:
1
|
<% String name=request.getAttribute("name"); %> |
JS:
1
|
var name='<%=name %>'; |
3、第三種比較簡單也較為常用一些
后臺:
1
2
|
ModelAndView model = new ModelAndView(); model.addObject("name","Jims"); |
JS:
1
|
var name = '${name}'; |
這樣子便可以 直接獲取到在ModelAndView中綁定的name屬性
以上這篇淺談在頁面中獲取到ModelAndView綁定的值方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://blog.csdn.net/yhj19920417/article/details/72824406