[英]How can I access Java list elements from my client-side JavaScript code?
I have the following simple script, which I am using to dynamically create the list elements in a <ul>
我有以下简单的脚本,用于在
<ul>
动态创建列表元素
<script type="text/javascript">
function generate(){
var arr = new Array();
<c:forEach items="${articles}" var="a" varStatus="status">
$('#listitems').append(
"<li>"+${a.title}+"</li>"
);
arr[${status.index}] ="${a.slideShow.images}";
</c:forEach>
}
</script>
My problem stems from the images attribute. 我的问题源于images属性。 Every article has a slideshow and every slideshow has a list of images.
每篇文章都有一张幻灯片,每张幻灯片都有一张图像列表。 I want to pull out the very first image from the list of images via the jave list.get(index);
我想通过jave list.get(index);从图像列表中拉出第一张图像。 I want to do something like "${a.slideShow.images.get(0)}";
我想做类似“ $ {a.slideShow.images.get(0)}”的事情; .
。 The get() is a java method from the list object.
get()是列表对象中的java方法。
Any ideas? 有任何想法吗?
In EL you can use the brace notation to access a List
element by index. 在EL中,您可以使用大括号符号按索引访问
List
元素。 Thus, the following should do: 因此,应执行以下操作:
arr[${status.index}] = "${a.slideShow.images[0]}";
This will behind the scenes do exactly as you proposed: a.getSlideShow().getImages().get(0)
. 这将在幕后完全按照您的建议进行:
a.getSlideShow().getImages().get(0)
。
That said, you normally declare JS arrays like follows: 就是说,通常您可以像下面这样声明JS数组:
var arr = [];
The new
keyword is considered discouraged in JS. JS不建议使用
new
关键字。
As those who commented on your question suggest, this is a common misunderstanding. 正如对您的问题发表评论的人所暗示的那样,这是一种常见的误解。 By the time your JavaScript executes (in the browser), Java and JSP and JSTL are no longer available.
到您的JavaScript执行时(在浏览器中),Java和JSP和JSTL不再可用。 The JSTL/JSP execute at the server to create source/HTML that is then sent to the client.
JSTL / JSP在服务器上执行以创建源/ HTML,然后将其发送到客户端。
View source on your page - it might shed some light. 在页面上查看源代码-可能会有所启发。 You should not see the JSP/JSTL you include above.
您不应看到上面包含的JSP / JSTL。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.