如何在对AJAX的响应中包含多个呈现的JSP? [英] How to include multiple rendered JSP into response to AJAX?

查看:70
本文介绍了如何在对AJAX的响应中包含多个呈现的JSP?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将ajax请求发送到Java后端,并使用两个html块作为响应来响应(来自Java后端).我想使用两个不同的JSP生成这两个html块.我这样做如下:

I need to send ajax request to java back-end and to response (from java back-end) with two html-blocks as answer. I want to generate those two html-blocks using two different JSPs. I do this as following:

req.setAttribute(...);
...
resp.setContentType("text/html");
RequestDispatcher dispatcher = req.getRequestDispatcher("one.jsp");
dispatcher.include(req, resp);
dispatcher = req.getRequestDispatcher("two.jsp");
dispatcher.include(req, resp);

它有效.但是在前端,我会收到一个像一个完整的html代码(呈现为one.jsp +呈现为two.jsp)的答案.但是我需要将其作为两个单独的html块接收,以将每个块放到自己的html块中. 正确的方法是什么?

And it works. But on the front-end I receive an answer like one solid html code (rendered one.jsp + rendered two.jsp). But I need to receive it as two separate html blocks to put each block to it's own . What is the proper way to do this?

Ajax代码:

    function addNew() {
        var request = $.ajax({
            url: "myUrl",
            type: "post",
            dataType: "html",
            success: function(data) {
                $("#divNameOne").html(<one part of data>);
                $("#divNameTwo").html(<second part of data>);
            },
            error:function() {
                alert("fail");
            }
        });
    }

推荐答案

在您的成功函数中,

var reponseHtml = $(data); // or you can use $($.parseHtml(data));
$("#divNameOne").html(responseHtml.find("#div1").html());
$("#divNameTwo").html(responseHtml.find("#div2").html());

可能有效.

这篇关于如何在对AJAX的响应中包含多个呈现的JSP?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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