Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 4.0
Info

The example code for this tutorial, preparable_interface, is available for checkout from https://svn.apache.org/repos/asf/struts/sandbox/trunk/struts2examples/Image Removed.

Introduction

...

In the prepare method you should put any statements that must be executed no matter what other Action class method will be called and also statements that should be executed if validation fails. Usually statements in the prepare method set the value for Action class instance fields that will be used to populate form controls and get the values that will be used to set the initial form field values.

Wiki MarkupIn addition to automatically running the prepare method the [prepare interceptor|http://struts.apache.org/2.3.1.2/docs/prepare - interceptor.html] will also call a method named prepare\[ActionMethodName\]. For example, define a prepare method and a prepareInput method in the Action 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.

Example Application

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:

java
Code Block
java
titleEditAction.java prepare Method
java
		
   carModelsAvailable = carModelsService.getCarModels() ;
		
   setPersonBean( editService.getPerson() );

...