如何使用 jQuery 调用 Struts Action 类并在屏幕上显示数据 [英] How to call the Struts Action class and display the data on screen using jQuery

查看:22
本文介绍了如何使用 jQuery 调用 Struts Action 类并在屏幕上显示数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 JSP,我在其中以表格格式显示数据列表.每列中的一列将有 3 个不同的链接.在 Action 类中有 3 个不同的方法,以便在单击链接时触发相应的方法.一旦该方法从 Action 类返回数据,将根据 Action 类返回的列表大小追加行数.我正在使用 struts 迭代器来迭代并添加 <tr>.我已经编写了 jQuery 来执行该操作,并且一切正常.

I have JSP where I am displaying list of data in a tabular format. One of the columns in each will have 3 different links. Have 3 different methods in Action class, so that the respective method will be triggered on click of the link. Once the method has returned the data from Action class, number of rows will be appended based on the list size returned by Action class. I am using struts iterator to iterate and add the <tr>. I have written the jQuery to perform that operation and everything works fine.

新要求是当我点击第一个链接时,它应该显示记录集.当我再次单击第一个链接时,应该删除显示的记录集.它有点像切换记录集.

New requirement is that when I click on the first link, it should display the set of records. When I click on the first link again, then the set of records that were displayed should be removed. Its kind of toggling the set of records.

为了实现这一点,我创建了一个 div 组件,将在其中添加新的记录集并尝试执行此操作,但没有按预期工作.附上了从每个文件中提取的源代码.请让我知道您的想法.

To achieve this, I created a div component, inside which the new set of records would be added and try to do it, but its not working as expected. Have attached the extract of the source code from each file. Please let me know your thoughts.

JSP 文件:

<s:iterator value="#mbean.myList" var="poItem">
    <tr>
        <td class="matlNumber">&nbsp;</td>
        <td><s:property value="#poItem.doc" /></td>
        <td>&nbsp;</td>
        <td>&nbsp;</td>
        <td><s:property value="#poItem.weekOne" />  </td>
        <td><s:property value="#poItem.weekFive" /></td>
    </tr>
</s:iterator>

JS 文件:

$(".poLink").click(function(){      
    var myData = $(this).closest('tr').find('td:first').text();
    document.forms[0].action = "poListMaterialPlanning.action?myData ="+myData ;
    document.forms[0].submit();
}

我尝试使用$.ajax(),但是无法将Action类返回的数据赋值给JSP中的struts变量.

I tried to use the $.ajax(), but could not assign the data returned by the Action class to the struts variable in JSP.

推荐答案

前提:句子

无法将 Action 类返回的数据分配给 JSP 中的 struts 变量.

could not assign the data returned by the Action class to the struts variable in JSP.

是错误的,因为在呈现的 HTML 页面中没有像Struts 变量"这样的东西.

也就是说,有几种方法可以实现您的目标(虽然不完全清楚):

That said, there are several ways to achieve your goal (although not completely clear):

  1. 渲染页面时加载内容,然后通过javascript隐藏/显示一>;这是最快的方法,但也有不鼓励这样做的情况,例如:

  1. Load the content when rendering the page, then hide / show it through javascript; this is the fastest way, but there are cases where it is discouraged, for example :

  • 页面加载后内容可能已更改,因为其他用户或软件对其进行了更新;
  • 为所有记录加载它太大了,内存占用和页面加载时间变得无法接受.

通过标准的 POST/GET 提交添加/删除内容;这是旧的方式,您重新加载所有内容,为所有内容充电,并且必须手动处理锚点和页面相关操作.

Add / remove the content through a standard POST / GET submit; this is the old way, you reload everything, recharge everything and have to manually handle anchors and page-related operations.

通过 AJAX 调用添加内容,通过 javascript(原生 javascript、jQuery、struts2-jQuery-plugin 或其他库)删除;如果解决方案 n.1 不适合您的情况,这可能就是您所需要的.

Add the content through AJAX calls, and remove it through javascript (raw javascript, jQuery, struts2-jQuery-plugin, or other libraries); this is probably what you need, if solution n.1 doesn't fit your case.

要坚持当前的标准/最佳实践/工具以简化您的工作,您应该使用:

To stick with the current standards / best practices / tools that will simplify your work, you should use:

  • Struts2-jquery-plugin instead of raw jQuery to perform the AJAX call. Take a look at the showcase, both Ajax and Widgets parts. You probably are looking for the Ajax->Div->Div - Reload example. THEN
  • return a JSP snippet in a target div, OR
  • a JSON object that you will manually parse through javascript to build the output (less network traffic, but more work), or feed to struts2-jquery tags. BTW, in case of JSON, it's better to use struts2-json-plugin, that will help you a lot.

旁注:如果您注入包含其他 struts2-jquery 标签的 JSP 片段,您需要阅读这个.

Side note: if you inject a JSP snippet containing others struts2-jquery tags, you need to read this.

这篇关于如何使用 jQuery 调用 Struts Action 类并在屏幕上显示数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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