2

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.

Roman C
  • 49,761
  • 33
  • 66
  • 176
user1782673
  • 87
  • 13
  • Keeping the input values is trivial; the search action would have the search form properties. Sounds more like you were doing your Ajax handling wrong, though. – Dave Newton May 15 '13 at 15:30
  • Yeah turns out I had made an error in the JSP resulting in the search form properties not being kept after the action was performed – user1782673 May 17 '13 at 07:41
  • hi @DaveNewton, i am stuck precisely at the same point, ie. i want to load the success/error jsp, with the error msg as well as the user input of the form. how do I keep the input values entered by the user? – soumitra goswami Mar 27 '23 at 14:06

1 Answers1

1

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

Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
  • I am facing similar issue, in my case I have a long form along with an image upload section, and I am not using AJAX, so when validation fails for the uploaded files, i want to keep the user on the same page and show the relevant error messages. From your answer, I understand, that that may not be possible, but the first solution is what I am trying to do now, you have mentioned it's trivial how to pass the form data to the searchResult.jsp, but I am not able to do that, could you please provide me with some insights on how to keep all the values entered in the form in the search.jsp page – soumitra goswami Mar 27 '23 at 12:40
  • Wow, ten years has passed :) You can also redirect to the same page, it's important you know your interceptor stack and when and how the validation is handled: https://stackoverflow.com/a/23450365/1654265 – Andrea Ligios Mar 29 '23 at 14:02