简体   繁体   English

任何方式使用jjl自定义标签的ajax而不在javascript中复制标签?

[英]any way to use ajax with jstl customtags without duplicating tags in javascript?

I am working on a page where part of the content is loaded normally and the pages are rendered using jsps. 我正在一个页面上正常加载部分内容,并使用jsps呈现页面。 The product images and links generated on that page are by custom jstl tags. 在该页面上生成的产品图像和链接是通过自定义jstl标记。

When the user scrolls more products are requested from the server in a JSON response and then using UnderscoreJS templates they are rendered and added to the bottom of the page. 当用户滚动时,在JSON响应中从服务器请求更多产品,然后使用UnderscoreJS模板将它们呈现并添加到页面底部。 Is there any way I can use the JSTL tags in Javascript without recreating them using a javascript function (it won't be possible to re-create all of them in javascript). 有没有什么办法可以在Javascript中使用JSTL标签,而无需使用javascript函数重新创建它们(不可能在javascript中重新创建所有这些标签)。

What is the best way to handle this scenario? 处理这种情况的最佳方法是什么? I guess I could return a rendered html response for the ajax call, but that would mean I have some ajax requests which use json and some that use rendered html... 我想我可以为ajax调用返回一个渲染的html响应,但这意味着我有一些使用json的ajax请求和一些使用渲染的html ...

You can't re-use the JSTL tags in JavaScript both because they are Java, not JavaScript, and because they are executed on the server side as the page is rendered, not on the client. 您不能在JavaScript中重复使用JSTL标记,因为它们是Java而不是JavaScript,并且因为它们在呈现页面时在服务器端执行,而不是在客户端上执行。

However, since your JavaScript can already get and render the data, why not just do away with rendering of the first part with JSTL, and do the whole thing in JavaScript/Ajax/UnderscoreJS ? 但是,既然你的JavaScript已经可以获取并呈现数据,那么为什么不放弃使用JSTL渲染第一部分,并在JavaScript / Ajax / UnderscoreJS中完成所有工作呢?

One way to solve this is to make AJAX method to return the HTML instead of the plain objects. 解决此问题的一种方法是使AJAX方法返回HTML而不是普通对象。

You can write a jsp page which will create the html for the new records and send the html as the response which can be appended to the previous contents. 您可以编写一个jsp页面,该页面将为新记录创建html,并将html作为响应发送,该响应可以附加到之前的内容中。

Ex 防爆

Assume that your page is like this 假设您的页面是这样的

<div class="container">
    <div class="record rec-1">
        //some html code
    </div>
    <div class="record rec-2">
        //some html code
    </div>

    .....
    .....

    <div class="record rec-n">
        //some html code
    </div>
</div>

Then when you want to load more contents and make the ajax call let the server return a processed html response like 然后当你想加载更多的内容并进行ajax调用时,让服务器返回一个处理过的html响应

    <div class="record rec-n+1">
        //some html code
    </div>
    <div class="record rec-n+2">
        //some html code
    </div>

    .....
    .....

    <div class="record rec-n+k">
        //some html code
    </div>

Which you can easily apped to the container . 您可以轻松地将其containercontainer You can use a jsp page to generate the html. 您可以使用jsp页面生成html。

I've used in the same situation at least two (more or less similar) solutions. 我在相同的情况下使用了至少两个(或多或少相似)的解决方案。

  1. Use included JSP. 使用包含的JSP。 The first request (page with list of product images and links) should return JSP for the whole page, eg 第一个请求(包含产品图像和链接列表的页面)应返回整个页面的JSP,例如

     <%@page contentType="text/html; charset=UTF-8"%> <!DOCTYPE html> <html> ... skipped ... <c:forEach items="${products}" var="product"> <%@include file="productInfo.jsp"%> </c:forEach> ... skipped ... </html> 

    Second request (AJAX request for continuation of the list) should return JSP with requested part of list: 第二个请求(继续列表的AJAX请求)应该返回带有请求的列表部分的JSP:

     <c:forEach items="${products}" var="product"> <%@include file="productInfo.jsp"%> </c:forEach> 

    Its content may be inserted by AJAX response handler in browser into DOM without any additional processing. 其内容可以由浏览器中的AJAX响应处理程序插入到DOM中,而无需任何其他处理。 Note that both responses include the same JSP to render information about the single product. 请注意,两个响应都包含相同的JSP来呈现有关单个产品的信息。

  2. Instead of <%@include file="..."%> you may use JSP tags . 您可以使用JSP标记代替<%@include file="..."%> The rest looks similar: JSP for the whole page 其余看起来类似:整个页面的JSP

     <%@page contentType="text/html; charset=UTF-8"%> <!DOCTYPE html> <html> ... skipped ... <c:forEach items="${products}" var="product"> <tag:productInfo product="${product}" /> </c:forEach> ... skipped ... </html> 

    JSP with requested part of list: 带有请求部分列表的JSP:

     <c:forEach items="${products}" var="product"> <tag:productInfo product="${product}" /> </c:forEach> 

This way you can reuse single view (JSP) for both responses (AJAX and non-AJAX), and eliminate second rendering mechanism for AJAX (JSON + manual DOM fragment creation from JavaScript). 这样,您可以为单个视图(AJAX和非AJAX)重用单个视图(JSP),并消除AJAX的第二种渲染机制(从JavaScript创建JSON +手动DOM片段)。

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

相关问题 如何在不使用jsp UseBean的情况下使用JSTL标记 - How to use JSTL tags without using jsp UseBean 将javascript变量传递给jstl标签 - Pass javascript variable to jstl tags 使用response.getWriter()打印JSTL标签 - Use response.getWriter() to print JSTL tags 如何在jstl + javascript中使用foreach? - How to use foreach in jstl + javascript? 如何将javascript与JSTL标签放在一个单独的文件中? - How to put javascript with JSTL tags in it in a separate file? 有没有办法在不重复代码的情况下使用不同的配置从 2 个不同的类中运行相同的一组测试两次 - Is there any way to run same set of tests from 2 different classes twice with different configurations without duplicating code 是否可以在不同的 usl jstl 标签中使用绑定的参数标签? - is possible to use binded param tags in different usl jstl tags? 使用JSTL标记转义XML来防止XSS是否有任何弊端 - Are there any drawbacks of using JSTL tags for escaping XML for XSS prevention 有没有一种方法可以在这里创建Model而无需复制代码? - Is there a way to create Model here without duplicating the code? 有没有办法在没有实体的情况下使用`@Procedure` 注释? - Any way to use the `@Procedure` annotation without an entity?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM