我有两个类的对象,它们从jsp页面上的servlet返回。
public class Port {
private String portName;
private boolean acceptability;
//getters and setters ... }
这是我的Components类,它有Ports对象
public class Component {
private int id;
private String name,icon;
private List<Port> inputPorts;
private List<Port> outputPorts;
//getters and setters ...
}
这是我的java脚本代码
<script>
makeComponent("Menu", "image.png", "green",
[makePort("input", true)],
[makePort("output", false)]);
</script>
我希望将对象值放在脚本标记
中我尝试了这个,但它无法正常工作
<script>
<c:forEach items="${components}" var="component">
makeComponent("${component.name}", "image.png", "green",
[<c:forEach items="$components.inputPorts" var="inputPort">
makePort("$inputPort.portName", ${inputPort.acceptability}),
</c:forEach>],
[makePort("OUT", false)]
);
</c:forEach> </script>
这个逻辑是否正确?我可以在javascript块中使用jstl标签吗?
答案 0 :(得分:2)
异常是','
我添加了<c:if test="${!loop.last}">,</c:if>
此代码并且可以正常使用
<c:forEach items="${components}" var="component">
makeComponent("${component.name}","images/55x55.png","green",
[
<c:forEach items ="${component.inputPorts}" var="port" varStatus="loop">
makePort("${port.portName}", ${port.acceptability})
<c:if test="${!loop.last}">,</c:if>
</c:forEach>
],
[
<c:forEach items ="${component.outputPorts}" var="port">
makePort("${port.portName}", ${port.acceptability})
<c:if test="${!loop.last}">,</c:if>
</c:forEach>
]);