一。从controller中传值到js页面,是不能像传到jsp页面那样直接接收,而要采用间接的传值:
1。由uil,请求: localhost:+'/bdmap?lat='+point.lat+'&lng='+point.lng;
2。controller中接收:
@GetMapping("bdmap")
public ModelAndView police(@RequestParam Map<String, Object> map ){
return new ModelAndView("mapBD/list").addObject("map", map);
}
3。页面间接接收:
需要在html中引入:<html xmlns:th="http://www.thymeleaf.org">
<body>
<div style=display:none;>
<input id="map_lat" th:value="${map.lat}" />
<input id="map_lng" th:value="${map.lng}" />
</div>
</body>
4。在js中获取控件的值:
var maplat = $("#map_lat").val();
var maplng = $("#map_lng").val();
if( maplat == "undefined" || maplng == "undefined"){
}
注意:在html中获取的值为空时,在js中取到的值时“undefined”,而不是undefined。
二,jsp中的js与加载的js文件中的js运行的先后次序与js文件加载的位置有关,在前的先运行。
三,jsp中变量与js文件中变量,若js中要用到jsp中文件的变量,则必须在js文件之前先运行并且要设为公用变量,反过来,如果jsp中要使用js中的文件变量,则要等到js文件加载完菜能使用。
如:
<script>
var plate = "<%=plate%>";
</script>
<script type="text/javascript" src="<%=request.getContextPath()%>/js/appraise.js">
</script>
则,appraise需等上面代码运行完后再加载,所以apprraise.js中可使用plate变量。
<script type="text/javascript" src="<%=request.getContextPath()%>/js/appraise.js">
</script>
<script>
var plate = "<%=plate%>";
</script>
则,appraise在上面代码运行之前就加载了,所以apprraise.js中不可使用plate变量。