rajub
rajub

Reputation: 320

struts2 annotation validation with multiple method in same action

I am new in struts2 how can implement action(method) level validation using annotation. I action class have two methods, Add and edit. When call add action then I want to validate all fields, but when I call edit action then I don't want to validate all fields. So i added the annotation based validation over the add and edit method. It's working for add but when i submit edit it's trying to validate all fields(add+edit method). I have added class level validateAnnotatedMethodOnly annotation but still giving same problem.

@InterceptorRefs({ @InterceptorRef(value = "defaultStack", params = { "validation.validateAnnotatedMethodOnly", "true",
        "validation.excludeMethods", "add, edit" }) })

How can I solve this problem, Please help me

=====================================================

Here is complete code

@InterceptorRefs({ @InterceptorRef(value = "defaultStack", params = { "validation.validateAnnotatedMethodOnly", "true" }) })
public class CustomerAction extends ActionSupport implements SessionAware, Preparable, ModelDriven<Customer> {

    private static final Logger logger = Logger.getLogger(CustomerAction.class);

    private AtomicLong atomicLong = null;
    private String startString;

    private Customer customer = new Customer();
    protected Map session;

    public Customer getModel() {
        return customer;
    }

    public Map getSession() {
        return session;
    }

    public void setSession(Map sess) {
        this.session = sess;
    }

    @Override
    @SkipValidation
    public String execute() {
        logger.info(" in execure action !! ");

        return SUCCESS;
    }


    @Override
    public void prepare() throws Exception {
        // TODO Auto-generated method stub
        if (!session.isEmpty()) {
            logger.info("inside prepare ::::::");
            customer = (Customer) session.get("CUSTOMER");
            // logger.info("---------------" + customer.getEmail());
            CustomerDAO customerDAO = CustomerDAOFactory.getCustomerDAO();
            customer = customerDAO.getCustomerDetails(customer.getEmail(), "");
        }

    }

    @Validations(requiredStrings = {
            @RequiredStringValidator(fieldName = "company_name", type = ValidatorType.FIELD, message = "Company name is required"),
            @RequiredStringValidator(fieldName = "mobile", type = ValidatorType.FIELD, message = "Mobile is required"),
            @RequiredStringValidator(fieldName = "email", type = ValidatorType.FIELD, message = "Email is required") }, emails = { @EmailValidator(type = ValidatorType.SIMPLE, fieldName = "email", message = "Please enter valid email.") })
    // ,@RequiredStringValidator(fieldName = "password", type = ValidatorType.FIELD, message = "Password is required")
    public String addCustomer() {

        // add customer code

        return SUCCESS;
    }

    public String editCustomer() {

        //edit customer code

        return SUCCESS;
    }

    public Customer getCustomer() {
        return customer;
    }

    public void setCustomer(Customer customer) {
        this.customer = customer;
    }
}

Upvotes: 2

Views: 1747

Answers (1)

rajub
rajub

Reputation: 320

I get the solution. thanks for all response. Solution is as below, i added validation annotation method only in struts.xml file then it works fine.

    <action name="createCustomer" class="com.struts.action.CustomerAction" method="createCustomer">
        <interceptor-ref name="defaultStack">
            <param name="validation.validateAnnotatedMethodOnly">true</param>
            <param name="validation.excludeMethods">editProfile</param>
        </interceptor-ref>  
        <result name="success" type="tiles">/register.tiles</result>
        <result name="input" type="tiles">/register.tiles</result>      
    </action>


    <action name="editProfile" class="com.struts.action.CustomerAction" method="editProfile">
        <interceptor-ref name="defaultStack">
            <param name="validation.validateAnnotatedMethodOnly">true</param>
            <param name="validation.excludeMethods">createCustomer</param>
        </interceptor-ref>          
        <result name="success" type="tiles">/welcome.tiles</result>
        <result name="input" type="tiles">/welcome.tiles</result>
    </action>   

Upvotes: 0

Related Questions