在 JavaScript 中访问 Java/Servlet/JSP/JSTL/EL 变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3832792/
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
Access Java / Servlet / JSP / JSTL / EL variables in JavaScript
提问by Reddy
I have a form in JSP. I have to populate it based on the request object (from the servlet). How do I use Java Script for accessing request object attributes or if you can suggest me any other better way to populate form dynamically?
我在 JSP 中有一个表单。我必须根据请求对象(来自 servlet)填充它。如何使用 Java Script 访问请求对象属性,或者您是否可以建议我任何其他更好的动态填充表单的方法?
回答by BalusC
You need to realize that Java/JSP is merely a HTML/CSS/JS code producer. So all you need to do is to just let JSP print the Java variable as if it is a JavaScript variable and that the generated HTML/JS code output is syntactically valid.
您需要意识到 Java/JSP 只是一个 HTML/CSS/JS 代码生产者。所以你需要做的就是让 JSP 打印 Java 变量,就好像它是一个 JavaScript 变量一样,并且生成的 HTML/JS 代码输出在语法上是有效的。
Provided that the Java variable is available in the EL scope by ${foo}
, here are several examples how to print it:
假设 Java 变量在 EL 作用域中可用,以下${foo}
是如何打印它的几个示例:
<script>var foo = '${foo}';</script>
<script>someFunction('${foo}');</script>
<div onclick="someFunction('${foo}')">...</div>
Imagine that the Java variable has the value "bar"
, then JSP will ultimately generate this HTML which you can verify by rightclick, View Sourcein the webbrowser:
假设 Java 变量的值为"bar"
,那么 JSP 最终会生成这个 HTML,您可以通过右键单击,在 webbrowser 中查看源代码来验证:
<script>var foo = 'bar';</script>
<script>someFunction('bar');</script>
<div onclick="someFunction('bar')">...</div>
Do note that those singlequotes are thus mandatory in order to represent a string typed variable in JS. If you have used var foo = ${foo};
instead, then it would print var foo = bar;
, which may end up in "bar is undefined" errors in when you attempt to access it further down in JS code (you can see JS errors in JS console of browser's web developer toolset which you can open by pressing F12 in Chrome/FireFox23+/IE9+). Also note that if the variable represents a number or a boolean, which doesn't need to be quoted, then it will just work fine.
请注意,这些单引号因此是强制性的,以便在 JS 中表示字符串类型的变量。如果您var foo = ${foo};
改用了,那么它会打印var foo = bar;
,当您尝试在 JS 代码中进一步访问它时,它可能最终会出现“bar is undefined”错误(您可以在浏览器的 Web 开发人员工具集的 JS 控制台中看到 JS 错误)可以在 Chrome/FireFox23+/IE9+ 中按 F12 打开)。另请注意,如果变量表示不需要引用的数字或布尔值,则它会正常工作。
If the variable happens to originate from user-controlled input, then keep in mind to take into account XSS attack holesand JS escaping. Near the bottom of our EL wiki pageyou can find an example how to create a custom EL function which escapes a Java variable for safe usage in JS.
如果变量恰好来自用户控制的输入,那么请记住考虑XSS 攻击漏洞和JS 转义。在我们的 EL wiki 页面底部附近,您可以找到如何创建自定义 EL 函数的示例,该函数转义 Java 变量以在 JS 中安全使用。
If the variable is a bit more complex, e.g. a Java bean, or a list thereof, or a map, then you can use one of the many available JSON librariesto convert the Java object to a JSON string. Here's an example assuming Gson.
如果变量有点复杂,例如 Java bean,或其列表,或映射,那么您可以使用许多可用的JSON 库之一将 Java 对象转换为 JSON 字符串。这是一个假设 Gson 的例子。
String someObjectAsJson = new Gson().toJson(someObject);
Note that this way you don't need to print it as a quoted string anymore.
请注意,通过这种方式,您不再需要将其打印为带引号的字符串。
<script>var foo = ${someObjectAsJson};</script>
See also:
也可以看看:
回答by Steve Perkins
If you're pre-populating the form fields based on parameters in the HTTP request, then why not simply do this on the server side in your JSP... rather than on the client side with JavaScript? In the JSP it would look vaguely like this:
如果您根据 HTTP 请求中的参数预先填充表单字段,那么为什么不在 JSP 中的服务器端执行此操作...而不是在客户端使用 JavaScript 执行此操作?在 JSP 中,它看起来像这样:
<input type="text" name="myFormField1" value="<%= request.getParameter("value1"); %>"/>
On the client side, JavaScript doesn't really have the concept of a "request object". You pretty much have to parse the query string yourself manually to get at the CGI parameters. I suspect that isn't what you're actually wanting to do.
在客户端,JavaScript 并没有真正的“请求对象”的概念。您几乎必须自己手动解析查询字符串才能获得 CGI 参数。我怀疑这不是你真正想要做的。
回答by Raj Kundalia
In JSP file:
在 JSP 文件中:
<head>
...
<%@ page import="com.common.Constants" %>
...
</head>
<script type="text/javascript">
var constant = "<%=Constants.CONSTANT%>"
</script>
This constant variable will be then available to .js files that are declared after the above code.
然后,此常量变量将可用于在上述代码之后声明的 .js 文件。
Constants.java is a java file containing a static constant named CONSTANT.
Constants.java 是一个包含名为 CONSTANT 的静态常量的 java 文件。
The scenario that I had was, I needed one constant from a property file, so instead of constructing a property file for javascript, I did this.
我遇到的情况是,我需要一个属性文件中的一个常量,所以我没有为 javascript 构建一个属性文件,而是这样做了。
回答by Parth Patel
In JSP page :
在 JSP 页面中:
<c:set var="list_size" value="${list1.size() }"></c:set>
Access this value in Javascipt page using :
使用以下命令在 Javascipt 页面中访问此值:
var list_size = parseInt($('#list_size').val());
I added javascript page in my project externally.
我在外部项目中添加了 javascript 页面。