在 JavaScript 中访问 Java/Servlet/JSP/JSTL/EL 变量 [英] Access Java / Servlet / JSP / JSTL / EL variables in JavaScript

查看:29
本文介绍了在 JavaScript 中访问 Java/Servlet/JSP/JSTL/EL 变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 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;,这可能会以bar is undefined"错误告终当您尝试在 JS 代码中进一步访问它时(您可以在浏览器的 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 wiki 页面 的底部附近,您可以找到如何创建自定义 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>

另见:

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆