I have the a class, CouponAction, with two methods, initCoupon and addCoupon. I only want validation on the addCoupon method, not on class level where it tries to validate all methods.
this class with methods looks something like this
@Scope("prototype") @Controller
@Namespace("/coupon")
public class CouponAction extends BaseAjaxAction<Coupon> {
............
@Action(value = "initPaymentCoupons", results = {
@Result(location = "/jsp/order/coupon/paymentCoupon.jsp", name = "success")
})
public String initCoupons(){
........
}
@Validations(requiredFields = {
@RequiredFieldValidator(fieldName = "couponCode", type = ValidatorType.FIELD, message = "Enter a coupon code")
})
@Action(value = "/addCoupon", interceptorRefs=@InterceptorRef("jsonValidationWorkflowStack"),
results = {
@Result(location = "/jsp/order/coupon/paymentCoupon.jsp", name = "success")
})
public String addCoupon() {
.......
}
Then I get:
2011-11-10 00:44:13,567 ERROR org.apache.struts2.components.ActionComponent.error:38 - Could not execute action: /coupon/initCoupons
No result defined for action vervik.actions.order.CouponAction and result input
All examples Iv seen uses the validation annotation on class level.
When I used xml I had a file:
CouponAction-addCoupon-validation.xml
which only validation the addCoupon method.
How can I make it work with validation annotation?
Sources:
http://struts.apache.org/2.1.6/docs/convention-plugin.html#ConventionPlugin-InterceptorRefannotation
What i am aware is that When multiple methods are used to map different actions on the same class, and one of them is annotated with
@Validations, those validators will be triggered for all the actions.Solution seems to use annotated with
@SkipValidationorvalidateAnnotatedMethodOnlyis set to true in the “validation” interceptor.refer the following link
Struts2 Validation
though even i have not worked with such case using annotation so hope this will work for you.