如何使用jsp将java数组传输到javaScript数组?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/516565/
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
How to transfer java array to javaScript array using jsp?
提问by kgrad
I have a list of strings on my server which I am trying to get to the client in the form of an array. The code I am attempting to use is the following:
我的服务器上有一个字符串列表,我试图以数组的形式访问客户端。我尝试使用的代码如下:
Within the jsp I have a List<String>
column
在jsp中我有一List<String>
列
I am attempting the following code:
我正在尝试以下代码:
<%int j = 0; %>
for(var i = 0; i < <%=columns.size()%>; i++)
{
colArray[i] = "<%=columns.get(j++)%>";
}
This code simply returns the first element in the columns list for every element in the colArray.
此代码只是为 colArray 中的每个元素返回列列表中的第一个元素。
I have also tried:
我也试过:
colArray = <%=columns.toArray()%>;
which does not work either. I feel like I am making a little mistake somewhere and am just not seeing it. Is what I am trying to do possible in the way that I am attempting?
这也不起作用。我觉得我在某个地方犯了一个小错误,只是没有看到。我正在尝试做的事情是否可能以我正在尝试的方式进行?
Thanks.
谢谢。
采纳答案by Michael Borgwardt
You're getting the JSP code that is executed on the server mixed up with the JavaScript code that's executed on the client. The snippet <%=columns.get(j++)%>
is executed once, on the server, and the JavaScript loop around it is irrelevant at this point. When it arrives the the client, the loop's body just says colArray[i] = "first entry";
which of course puts the same string into every element of the array.
您将在服务器上执行的 JSP 代码与在客户端执行的 JavaScript 代码混合在一起。该代码段<%=columns.get(j++)%>
在服务器上执行一次,此时围绕它的 JavaScript 循环无关紧要。当它到达客户端时,循环体只是说明colArray[i] = "first entry";
哪个当然将相同的字符串放入数组的每个元素中。
What you need to do instead is to have a loop execute on the server, like this:
您需要做的是在服务器上执行一个循环,如下所示:
<% for (int i=0; i<columns.size(); i++) { %>
colArray[<%= i %>] = "<%= columns.get(i) %>";
<% } %>
My JSP skills are rusty, and the syntax may be different, but I hope you get the idea.
我的JSP技能生疏了,语法可能不一样,但希望你能明白。
Edit:As was pointed out in the comments, you need to be VERY careful about escaping anything in those Strings that could cause them to be interpreted as JavaScript code (most prominently quotation marks) - especially if they contain user-generated content. Otherwise you're leaving your app wide open to Cross-site scriptingand Cross-site request forgeryattacks.
编辑:正如评论中指出的那样,您需要非常小心地转义这些字符串中可能导致它们被解释为 JavaScript 代码(最显着的引号)的任何内容 - 特别是如果它们包含用户生成的内容。否则,您的应用程序就会对跨站点脚本和跨站点请求伪造攻击敞开大门。
回答by Grant Wagner
Once the JavaScript reaches the client, the server code has stopped executing. The server code does not execute "in parallel" with the client code.
一旦 JavaScript 到达客户端,服务器代码就停止执行。服务器代码不与客户端代码“并行”执行。
You have to build the entire JavaScript initialization in Java and send it, complete and executable, to the client:
您必须用 Java 构建整个 JavaScript 初始化并将其完整且可执行的发送到客户端:
<%
StringBuffer values = new StringBuffer();
for (int i = 0; i < columns.size(); ++i) {
if (values.length() > 0) {
values.append(',');
}
values.append('"').append(columns.get(i)).append('"');
}
%>
<script type="text/javascript">
var colArray = [ <%= values.toString() %> ];
</script>
That is just one way to do it, you can also build the output "on the fly" by embedding the server code inside the [
and ]
. I used this example to try to demonstrate the separation between building the string that comprises the client-side JavaScript and outputting that to the browser.
这只是一种方法,您还可以通过将服务器代码嵌入到[
和 中来“即时”构建输出]
。我使用这个示例来尝试演示构建包含客户端 JavaScript 的字符串和将其输出到浏览器之间的分离。
回答by perrohunter
Try using JSON (Javascript object notation) it'd be quite simple to encode the array and decode it on javascript
尝试使用 JSON(Javascript 对象表示法)对数组进行编码并在 javascript 上对其进行解码非常简单
check it out here
在这里查看
回答by fmanna
Exp Language:
经验语言:
colArray = ${columns}
回答by José Almeida
The solutions posted above didn't work in my case, I needed an extra Javascript variable to do the transference:
上面发布的解决方案在我的情况下不起作用,我需要一个额外的 Javascript 变量来进行转移:
var codesJS=new Array();
<% String[] codes=(String[])request.getAttribute("codes");
if(codes!=null){
for(int i=0; i<codes.length; i++){ %>
var code='<%= codes[i] %>'; //--> without this doesnt work
codesJS[<%= i %>]=code;
<%}
}%>
回答by F3R1
For me this solution has worked. First of all You should make a JSONArray and use it's toJSONString() method. This method converts the list to JSON text. The result of it is a JSON array.
对我来说,这个解决方案奏效了。首先,您应该创建一个 JSONArray 并使用它的 toJSONString() 方法。此方法将列表转换为 JSON 文本。它的结果是一个 JSON 数组。
<%
List<String> exampleList = new ArrayList<>();
exampleList.add("Apple");
exampleList.add("Orange");
exampleList.add("Lemon");
JSONArray fruitList = new JSONArray();
fruitList.addAll(exampleList);
%>
In your JSP page you should invoke the toJSONString() method of the list, and pass the JSON text to a JavaScript array.
在您的 JSP 页面中,您应该调用列表的 toJSONString() 方法,并将 JSON 文本传递给 JavaScript 数组。
<script type="text/javascript"> var fruitArray = <%= fruitList.toJSONString() %>;</script>
(Optionally You could make a simple getter method for the list. In case if you only instantiate the JAVA class - which has the list field - int the JSP page.)
(可选地,您可以为列表创建一个简单的 getter 方法。如果您仅实例化 JAVA 类 - 它具有列表字段 - 在 JSP 页面中。)