在JavaScript中访问Java / Servlet / JSP / JSTL / EL变量 [英] Access Java / Servlet / JSP / JSTL / EL variables in JavaScript
问题描述
我在JSP中有一个表单。我必须根据请求对象(来自servlet)填充它。如何使用Java Script访问请求对象属性,或者如果您可以建议我以其他更好的方式动态填充表单?
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?
推荐答案
您需要意识到Java / JSP仅仅是一个HTML / CSS / JS代码生成器。所以你需要做的就是让JSP打印Java变量,好像它是一个JavaScript变量,并且生成的HTML / JS代码输出在语法上是有效的。
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.
如果通过 $ {foo}
在EL范围内提供Java变量,以下是几个如何打印它的示例:
Provided that the Java variable is available in the EL scope by ${foo}
, here are several examples how to print it:
<script>var foo = '${foo}';</script>
<script>someFunction('${foo}');</script>
<div onclick="someFunction('${foo}')">...</div>
想象一下,Java变量的值为bar
,然后JSP将最终生成这个HTML,您可以通过右键单击验证,在webbrowser中查看源:
Imagine that the Java variable has the value "bar"
, then JSP will ultimately generate this HTML which you can verify by rightclick, View Source in the webbrowser:
<script>var foo = 'bar';</script>
<script>someFunction('bar');</script>
<div onclick="someFunction('bar')">...</div>
请注意,这些单引号因此是强制性的,以便在JS中表示字符串类型变量。如果您使用 var foo = $ {foo};
,则会打印 var foo = bar;
,当您尝试在JS代码中进一步访问时,可能会出现bar is undefined错误(您可以在浏览器的Web开发人员工具集的JS控制台中看到JS错误,您可以通过在Chrome / FireFox23 + / IE9 +中按F12打开它)。另请注意,如果变量表示数字或布尔值(不需要引用),那么它就可以正常工作。
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.
如果变量恰好起源从用户控制的输入,然后记住考虑到 XSS攻击漏洞和 JS转义。在我们的EL维基页面的底部附近,您可以找到一个示例,了解如何创建一个转义Java变量的自定义EL函数JS中的安全使用。
If the variable happens to originate from user-controlled input, then keep in mind to take into account XSS attack holes and JS escaping. Near the bottom of our EL wiki page you can find an example how to create a custom EL function which escapes a Java variable for safe usage in JS.
如果变量有点复杂,例如Java bean或其列表或地图,然后您可以使用许多可用的 JSON库之一进行转换Java对象为JSON字符串。这是假设Gson的一个例子。
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 libraries to convert the Java object to a JSON string. Here's an example assuming 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>
参见:
- 我们的JSP wiki页面 - 请参阅JavaScript一章。
- 如何在JSP中转义JavaScript?
- 调用Servlet并从JavaScript调用Java代码以及参数
- 如何使用Servlets和Ajax?
- Our JSP wiki page - see the chapter "JavaScript".
- How to escape JavaScript in JSP?
- Call Servlet and invoke Java code from JavaScript along with parameters
- How to use Servlets and Ajax?
See also:
这篇关于在JavaScript中访问Java / Servlet / JSP / JSTL / EL变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!