permaculture playing cards*
The moose likes Struts and the fly likes Struts2 annotation validation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Struts2 annotation validation" Watch "Struts2 annotation validation" New topic

Struts2 annotation validation

Arun Ak

Joined: Jun 21, 2009
Posts: 21
My action class implements ModelDriven interface. I want to validate the form using @RequiredStringValidator.

its not validating "name" . do any one of you know where I made mistake.

Thanks in advance.
Pushkar Choudhary

Joined: May 21, 2006
Posts: 425

Arun two wrote:

Please edit your name to use a real first and last name. See the Naming Policy for more details.
Mark E Hansen
Ranch Hand

Joined: Apr 01, 2009
Posts: 643
Did you include the actionPackage parameter in your struts.xml file?
If you are going to use annotations in the source files, you'll need to
include the "actionPackages" initialization parameter to the Struts2
filter element in the application's web.xml file, as in:
The actionPackage denotes the Java package under which Struts2 should look
for classes to inspect for annotations.

Arun Ak

Joined: Jun 21, 2009
Posts: 21
Ok I have resolved my validation issue but entered into new issues

My action class have two method one for loading the form and other one to process the form data.
both method to carry out certain validation such on load check ids if not then dont load the form , while processing form data varify correction of form data.

public Class MyAction extends ActionSupport implements ModelDrivern<MyForm>{

@ConversionErrorFieldValidator(type=ValidatorType.FIELD,fieldName="age",shortCircuit=true,message="Invalid data type",)
@IntRangeFieldValidator(type=ValidatorType.FIELD, fieldName = "age",min="1",max="99")
@RequiredStringValidator(type = ValidatorType.FIELD, fieldName = "name", message = "Supply Name ")
public String processFormData() throws Exception {
return SUCCESS;

expressions = {
@ExpressionValidator(expression="employeeId == -1", message="Employee not selected"),
@ExpressionValidator(expression="deptd == -1", message="Depatment not selected")

public String loadForm(){
return SUCCESS;



When I load the form it also validationg the process form action annotation. I want to do validation on load form and other set of validation on process data - as I mentioned in the above code.
Please guide me how to achive
and if you any one know how to remove default message @ConversionErrorFieldValidator (Invalid field value for field "age".)- currently It shows default message and my custom message
Thanks in advance.
Martin O'Brien

Joined: Mar 04, 2010
Posts: 1
Hi Arun Ak

You said you solved problem. Pleas tell me how, beacuse I've the same!


Arun Ak

Joined: Jun 21, 2009
Posts: 21
Please refer my previous post in that I added some annotation to my action.
It is sorta covered in the JavaRanch Style Guide.
subject: Struts2 annotation validation
Similar Threads
Nested Model Class + ModelDriven + Struts 2
validation by annotations
Struts 2 validation using Annotation is not working
Struts2 validation using annotaions for Model Driven
separate validation for each method in action class