How to call the Struts Action class and display the data on screen using jQuery

前端 未结 1 1394
無奈伤痛
無奈伤痛 2020-12-07 05:18

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

相关标签:
1条回答
  • 2020-12-07 05:51

    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:

    • 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.

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

    0 讨论(0)
提交回复
热议问题