简体   繁体   English

Ajax和jstl,servlet setAttribute和javascript中jsp中的foreach

[英]Ajax and jstl, servlet setAttribute and foreach in jsp inside javascript

I got this ajax function which fetches some information from my servlet. 我得到了这个ajax函数,该函数从我的servlet中获取一些信息。 I need to display this info in multiple rows. 我需要在多行中显示此信息。 So before I start making some crazy long String and start working on it with javascript, I wanted to hear if it's possible to do something like below. 因此,在我开始制作一些疯狂的长字符串并开始使用javascript进行处理之前,我想知道是否可以执行以下操作。

Basically this ajax function asks my servlet for a list, which I'd like my c:forEach to display on success. 基本上,这个ajax函数向我的servlet请求一个列表,我希望c:forEach成功显示该列表。

        $.ajax({
                type: "POST",
                url: "eventHandler",
                data: {"action" : "searchCards"},
                success: function(data){  
                    <c:forEach items="${Cards}" var="c"> 
                        $(".rightWrapperDisplaySearch").append(${c.color});
                    </c:forEach>     
                },
                error : function() {
                    Announce("Error!");     
                },
            });

My servlet method 我的servlet方法

String searchCriteria = request.getParameter("value");          

            LIST = (ArrayList<Cards>) DAO.findAllCards();

            request.setAttribute("Cards", LIST);
            request.getRequestDispatcher("/WEB-INF/jsp/view/All/startPage.jsp").forward(
                    request, response);

Yes, mixing JSTL with Javascript is possible, if the purpose of JSTL is to dynamically construct the Javascript code. 是的,如果JSTL的目的是动态构造Javascript代码,则可以将JSTL与Javascript混合使用。 Server side code (thru JSTL) is generated first before client side is executed (Javascript). 在执行客户端(Javascript)之前,首先生成服务器端代码(通过JSTL)。 I don't see any conflict with the dollar $ notation used by EL (expression language) and by JQuery. 我看不到EL(表达式语言)和JQuery使用的美元符号表示任何冲突。

JQuery uses $() jQuery使用$()

EL uses ${} EL使用${}

I just found the solution at this link 我刚刚在此链接中找到了解决方案

You cannot do that since javascript executes on client & JSP executes on server side. 您无法执行此操作,因为javascript在客户端执行,而JSP在服务器端执行。

If you want to set javascript variable to JSP session, then you pass this variable through the URL like this 如果要将javascript变量设置为JSP会话,则可以通过如下所示通过URL传递此变量

 var number = 7;
    window.location="http://example.com/index.jsp?param="+number;

Now receive this var in your JSP page like this 现在像这样在您的JSP页面中接收此var

String var = request.getParameter("param");

Now set it in session 现在在会话中进行设置

  session.setAttribute("test", var);

Now you can't use below lines of code: 现在,您不能使用以下代码行:

var number = 7;
<%session.setAttribute("test", number);%>

In the above code, server will only execute the code inside <% %>. 在上面的代码中,服务器将仅在<%%>内部执行代码。 It does not know anything outside of the JSP tags. 它不知道JSP标记之外的任何内容。 So, it will also dont know about your javascript variable number. 因此,它也不知道您的JavaScript变量号。

Server executes the code & the result will be sent to the browser, then your browser will execute that javascript code var number=7; 服务器执行代码并将结果发送到浏览器,然后您的浏览器将执行该javascript代码var number=7;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM