Below I will share with you a method of using EL expressions to obtain context parameter values in JS. It has a good reference value and I hope it will be helpful to everyone.
1. Action returns parameters to the page
/** * 测试js中获取后台传值 * @param model * @param req * @return String */ @RequestMapping("getValue") public String getValue(Model model, HttpServletRequest req){ model.addAttribute("stringValue", "测试在js中取值..."); model.addAttribute("numberValue", 111); List<String> list = new ArrayList<String>(); list.add("aaa"); list.add("bbb"); list.add("ccc"); model.addAttribute("arrayValue", list); model.addAttribute("jsonStringValue", JSON.toJSONString(list)); User user = new User(); user.setUserID("1"); user.setUserName("test"); user.setMobile("13800000000"); user.setEmail("test@163.com"); user.setNickName("test"); model.addAttribute("objJsonString", JSON.toJSONString(user)); return "/getValue.htm"; }
2. Use EL expression in js to obtain parameter value
<script type="text/javascript"> $(function(){ var stringValue = '${stringValue}'; console.log('stringValue-------------' + stringValue); var numberValue = ${numberValue}; console.log('numberValue-------------' + numberValue); var jsonStringValue = ${jsonStringValue}; console.log('jsonValue---------------' + jsonStringValue); var jsonStringValue1 = '${jsonStringValue}'; console.log('jsonValue1---------------' + jsonStringValue1); var objJsonString = '${objJsonString}'; console.log("objJsonString------------------- " + objJsonString); var obj = JSON.parse(objJsonString); console.log("userName ------------ " + obj.userName); }); </script>
Remarks: To get numeric parameter values, EL expressions in js do not need to be quoted; to get string parameter values, EL expressions in js need to be quoted; object and collection type parameter values need to be converted using JSON.toJSONString() in the background.
The above is what I compiled for everyone. I hope it will be helpful to everyone in the future.
Related articles:
java jquery method of processing xml data
Server-side configuration to implement AJAX cross-domain request
#Ajax get request cache processing solution
The above is the detailed content of How to use EL expression to obtain context parameter value in JS. For more information, please follow other related articles on the PHP Chinese website!