Info |
---|
The example code for this tutorial, Preparable_Interface_Struts2_Mvnpreparable_interface, is available on Google Code - httpat https://codegithub.google.com/papache/struts2-examples/downloads/list. After downloading and unzipping the file, you'll have a folder named Preparable_Interface_Struts2_Mvn. In that folder will be a README.txt file with instructions on now to build and run the example applicationstruts-examples. |
Introduction
Often the data used to populate a form control is dynamically generated, perhaps from a database. When the user submits the form, the Struts 2 validation interceptor attempts to validate the user's form input. If validation fails the Struts 2 framework returns the value "input" but the "input" action is not re-executed. Rather the view associated with the "input" result is rendered to the user. Usually this view is the page that displayed the original form.
...
In addition to automatically running the prepare method the prepare interceptor will also call a method named prepare[ActionMethodName]. For example, if you define a prepare method and a prepareInput method and in the Action will call class that implements preparable. When the Struts 2 framework calls the input method, the prepare interceptor will call the prepareInput and the prepare methods before calling the input method.
...
If you examine class EditAction in the example application (see above) you'll see that it implements the Preparable Interface. In the prepare method is this code:
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
carModelsAvailable = carModelsService.getCarModels() ; setPersonBean( editService.getPerson() ); |
The above statements get the car model values used to populate the car model check boxes displayed in the form and also get the information about the Person object being edited.
...
When your application requires specific statements to be executed no matter which method of the Action class is called or when validation fails, you should extend implement the Preparable interface and override the prepare method.