将变量从JSP传递给Javascript [英] Passing variable from JSP to Javascript

查看:135
本文介绍了将变量从JSP传递给Javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道已经存在相关问题,但我不能简单地完成这项工作,我有一个带有java变量的JSP文件:

I know there is already questions about that, but I just can´t simply get this work, I have a JSP file with a java variable in it:

String test =Hello;

我需要在同一个JSP中嵌入的Javascript中读取这个值文件,我尝试了很多选项,但它不起作用,为了安全起见,我不想使用URL或隐藏值传递值。

And I need to read this value in the Javascript embedded in the same JSP file, I tried so many options, but it is not working, and for security I don't want pass the value using the URL or hidden values.

任何想法如何使这个工作?

Any ideas of how get this working?

推荐答案

我知道这个是旧的,但这对我有用:

I know this one is old, but this worked for me:

var javaScriptVar="<%out.print(javaString);%>";

你需要确保如果你使用的是外部js文件(在jsp文件中) ,上面的行必须在include脚本标记之前。例如,这是jsp文件:

you need to make sure that if you are using an external js file (out of the jsp file), the above line has to be before the "include" script tag. for example this is the jsp file:

var javaScriptVar="<%out.print(javaString);%>";
<script src="some_location/test.js"></script>

这篇关于将变量从JSP传递给Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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