Reputation: 87
I currently have a JSP Page with a form in it.
On submission of this form I want to reload this same page with the form being populated with the user input values before the form was submitted. The Search results will then be outputted on the page.
I have successfully got the search results to output on the page but would like some advice on how to keep the input values in the form elements on page reload.
I did load the search results via the struts 2 Ajax library however if there was a validation error the ajax request would return the full jsp page itself, rather than the search results, as on error the page should reload itself displaying the action errors. As a result I have abandoned using Ajax for loading the search results.
Upvotes: 2
Views: 1881
Reputation: 50203
#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:
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);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
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.
Upvotes: 1