在 Jquery post/get/ajax 调用中处理 Struts 2 操作的正常模式

发布于 2024-11-30 02:12:06 字数 926 浏览 5 评论 0原文

我知道我在这里遗漏了一些概念性的东西,它一直让我困惑,但我将提出一个用例,并希望得到最佳实践响应。

       $.ajax({
       type: "POST",
       url: "/myapp/FilterRecord.action", 
       data: "pageSource=list_edit_add&table=" + table + "&output=" + output + "&selectedIds=" + json_text, 
       success: function(data) {
           document.close();
           document.open();            
           document.write(data);
       }
  });

本例中,调用的是jquery的ajax方法。 Struts 2 操作正在使用默认结果类型(即 Dispatcher Result)执行。 Action.SUCCESS 后,将进入上面的成功函数。传入的数据是一个完整的jsp页面,包括head和body。在上面的代码中,我们以一种不一定要使用的方式来操作 document.write()。上述的目的是获得头部部分和身体部分。设置 jquery 更好的页面部分的其他一些方法是:

  1. document.all[0].innerHTML = data

  2. ${'#someRandomSection'}.html(data)

但他们都没有捕获传递给我们的完整内容。那么显示 DispatcherResult 结果(即传回给我们的整个 jsp 页面)的正确方法是什么?我在该页面上使用了一些涉及的 ​​javascript,并且使用我在上面的用例中提出的方法无法正确呈现。

I know I am missing something conceptually here and it keeps tripping me up but I will present a use case and would appreciate a best practice response.

       $.ajax({
       type: "POST",
       url: "/myapp/FilterRecord.action", 
       data: "pageSource=list_edit_add&table=" + table + "&output=" + output + "&selectedIds=" + json_text, 
       success: function(data) {
           document.close();
           document.open();            
           document.write(data);
       }
  });

In this case, the ajax method of jquery is being called. A Struts 2 action is being performed using the default result type, that is Dispatcher Result. Upon Action.SUCCESS, the success function above is entered. The data being passed in is a complete jsp page, head and body both. In the code above, we are sort of manipulating document.write() in a way which it is not necessarily meant to be used for. The aim of the above is to get both the head section and body section. Some other approaches for setting parts of the page which jquery is better set up for are:

  1. document.all[0].innerHTML = data

  2. ${'#someRandomSection'}.html(data)

but neither of them capture the full content being passed to us. What then is the proper way to display the result of a DispatcherResult, that is the entire jsp page which is passed back to us? I have some involved javascript going on for this page, and it is not correctly rendering with the approach I presented in the use case above.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

为你拒绝所有暧昧 2024-12-07 02:12:06

首先,您传递数据的方式不正确..看起来您将其传递为查询字符串..您需要将其更改为以下内容:

data: {
pageSource:list_edit_add,
table:table,
output:output,
selectedIds: json_text
}

并且您需要指定 dataType

dataType: "html".

您可以为您的设置一个jsfiddle吗页..

first of all the way you are passing the data is incorrect.. it looks like you are passing it as if its a querystring.. you need to change it to follows:

data: {
pageSource:list_edit_add,
table:table,
output:output,
selectedIds: json_text
}

and you need to specify the dataType

dataType: "html".

Can you setup a jsfiddle for your page..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文