Home > Web Front-end > JS Tutorial > How EL gets context parameter values ​​(code attached)

How EL gets context parameter values ​​(code attached)

php中世界最好的语言
Release: 2018-04-28 10:23:12
Original
1456 people have browsed it

This time I will show you how EL obtains the context parameter value (with code). What are the precautions for EL to obtain the context parameter value? The following is a practical case, let's take a look.

1. actionReturn 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";
}
Copy after login

2. Use EL in js ExpressionGet the 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>
Copy after login

Remarks: Get the numeric parameter value, not used in EL expressions in js Add quotation marks; take the string type parameter value. EL expressions in js need to be quoted; object and collection type parameter values ​​need to be converted using JSON.toJSONString() in the background.

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

VUE extends ElTableColumn

Detailed explanation of the use of watch method in Vue

Vue.js implements custom login form

The above is the detailed content of How EL gets context parameter values ​​(code attached). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template