Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
362 views
in Technique[技术] by (71.8m points)

html - How to call the Struts Action class and display the data on screen using 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.

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 File:

<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 File:

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Premise: the sentence

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

is wrong because there is nothing like a "Struts variable" in the rendered HTML page.

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

  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 :

    • the content could have been changed after the page is loaded, because of other users or softwares updating it;
    • it's too big to load it for all the records, and the memory footprint and page loading time becomes unacceptable.
  2. 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.

  3. 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:

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...