EL表达式中的JavaScript变量 [英] JavaScript variable in EL expression

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

问题描述

在我的servlet中,我通过 for 周期将许多值发送回JSP页面,如下所示:

In my servlet I'm sending many values back to JSP page by for cycle like this:

protected void doGet(HttpServletRequest request, HttpServletResponse response){
    for (int i = 0; i < veryBigNumber; i++){  
        if (something){  
            request.setAttribute("value" + i, "true");  
        else{  
            request.setAttribute("value" + i, "false");  
    }  
}

在JSP中,我想在

<script>  
    for (var i=0; i < veryBigNumber; i++){  
        if ("${value + (i)}" == "true"){  
            doSomething;  
        } else{  
            doSomethingElse;   
    }  
}  
</sctipt>

问题是我不知道如何使变量 i EL表达式的一部分。可能吗?如果是的话,怎么办?

谢谢...

The problem is I don't know how to make the variable i a part of EL expression. Is it possible? And if yes, how?
Thanks...

推荐答案

您在这里犯了一个概念上的错误。 Java / JSP / EL在网络服务器上运行,并生成HTML / CSS / JS,后者又在网络浏览器中运行(在网络浏览器中单击鼠标右键,然后执行查看源代码自己查看)。但是,您期望EL和JS在Web浏览器中同步运行。

You're making a conceptual mistake here. Java/JSP/EL runs on webserver and produces HTML/CSS/JS which in turn runs in webbrowser (rightclick page in webbrowser and do View Source to see it yourself). Yet you're expecting that EL and JS runs in sync in the webbrowser. This is not true.

您基本上需要使用JSP来执行迭代和EL评估,并相应地编写它,以便它打印出您想要的正确JS代码。

You basically need to perform the iteration and EL evaluation using JSP instead and write it accordingly so that it prints the proper JS code you want.

<script>
    <c:forEach begin="0" end="${veryBigNumber - 1}" var="i">
        <c:set var="value" value="value${i}" />
        <c:choose>
            <c:when test="${requestScope[value]}">
                doSomething;
            </c:when>
            <c:otherwise>
                doSomethingElse;   
            </c:otherwise>
        </c:choose>
    </c:forEach>
</script>

请注意,此特定结构笨拙且有代码味道。具体的功能要求尚不十分清楚,因此无法针对您要达到的目标提出更优雅的解决方案。

Note that this particular construct is clumsy and a code smell. The concrete functional requirement is not exactly clear, so it's not possible to propose the more elegant solution for whatever you're trying to achieve.

这篇关于EL表达式中的JavaScript变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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