[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2573
  • Last Modified:

Struts - set the formbean in request

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.
0
matthew016
Asked:
matthew016
  • 4
  • 2
1 Solution
 
malfunction84Commented:
In order to repopulate a form's elements, there are a few things that must be configured in your project:

- You must use the "struts" custom JSP tags to create your form elements (as opposed to plain HTML form elements).
- The HTML form's "action" must point to a Struts Action which has the ActionForm associated with it (configured in struts-config.xml).
- The ActionForward your that your execute() method returns must lead to a JSP file which uses the "struts" JSP tags mentioned above (usually the same JSP that you used for form submission the first time).

If you look closely, these requirements also outline the cycle you're trying to create: entry, submission, validation, reentry/acceptance.

Are all of these true for your project?
0
 
matthew016Author 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.
0
 
malfunction84Commented:
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
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
malfunction84Commented:
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.
0
 
matthew016Author Commented:
No, I didn't use the second, the first one was good.
I had an error in my code.
0
 
malfunction84Commented:
Oh, okay.  Glad to know you got it working.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now