Two Laptop Bag*
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 Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Struts2 annotation validation" Watch "Struts2 annotation validation" New topic
Author

Struts2 annotation validation

Arun Ak
Greenhorn

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.
Arun
Pushkar Choudhary
Rancher

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: 645
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
Greenhorn

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;
}

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

public String loadForm(){
getLearnableForm().setMinimumRelevance(20);
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.
Arun
Martin O'Brien
Greenhorn

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

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

thanks
martin

Arun Ak
Greenhorn

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