JavaScript variable in EL expression

后端 未结 1 822
无人共我
无人共我 2021-01-26 00:16

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

protected void doGet(HttpServletRequest request, HttpServletResponse r         


        
相关标签:
1条回答
  • 2021-01-26 01:05

    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.

    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.

    0 讨论(0)
提交回复
热议问题