layout: default title: Annotations parent: title: Core Developers Guide url: index.html

Annotations

{:.no_toc}

  • Will be replaced with the ToC, excluding a header {:toc}

In many places, applications can use Java 5 annotations as an alternative to XML and Java properties configuration. This page serves as a reference for all annotations across the framework.

Action Annotations

Since Struts 2.1, these annotations are provided by the Convention Plugin. Codebehind and Zero Config plugins are deprecated from Struts 2.1 on.

Action annotations are available when the framework scans the classpath for Action classes, rather than specifying individual mappings through XML configuration. See the Convention Plugin page for information on how to set up classpath scanning to allow the use of Action annotations.

AnnotationDescription
Actions AnnotationGroup of @Action annotations, maps multiple URLs to the same action
Action AnnotationDefines the URL of an action
InterceptorRefs AnnotationGroup of @InterceptorRef annotations
InterceptorRef AnnotationInterceptor, or interceptor stack to be applied to at action
Results AnnotationGroup of @Result annotations
Result AnnotationDefines a result for an action
Namespace AnnotationSet the path of the action URL (used to overwrite the default)
ResultPath AnnotationSet where the results are located (used to overwrite the default)
ParentPackage AnnotationSet the parent package of the actions (used to overwrite the default)
ExceptionMappingsGroup of @ExceptionMapping annotations
ExceptionMappingDefines an exception mapping

Workflow Annotations

AnnotationDescription
InputConfig AnnotationDefines what method to execute, or result to be returned if there are validation errors

Interceptor Annotations

To use these annotations, you have to specify the AnnotationWorkflowInterceptor to your interceptor stack.

AnnotationDescription
After AnnotationMarks a action method that needs to be executed after the result.
Before AnnotationMarks a action method that needs to be executed before the main action method.
BeforeResult AnnotationMarks a action method that needs to be executed before the result.

Validation Annotations

To use annotation-based validation, annotate the class or interface with Validation Annotation.

AnnotationDescription
ConversionErrorFieldValidator AnnotationChecks if there are any conversion errors for a field.
DateRangeFieldValidator AnnotationChecks that a date field has a value within a specified range.
DoubleRangeFieldValidator AnnotationChecks that a double field has a value within a specified range.
EmailValidator AnnotationChecks that a field is a valid e-mail address.
ExpressionValidator AnnotationValidates an expression.
FieldExpressionValidator AnnotationUses an OGNL expression to perform its validator.
IntRangeFieldValidator AnnotationChecks that a numeric field has a value within a specified range.
RegexFieldValidator AnnotationValidates a regular expression for a field.
RequiredFieldValidator AnnotationChecks that a field is non-null.
RequiredStringValidator AnnotationChecks that a String field is not empty.
StringLengthFieldValidator AnnotationChecks that a String field is of the right length.
UrlValidator AnnotationChecks that a field is a valid URL.
Validation AnnotationMarker annotation for validation at Type level.
Validations AnnotationUsed to group validation annotations.
VisitorFieldValidator AnnotationInvokes the validation for a property's object type.
CustomValidator AnnotationUse this annotation for your custom validator types.

Resources

Type Conversion Annotations

By default, type conversion for Maps and Collections using generics is directly supported.

In short, instead of specifying the types found in collections and maps as documented in Type Conversion, the collection's generic type is used. By using annotations, an application should be able to avoid using any ClassName-conversion.properties files.

To use annotation-based type conversion, annotate the class or interface with the Conversion Annotation.

AnnotationDescription
Conversion AnnotationMarker annotation for type conversions at Type level.
CreateIfNull AnnotationFor Collection and Map types: Create the types within the Collection or Map, if null.
Element AnnotationFor Generic types: Specify the element type for Collection types and Map values.
Key AnnotationFor Generic types: Specify the key type for Map keys.
KeyProperty AnnotationFor Generic types: Specify the key property name value.
TypeConversion AnnotationUsed for class and application wide conversion rules.

Tiles Annotations

The Tiles Plugin provides it's own set of Annotations. They can be used to keep tiles.xml short. Instead tiles definitions can be created by annotating actions.

AnnotationDescription
TilesDefinitionRepresents a <definition> element in tiles.xml
TilesDefinitionsA list of TilesDefinition Annotations
TilesPutAttributeRepresents a <put-attribute> element in tiles.xml
TilesPutListAttributeRepresents a <put-list-attribute> element in tiles.xml
TilesAddAttributeRepresents a <add-attribute> element in tiles.xml
TilesAddListAttributeRepresents a <add-list-attribute> element in tiles.xml