views:

270

answers:

2

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?

+1  A: 

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.

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

Upper Stage
He's **generating** Javascript code with JSP/JSTL...
BalusC
You are correct. By the wording of the question and the attached comments, I was not certain he understood that. Do you think my answer is confusing?
Upper Stage
+3  A: 

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.

BalusC
new keyword discouraged for creating arrays? Or always discouraged?
Upper Stage
@Upper Stage: http://stackoverflow.com/questions/383402/is-javascript-s-new-keyword-considered-harmful
BalusC
@Binaryrespawn: What do you mean with "causing my page not to load"? This is too ambiguous. If you get a blank page, check server logs for any exceptions/errors. If you get a normal page, but the images doesn't get loaded, rightclick page and view generated HTML/JS source code and verify if it is syntactically valid.
BalusC
Hi,this stsatement did not work for me. It causes my page not to load, may be because there are articles with no slideshow and hence no images. Hence I did this, <c:if test="${a.slideShow.images != null}>arr[${status.index}] = ${a.slideShow.images[0]};</c:if> , but this causes my page not to load. and its the part where it said images[0]. Now the concept works, because, as a test, I tried ${articles[0].title} within the body and this worked. So I am really baffeled.
Binaryrespawn
As said, just open page in browser and check generated JS code if it is **syntactically valid**. Maybe it represents a string value and you're forgotten singlequotes around the JS variable value.
BalusC