我正在使用spring mvc创建一个jsp站点。
protected String[] complexProperty = null;// ["something","something's value"]
在控制器中我添加了
model.addAttribute("SetProperties", arrays);//where arrays is [] of complexProperties = [[1,a],[2,b],[]...]
在jsp网站的javascript(jquery)中,我想访问这样的具体元素
var complexProperty = '${SetProperties[0][0]}';// will give me 1
alert(whichTable+" complex:"+complexProperty2);
现在我想在for循环中打印它们,所以我需要以某种方式动态地更改那些[0] [0]。但我无法弄清楚该怎么做 我已经尝试了
var temp=0;
var complexProperty = '${SetProperties[temp][temp]}';
var complexProperty = '${SetProperties["temp"]["temp"]}';
var complexProperty = '${SetProperties[${"temp"}][${"temp"}]}';
var complexProperty = '${SetProperties["${temp}"]["${temp}"]}';
但他们都没有给我1,无论是错误还是奇怪的事情。
答案 0 :(得分:0)
从未使用过JSP,但也许你可以继续使用它:
var complexProperty = ${SetProperties};
$.each(complexProperty, function( index, value ) {
alert( index + ": " + value );
});
答案 1 :(得分:0)
您正在将JSP变量与Javascript变量混合使用。最好的方法是,如果你的javascript代码调用后端来检索JSON对象,并像以后任何其他javascript对象一样使用它。
如果您有/想要在Javascript代码中使用来自http请求属性的对象,最好在JSP页面中将其序列化为JSON。
根据您的类路径(Jackson / GSON)上的内容,您可以通过某种方式执行此操作(GSON示例):
<%@ page import="com.google.gson.Gson" %>
<% Gson gson = new Gson(); %>
<script type="text/javascript">
var setProperties = <%= gson.toJson(request.getAttribute("setProperties")) %>
</script>
如果GSON不在您的课程中,请不要忘记添加它:
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.2.4</version>
</dependency>
然后您可以按照您想要的方式迭代setProperties
,例如:
for (var key in setProperties) {
console.log(key, setProperties[key]);
}
另一种方法是创建自己的JSP函数,如下所述:http://thinkinginsoftware.blogspot.com.es/2013/02/custom-jsp-taglib-to-convert-object-to.html