简体   繁体   English

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

[英]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.

相关问题 如何在Worklight的客户端代码上使用java? - How to use java on client-side code of Worklight? 如果我在客户端或网页上上传我的 .jar 文件,我该如何保护它? - How can I secure my .jar file if I upload it on client-side or on webpage? 如何在服务器端运行 JavaScript 代码 Java 代码? - How can I run JavaScript code at server side Java code? 客户端Java本质上不如JavaScript安全吗? - Is client-side java intrinsically less secure than javascript? 如何使用Java JSF / Tobago显示客户端消息? - How to show a client-side message with Java JSF/Tobago? Java:如何添加SSL客户端身份验证 - Java: how to add SSL client-side authentication 如何从我的休息服务发送一个json对象,以便我可以在客户端javascript解析 - How do I send a json object from my rest service so I can parse in out on the client side javascript Tapestry 5-如何在客户端上进行客户端“必需”验证 <t:radiogroup> ? - Tapestry 5 - How can I do client-side 'required' validation on <t:radiogroup>? 如何从Java服务器将Google api access_token传递给gapi javascript客户端? - How can I pass a google api access_token from java server to gapi javascript client? 客户端如何知道文件是从服务器下载或接收的? - How can client-side know that file is downloaded or received from server?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM