How To Call The Struts Action Class And Display The Data On Screen Using Jquery
Solution 1:
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):
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.
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.
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.
Post a Comment for "How To Call The Struts Action Class And Display The Data On Screen Using Jquery"