We help IT Professionals succeed at work.

Struts - set the formbean in request

matthew016
matthew016 asked
on
2,588 Views
Last Modified: 2013-11-24
Hello,

After the submit of a form, if an error occurs in the "execute" method of the associated Action,
I want to display an error message and forward to the form again with the introduced values.

I could only display error messages for now on.
I want to know how I can display the introduced values again.

[...]
DynaActionForm ptForm = (DynaActionForm) form;
[...]
if ( ! [...] ) {
    [...]
    // PUT HERE FORM IN REQUEST AGAIN ?
    errors.add( ActionErrors.GLOBAL_ERROR,
        new ActionError("errorkey", "Formulaire incorrect"));
    saveErrors(request, errors);
    return mapping.findForward("error");   // <- form will be empty, I want to display the introduced values before he submitted
}
[...]

Thank you for any help.
Comment
Watch Question

Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
Yes, I did all of these.

* I use the struts tags : <html:text ... <html:select... etcetera
* The struts action has a dynaform associated in the struts-config xml.
* The forward, after an error, leads to the JSP displaying the same form.
Alright, you also need the following:

- Your <action> definition in struts-config.xml needs an "input" attribute that is set to the JSP that the form exists in.
- You should write your own class which extends either ActionForm or DynaActionForm and overrides the validate method:

public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
   ...
}

It's also possible to use a special kind of DynaActionForm called a DynaValidatorForm which allows you to define the validation via XML.  This article contains instructions on how to do that:
http://www.cs.northwestern.edu/academics/courses/special_topics/395-wf/dynaforms-notes.html
The approach defined in my first comment is not the complete solution, so readers may be misled when they see it.  I believe my second comment should be marked as the solution instead.  It is more accurate.

Author

Commented:
No, I didn't use the second, the first one was good.
I had an error in my code.
Oh, okay.  Glad to know you got it working.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.