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

java - JSTL inside jQuery or JavaScript - Stack Overflow

programmeradmin3浏览0评论

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=".tld"%>
<%@ taglib prefix="c" uri="" %>
<%@ 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>

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/stripes.tld"%>
<%@ taglib prefix="c" uri="http://java.sun./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>
Share Improve this question edited Oct 6, 2015 at 14:27 Bhavesh Odedra 11.1k12 gold badges36 silver badges58 bronze badges asked Jan 20, 2015 at 9:56 nahidnahid 4073 gold badges9 silver badges17 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 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/>"));
  });
});

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.

mplungjans solution is plete 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 plete 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 mas around <c:out value="${newVar}"/>

发布评论

评论列表(0)

  1. 暂无评论