Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/475.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Velocity数组到javascript数组_Javascript_Velocity - Fatal编程技术网

Velocity数组到javascript数组

Velocity数组到javascript数组,javascript,velocity,Javascript,Velocity,我需要将velocity字符串数组传递给JavaScript函数。因此,我想将velocity数组转换为JavaScript数组 源代码如下所示: String[] arrStr[] = new String[3]; arrStr[0] = "String 1"; arrStr[1] = "String 2"; arrStr[2] = "String 3"; request.setAttribute("a", arrStr); 在我的HTML模板中 #set ( #arr = $request

我需要将velocity字符串数组传递给JavaScript函数。因此,我想将velocity数组转换为JavaScript数组

源代码如下所示:

String[] arrStr[] = new String[3];
arrStr[0] = "String 1";
arrStr[1] = "String 2";
arrStr[2] = "String 3";
request.setAttribute("a", arrStr);
在我的HTML模板中

#set ( #arr = $request.getAttribute("a"))

<script language="javascript">

var newArr = "${arr}";

</script>
#set(#arr=$request.getAttribute(“a”))
var newArr=“${arr}”;
但是字符串数组不会复制到newArr。有人能帮我解决这个问题吗?

比如:

var newArr = [ 
#foreach( $var in $arr )#if($foreach.index>  0),#end "$var" #end 
]; 

这是一个非常好的问题,我尝试了无数的解决方案,没有任何合理的方法不能生成字符串数组,所有结果都是[param1,param2]而不是[param1”,“param2”],并出现错误“param1未定义”

然而,我能够通过使用#foreach作为参数和rest参数来解决这个问题

在HTML中:

functionName(
  #foreach($obj in $array)
      #if ($velocityCount != $array.size())'$obj',
      #else'$obj'
      #end
  #end
)
在Javascript中:

functionName(...args) 
…args是一个数组,它“将不确定数量的参数表示为数组”。因此,如果需要数组之外的其他参数,请记住将它们放在数组之前:

functionName(param1, param2, ...args)

如果
$var
的值包含
”或换行符或反斜杠怎么办?如果
$obj
的值包含
”或换行符或反斜杠怎么办?