最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How can I access Java list elements from my client-side JavaScript code? - Stack Overflow

programmeradmin5浏览0评论

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.

Any ideas?

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.

Any ideas?

Share Improve this question edited Feb 17, 2010 at 14:09 Faiyet asked Feb 12, 2010 at 18:43 FaiyetFaiyet 5,47114 gold badges52 silver badges67 bronze badges 5
  • 1 Javascript is in no way related to Java, apart from the first four letters of the name. – Adam Robinson Commented Feb 12, 2010 at 18:45
  • 2 Forget about Java, use plain (real javascript), and DOM (Document Object Model) to get the items in that UL. JavaScript is also typically client side code. – JL. Commented Feb 12, 2010 at 18:46
  • 1 Guys, read the question and code instead of only title before menting. – BalusC Commented Feb 12, 2010 at 19:00
  • Titles are important. Are they not a synopsis of the problem? If the question states something different than the prose, might it not suggest a misunderstanding? – Upperstage Commented Feb 12, 2010 at 19:03
  • Hi guys, I'm terrably sorry about the ambiguous wording. BalusC, point well taken. Thanks for the help. – Faiyet Commented Feb 17, 2010 at 14:11
Add a ment  | 

2 Answers 2

Reset to default 5

In EL you can use the brace notation to access a List element by index. 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).

That said, you normally declare JS arrays like follows:

var arr = [];

The new keyword is considered discouraged in JS.

As those who mented on your question suggest, this is a mon 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.

View source on your page - it might shed some light. You should not see the JSP/JSTL you include above.

发布评论

评论列表(0)

  1. 暂无评论