1

I want to pass array elements from Java code into jsp page so that if I click the button it will show the array elements in the page.

I tried to use JSTL forEach tag inside JavaScript or jQuery but it doesn't work and I don't get any error when I run the program!

.jsp page joining jQuery and JSTL

<%@ taglib prefix="stripes" uri="http://stripes.sourceforge.net/stripes.tld"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="tags" tagdir="/WEB-INF/tags" %>

<stripes:submit name="showData" value="Show" id="button"></stripes:submit>

<script>
    $(document).ready(function() {
        $("button").button().click(function(){
            <c:forEach var="array" items="${actionBean.myArray }">
                <c:out value="${array }"></c:out>
            </c:forEach>
        });
    });
</script>

.jsp page joingin JS and JSTL

<stripes:submit name="showData" value="Show" onClick="myFunc"></stripes:submit>

<c:set var="newVar">
    <c:forEach var="array" items="${actionBean.myArray }">
        <c:out value="${array }"></c:out>
    </c:forEach>
</c:set>

<script type="text/javascript">
    var my_js_data = <c:out value="${newVar}"/>
    function myFunc() {
        return my_js_data;
    }
</script>

3 Answers 3

2

Do you mean

var arr = [];
<c:forEach var="array" items="${actionBean.myArray }">
arr.push("<c:out value="${array }"></c:out>");
</c:forEach>

$(document).ready(function() {
  $("#button").click(function(){
    $("#somecontainer").html(arr.join("<br/>"));
  });
});
Sign up to request clarification or add additional context in comments.

Comments

1

The JSP renders server side and the browser takes that output and parses and runs the Javascript. You will want to view source on your page output to make sure that what is being output is valid Javascript. As currently shown it is unlikely that it would be.

Comments

1

mplungjans solution is complete and should be the accept answer. VPK and Matthew Werny tell you what are the issue with your current code.

As VPK indicated you're not properly assigning array value to JSTL variable, though, his proposal will assign only the latest value, following should concatenate all

<c:forEach items="${actionBean.myArray}" var="array" varStatus="stat">
  <c:set var="newVar" value="${stat.first ? '' : newVar} ${array}" />
</c:forEach>

than as Matthew mentioned, JSTL is processed server side, and if e.g. your array holds only one value test you end up with

var my_js_data = test

while instead you wanted

var my_js_data = 'test';

Your complete solution is to either go for what mplungjans said, or do

<c:forEach items="${actionBean.myArray}" var="array" varStatus="stat">
  <c:set var="newVar" value="${stat.first ? '' : newVar} ${array}" />
</c:forEach>
<script type="text/javascript">
    var my_js_data = '<c:out value="${newVar}"/>';
    function myFunc() {
        return my_js_data;
    }
</script>

note the commas around <c:out value="${newVar}"/>

1 Comment

Still struggling with it. I tried to show a simple string value in jsp page to test if it works and it works fine displaying a string, but doesn't show an array. Maybe there is something wrong with my java code @DefaultHandler public Resolution myDefault() { return new ForwardResolution("/WEB-INF/jsp/import.jsp"); } public Resolution showArray() throws IOException { array.add("a");array.add("b");array.add("c"); return new ForwardResolution("/WEB-INF/jsp/import.jsp"); }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.