Java 是否可以使用 jsp 变量值来初始化 JQUERY 变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2431084/
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
Is it possible to use jsp variable value to initialize JQUERY variable?
提问by Param-Ganak
I have some questions which are as follows:
我有一些问题如下:
How can I use the JSP variable/array in JQUERY code? Here what ever the JQUERY code we have is stored in separate .js file and that file is included in the JSP file.
Actually I want to initialize the JQUERY array with the JSP variable. So please guide me to achieve this task.
如何在 JQUERY 代码中使用 JSP 变量/数组?在这里,我们拥有的 JQUERY 代码存储在单独的 .js 文件中,并且该文件包含在 JSP 文件中。
实际上我想用 JSP 变量初始化 JQUERY 数组。所以请指导我完成这个任务。
采纳答案by Buhake Sindi
In Plain Old JSP
在普通的旧 JSP 中
<script>
var someText = "<%= myBean.getText() %>";
</script>
Using EL (Expression Language)
使用 EL(表达式语言)
<script>
var someText = "${myBean.text}";
</script>
Using Struts
使用 Struts
<script>
var someText = '<bean:write name="myBean" property="text" />';
</script>
Using JSTL
使用 JSTL
<script>
var someText = '<c:out value="${myBean.text}" />';
</script>
In essence, it's possible to populate Javascript objects from JSP. Don't forget that scriptlets and tags are just rendered back as HTML/XHTML, so JS cannot talk to tags and vice-versa.
本质上,可以从 JSP 填充 Javascript 对象。不要忘记,scriptlet 和标签只是作为 HTML/XHTML 呈现回来的,所以 JS 不能与标签对话,反之亦然。
回答by Thilo
Not directly. You'd have to set it into the page somewhere.
不直接。您必须将其设置到页面的某个位置。
<script>
var fromJsp = '${theVar}';
</script>
Note that this could get tricky for complex objects. Maybe JSON serialization can be your friend here.
请注意,对于复杂的对象,这可能会变得棘手。也许 JSON 序列化可以成为你的朋友。
Also note that this is one-way only. It is impossible to set the value of JSP variables from JavaScript (since the JavaScript runs client-side after the JSP has finished its work).
另请注意,这只是一种方式。不可能从 JavaScript 设置 JSP 变量的值(因为 JavaScript 在 JSP 完成其工作后运行客户端)。
回答by Padmarag
You can use this -
你可以用这个——
<input type="hidden" id="var1" value="<%=jspVar%>" />
and then use var1 in jQuery.
<input type="hidden" id="var1" value="<%=jspVar%>" />
然后在 jQuery 中使用 var1。
回答by BalusC
Java/JSP runs in webserver at the server machine and produces HTML/CSS/JS code. Server machine sends HTML/CSS/JS code to client machine. HTML/CSS/JS runs in webbrowser at client machine. Rightclick page and view source, you don't see any Java/JSP code.
Java/JSP 在服务器机器上的 webserver 中运行并生成 HTML/CSS/JS 代码。服务器机器将 HTML/CSS/JS 代码发送到客户端机器。HTML/CSS/JS 在客户端机器的 webbrowser 中运行。右键单击页面并查看源代码,您看不到任何 Java/JSP 代码。
JSP is a view technology which provides a template to write HTML/CSS/JS in and the ability to interact with backend Java data using taglibs/EL to control page flow and access data.
JSP 是一种视图技术,它提供了一个模板来编写 HTML/CSS/JS,并且能够使用 taglibs/EL 与后端 Java 数据交互以控制页面流和访问数据。
Whenever you want to let JavaScript access Java/JSP variables, all you need to do is to just write a Java variable as ifit is a JavaScript variable.
每当您想让 JavaScript 访问 Java/JSP 变量时,您只需要像编写JavaScript 变量一样编写 Java变量即可。
<script>var foo = '${bean.foo}';</script>
Is an excellent example. Note that those quotes are required for JavaScript itself, not for JSP/EL. Imagine that ${bean.foo}
returns bar
, then the generated HTML/CSS/JS page which arrived at the client side would end up looking like:
是极好的例子。请注意,这些引号是 JavaScript 本身所必需的,而不是 JSP/EL 所必需的。想象一下,${bean.foo}
返回bar
,然后生成的 HTML/CSS/JS 页面到达客户端最终看起来像:
<script>var foo = 'bar';</script>
Whenever you want to let Java/JSP access JavaScript variables, all you need to do is to let JavaScript fire a (XML)HTTP request. More background info and examples can be found in this article.
每当您想让 Java/JSP 访问 JavaScript 变量时,您所需要做的就是让 JavaScript 触发 (XML)HTTP 请求。更多背景信息和示例可以在这篇文章中找到。