Search code examples
javavalidationconfigurationstruts2

How to create alias for action in Struts2


Both of these files are having same validation rules.How to used same xml for mupltiple Actions?

Action1-validation.xml
Action2-validation.xml

struts.xml:

<action name="Action1" class="test.Action1"/>
<action name="Action2" class="test.Action2"/>

Now how to use the same validation file based on the Action alias name for both Actions? How to create alias name for an Action ?


Action1
{
  String name;
  Date dob;
  int age;
}
Action2
{
  String name;
  Date dob;
  int age;
}

what is the meaning of ActionAliasName and How to create that ?,which is mentioned from below line.

The validation.xml format is either <ActionClassName>-validation.xml or <ActionClassName>-<ActionAliasName>-validation.xml.

Solution

  • You want to use the same validation file for both action classes because they have the same properties. You should read Defining Validation Rules and choose option 3. Create a base action class that has the same property set for both action classes and create *-validation.xml for this.

    Actions are mapped to the methods by name in the action configuration. This name might differ from the method name, that's why it's called an action alias.

    You might also been interested How Validators of an Action are Found that will compliment said above.

    Another option is to use one class for actions that are mapped to different methods and use the same per action class validation.