#1: Classic way
If you send a form
from search.jsp
to search.action
, and on SUCCESS you will load the result in searchResult.jsp
, and searchResult.jsp
contains the same form with the search criteria (and obviously the grid with the search results), in order to preserve and automatically re-populate the criteria you've written in the previous page, you need:
- the same Struts2 tags with the same name/value attribute in both
search.jsp
and searchResult.jsp
; actually, I suggest you to create a JSP snippet and to include it in both the JSPs with <jsp:include/>
, to avoid code redundancy (two identical forms in two different pages);
- the Getters and Setters for all the variables in your
search.action
, to be set by search.jsp
, and to be read by searchResult.jsp
.
No AJAX is involved in this, and as said by Dave, it's trivial.
#2: AJAX way
If you instead are using AJAX, then your form will be posted but your page will remain the same, only a target div (the div containing the results of the search) will be populated.
In this case, the data will be preserved because the page will not be reloaded.
Theoretically, in your searchAJAX.action
you would only need the Setters for the search criteria, because they will only need to be set, and not read back.
Being your SUCCESS result loaded in a div, by the way, will result in the need of loading a JSP snippet, not a whole JSP. The JSP snippet returned will be injected into the body of the original page, then it should contain only the relevant HTML (and the struts-tags definition), without DTD, <html>
, <head>
nor <body>
elements.
This is valid for the SUCCESS, but for the ERROR result too.
If you return a whole JSP page as global error, or action local error result, you will have a page inside a page, as you had.
The solution is to use
- a full JSP as error page for ERROR result returned by classic Actions;
- a JSP snippet as error page for ERROR result returned by AJAX Actions.
an example of a JSP snippet for ERROR:
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<strong>OOOOPS an error occurred !</strong>
Obviously another way would be returning a JSON array of data, parsing it and building the HTML client-side with Javascript, to reduce the load of network traffic, but you can easily start with the old way.