Home  >  Article  >  Web Front-end  >  How to use EL expression to obtain context parameter value in JS

How to use EL expression to obtain context parameter value in JS

亚连
亚连Original
2018-05-28 15:55:352270browse

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 = &#39;${stringValue}&#39;;
		console.log(&#39;stringValue-------------&#39; + stringValue);
				
		var numberValue = ${numberValue};
		console.log(&#39;numberValue-------------&#39; + numberValue);
				
		var jsonStringValue = ${jsonStringValue};
		console.log(&#39;jsonValue---------------&#39; + jsonStringValue);
				
		var jsonStringValue1 = &#39;${jsonStringValue}&#39;;
		console.log(&#39;jsonValue1---------------&#39; + jsonStringValue1); 
				
		var objJsonString = &#39;${objJsonString}&#39;;
		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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn