将 servlet 变量传递给 javascript
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13222034/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Passing a servlet variable into javascript
提问by user1798546
I'm looking to pass a servlet variable, myVar that is passed into a JSP page, and pass it to JavaScript. The JavaScript is an external javascript that is included into the JSP page.
我希望传递一个 servlet 变量 myVar,它被传递到一个 JSP 页面,并将它传递给 JavaScript。JavaScript 是包含在 JSP 页面中的外部 javascript。
I have a button that calls the JavaScript function, but I'm unable to pass any of the variables that are passed into the JSP page through the servlet. The button is not a part of a form.
我有一个调用 JavaScript 函数的按钮,但是我无法传递任何通过 servlet 传递到 JSP 页面的变量。按钮不是表单的一部分。
I've tried in a function in JavaScript to call:
我尝试在 JavaScript 中调用一个函数:
var x = '<%=myVar%>';
AND
和
var x = '${myVar}';
AND
和
var x = '<%= (String)request.getParameter("myVar") %>';
However, x
is always a string of what I inputted.
但是,x
始终是我输入的字符串。
I'm not using AJAX or JQuery. Any ideas?
我没有使用 AJAX 或 JQuery。有任何想法吗?
Example Code is a simplified version: (so the button is actually a drop down that calls the js when I change the value, however, I want other variables that are not part of the drop down to be called in changeCLass)
示例代码是一个简化版本:(所以按钮实际上是一个下拉列表,当我更改值时调用 js,但是,我希望在 changeCLass 中调用不属于下拉列表的其他变量)
Servlet side:
服务端:
request.setAttribute("otherVars","tests");
JSP:
JSP:
<script type="text/javascript" src="external.js"></script>
<select name="vars" id="myVars" onchange="changeClass(this)">
<option value='1' selected="selected">1</option>
</select>
external.js included in JSP:
JSP 中包含的 external.js:
function changeClass(newVarX) {
var newVarId =newVarX.value;
var tID = '${otherVars}';
alert(newVarId + " " + tID);
}
Output: 1 $(otherVars}
输出:1 $(otherVars}
but output should be: 1 tests
但输出应该是:1 个测试
回答by Jan
This does not work, because your JavaScript file is not processed by the server. Possible solutions are:
这不起作用,因为您的 JavaScript 文件不是由服务器处理的。可能的解决方案是:
Declare the variable
tID
globally in the JSP file.JSP:
<script type="text/javascript" src="external.js"></script> <script type="text/javascript"> var tID = '${otherVars}'; </script> <select name="vars" id="myVars" onchange="changeClass(this)"> <option value='1' selected="selected">1</option> </select>
JavaScript (external.js):
function changeClass(newVarX) { var newVarId = newVarX.value; alert(newVarId + " " + tID); }
Have the JavaScript file also be processed. You may use a JSP file for the JavaScript and apply the correct content type:
JSP:
<script type="text/javascript" src="external.jsp"></script> <select name="vars" id="myVars" onchange="changeClass(this)"> <option value='1' selected="selected">1</option> </select>
JavaScript (external.jsp --> note, that it's a JSP file, too, but the content type is set to text/javascript):
<%@ page language="java" contentType="text/javascript; charset=UTF-8" pageEncoding="UTF-8"%> function changeClass(newVarX) { var newVarId = newVarX.value; var tID = '${otherVars}'; alert(newVarId + " " + tID); }
tID
在 JSP 文件中全局声明变量。JSP:
<script type="text/javascript" src="external.js"></script> <script type="text/javascript"> var tID = '${otherVars}'; </script> <select name="vars" id="myVars" onchange="changeClass(this)"> <option value='1' selected="selected">1</option> </select>
JavaScript (external.js):
function changeClass(newVarX) { var newVarId = newVarX.value; alert(newVarId + " " + tID); }
还要处理 JavaScript 文件。您可以为 JavaScript 使用 JSP 文件并应用正确的内容类型:
JSP:
<script type="text/javascript" src="external.jsp"></script> <select name="vars" id="myVars" onchange="changeClass(this)"> <option value='1' selected="selected">1</option> </select>
JavaScript(external.jsp --> 注意,它也是一个 JSP 文件,但内容类型设置为 text/javascript):
<%@ page language="java" contentType="text/javascript; charset=UTF-8" pageEncoding="UTF-8"%> function changeClass(newVarX) { var newVarId = newVarX.value; var tID = '${otherVars}'; alert(newVarId + " " + tID); }