我的代码如下,我想用Java代码创建所有classListMap对象,然后转换为categoryList,我该怎么办?
...
var categoryList = [];
<%
List<Object> modelMaster = psiSearchForm.getModelMaster();
String parentKey = "";
String categoryKey = "";
String text = "";
for (int i = 0; i < modelMaster.size(); i++) {
Map option = (Map) modelMaster.get(i);
parentKey = (String)option.get(ConstantContainer.MODALITY);
categoryKey = (String)option.get(ConstantContainer.CATEGORY);
text = categoryKey + ": " + (String)option.get(ConstantContainer.CATEGORY_NAME);
%>
categoryList.push({
parent: "<%=parentKey%>",
key: "<%=parentKey%><%=categoryKey%>",
text: "<%=text%>"
});
<%
}
%>
答案 0 :(得分:0)
您正试图在JSP中调用JavaScript函数,这是不可能的。您可以在JSP中创建数组,并在javascript中设置最终值。
<body>
<%
JSONArray arr = new JSONArray();
for(int i =0; i < 5; i++) {
JSONObject object = new JSONObject();
object.put("name", "santa"+i);
object.put("mail", "santa"+i+"@email.com");
arr.add(object);
}
%>
<script>
var obj = <%= arr %>;
console.log("JS obj", obj);
</script>
</body>
我使用的库是json-simple-1.1.1.jar。
页面导入
<%@page import="org.json.simple.JSONObject,org.json.simple.JSONArray"%>