如何从我的客户端JavaScript代码访问Java列表元素?

时间:2022-04-11 15:54:09

I have the following simple script, which I am using to dynamically create the list elements in a <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. 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); I want to do something like "${a.slideShow.images.get(0)}";. The get() is a java method from the list object.

我的问题源于图像属性。每篇文章都有一个幻灯片,每个幻灯片都有一张图片列表。我想通过jave列表从图像列表中提取出第一张图像。我想做一些类似“${a. slideshow .image .get(0)}”的操作。get()是来自list对象的java方法。

Any ideas?

什么好主意吗?

2 个解决方案

#1


6  

In EL you can use the brace notation to access a List element by index. Thus, the following should do:

在EL中,您可以使用支撑符号来通过索引访问列表元素。因此,如下所示:

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中,新关键字被认为是不鼓励的。

#2


1  

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. The JSTL/JSP execute at the server to create source/HTML that is then sent to the client.

正如那些评论你的问题的人所说,这是一个常见的误解。当您的JavaScript执行(在浏览器中)时,Java和JSP和JSTL不再可用。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。

#1


6  

In EL you can use the brace notation to access a List element by index. Thus, the following should do:

在EL中,您可以使用支撑符号来通过索引访问列表元素。因此,如下所示:

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中,新关键字被认为是不鼓励的。

#2


1  

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. The JSTL/JSP execute at the server to create source/HTML that is then sent to the client.

正如那些评论你的问题的人所说,这是一个常见的误解。当您的JavaScript执行(在浏览器中)时,Java和JSP和JSTL不再可用。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。