uraza uraza - 1 year ago 82
Java Question

Form data lost when using sendRedirect Struts 1.x

I have an action that after validation error in the action itself will do a sendRedirect to a URL but all the form data doesn't show.

public ActionForward doAction(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws Exception {

String id = 123;
if (!validate()) {
addErrorMessage(request, "FAIL");
request.sendRedirect("/myUrl.do?id=" + id);
return null;

// else proceed with stuff

Now when I check in my browser, the URL is right but all my form data on the page isn't showing. If I just reload the page the form data will show again. What can be causing this?

The myUrl.do will go to another action and that action doesn't get the form values.

Answer Source

I solved it by setting the form action to the action I want to go to by using JavaScript.

$('myForm').writeAttribute('action', '/myAction.do');

And then doing a submit of the form.