Dynject comes with built-in support for Struts 1.x via the
StrutsUtil class, and you can annotate your ActionForm as if it was a bean. Of course, nothing can be done about the fact that ActionForm properties must be Strings, but as far as Dynject is concerned, you can use any kind of validating annotation on those fields (even numeric ones), since Dynject will handle the conversions automatically.
For example:
public class Customer extends ActionForm {
@Normalized(
whitespace=WhitespaceProcessing.COLLAPSE,
trim=TrimType.BOTH,
transform=TransformType.UPPERCASE)
@Length(min=3,max=20,messageKey="customer.length")
private String customer;
@IsDate(value="dd/MM/yyyy", messageKey="customer.birthdate")
private String birthdate;
@MinInclusive(value=0,messageKey="customer.low.discount")
@MaxInclusive(value=20,messageKey="customer.high.discount")
private String discount;
...getters and setters here....
}
In your action, you simply call the method StrutsUtil.validate, passing to it an instance of the action form, and it validates and normalizes the instance, and then returns the set of errors as an ActionErrors instance:
public class SampleAction extends Action {
public final ActionForward execute(ActionMapping mapping, ActionForm customer
HttpServletRequest request, HttpServletResponse response)
throws Exception {
ActionErrors errors = StrutsUtil.validate(customer);
if (errors.size() > 0 ) {
saveErrors(request,errors);
return mapping.getInputForward();
}
request.setAttribute("customer",customer);
return mapping.findForward("success");
}
}