在jstl中使用javascript变量 [英] using javascript variable inside jstl

查看:117
本文介绍了在jstl中使用javascript变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用jstl在javascript中迭代HashMap。可以这样做吗?

I want to iterate a HashMap in javascript using jstl. is it possible to do like this?

function checkSelection(group,tvalue){
alert(group);
alert(tvalue);

<c:forEach items="${configuredGroupMap}" var="groupMap">
    alert("aa<c:out value="${groupMap.key}"/>");
    <c:if test="${groupMap.key==group}">
        alert("t<c:out value="${groupMap.key}"/>");
        <c:if test="${groupMap.value==tvalue}">
            alert("equal");
        </c:if>
    </c:if>
</c:forEach>
}

 <c:if test="${groupMap.key==group}">


推荐答案

使用jstl在javascript中迭代HashMap - 不可能

"to iterate a HashMap in javascript using jstl" - Not possible

JSTL在服务器端由您的servlet容器执行,其中Javascript只是一个将被跳过的文本,而JavaScript则在JSTL未知的客户端执行。在服务器完成处理JSTL之后,将呈现来自JSTL的生成的HTML(如果有的话)以及其他JavaScript / HTML。

JSTL is executed in server side by your servlet container for which Javascript is just a text which would be skipped whereas JavaScript is executed in client side where JSTL is unknown. After the server completes processing JSTL, the generated HTML(if any) from the JSTL along with other JavaScript/HTML will be rendered.

例如,如果你有这个,

For example, if you have this,

<c:forEach var="myItem" items="${myCollection}">
alert('<c:out value="${myItem.id}">')
<c:if test="${myItem.id == 0}"> 
   alert("zero"); 
</c:if>
</c:forEach>

如果集合中的bean的id为0,1,2,则服务器呈现以下内容通过执行上述代码到客户端,

If the ids of the beans in the collection are 0, 1, 2, the server renders the following to the client side by executing the above code,

alert('0')
alert('zero')
alert('1')
alert('2')

现在浏览器将在加载页面时给出4个警报(如果您有10000个项目,则会向浏览器呈现10000个警报语句)。所以重点是你没有在JavaScript中迭代Java集合,你只是在使用JSTL迭代集合的服务器中生成了大量的Javascript语句,并且你已经将这些Javascript语句与其他html内容一起提供给浏览器。

Now the browser will give you 4 alerts on loading the page (what if you have 10000 items, you will render 10000 alert statements to the browser). So the point is that you haven't iterated Java collection in JavaScript, you have simply generated a serious of Javascript statements in server iterating the collection using JSTL and you have provided those Javascript statements along with other html contents to browser.

这篇关于在jstl中使用javascript变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆