The content of this article is about the analysis of two cases of using EL expressions in JavaScript to obtain parameters passed in the background. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you. help.
The problem I encountered in the project today was that the jsp page referenced a separate js file, but some parameters passed in the background directly used EL expressions to obtain errors in the js file. I searched for information online and found a solution. Make a summary for reference.
There are two situations when using EL expressions in JS to obtain parameters passed in the background.
The first situation:
(1)Introduce the jstl tag library into the jsp page:
<%@ taglib uri=”http://java.sun.com/jsp/jstl/core” prefix=”c”%>
(2) Write js code on the jsp page:
<script> var merchantId=” ${sessionScope.merchantIdMap}” </script>
Second case :
(1) Introduce jstl tags into the jsp page Library:
<%@ taglib uri=”http://java.sun.com/jsp/jstl/core” prefix=”c”%>
(2) The jsp file introduces a separate js file. At this time, EL expressions cannot be used directly in the js file, and an error will be reported. There are two processing methods here:
The first:
Use hidden fields to store parameter values in jsp pages:
<input type=”hidden” id=merchantIdMap” value=”${sessionScope.merchantIdMap}”>
Get the value of this hidden field in js files:
var merchantId=$(“#merhantIdMap”);
Second option:
Define global variables in the jsp page:
<c:set var="merchantIdMapGlob" value="${sessionScope.merchantIdMap}" scope="application"/> <c:set var="warehouseIdMapGlob" value="${sessionScope.warehouseIdMap}" scope="application"/> <script> var merchantIdMapGlob = "${merchantIdMapGlob}"; var warehouseIdMapGlob = "${warehouseIdMapGlob}"; </script>
You can use this global variable directly in the js file:
var merchantId = merchantIdMapGlob;
Related recommendations:
javaScript several ways to use EL expression_javascript skills
The above is the detailed content of Analysis of two cases of using EL expressions in javascript to obtain parameters passed in the background. For more information, please follow other related articles on the PHP Chinese website!