Jothi
Jothi

Reputation: 15090

How to exclude action methods from validation in struts2

My Action class have the following methods,

1.add
2.edit
3.loadEdit
4.remove
5.list
6.execute

in this i need to apply validation for add and edit..how do need to config in struts.xml.I followed,

<action name="editComment" method="edit"
        class="com.mmm.ehspreg2.web.action.product.CommentAction">
    <result name="success">/jsp/propertyManager/loadList.jsp</result>
</action>
<action name="removeComment" method="remove"
        class="com.mmm.ehspreg2.web.action.product.CommentAction">
    <interceptor-ref name="validation">
        <param name="excludeMethods">remove</param>
    </interceptor-ref>
    <result type="tiles">listComment</result>
    <result type="tiles" name="input">listComment</result>
</action>

When I configure it like this, remove action method is not getting called. I don't understand the problem. Please assist.

Upvotes: 4

Views: 13563

Answers (2)

Nitesh
Nitesh

Reputation: 101

you can also use @SkipValidation before method initialization in action class

e.g.

@SkipValidation
public String save() {
    String result = super.save();
    if (result.equals(SAVE)) {
        setMessage1(getText("save.successful"));
    } else {
        setMessage1(getText("save.unsuccessful"));
    }
    jsonResponse = new Hashtable<String, Object>();
    jsonResponse.put(FIELD_JSONRESPONSE_STATUS,
            KEY_JSONRESPONSE_MESSAGE_SUCCESS);
    jsonResponse.put(FIELD_JSONRESPONSE_MESSAGE,
            KEY_JSONRESPONSE_EMPTY_STRING);
    jsonResponse.put(FIELD_JSONRESPONSE_VALUE, domainModel.getId());
    // System.out.println("domainModel>>>>>>" + domainModel.getId());
    return result;
}

Upvotes: 10

Pat
Pat

Reputation: 25675

Simply list all the methods you don't want to be run through the validation framework in the excludeMethods parameter. Since you only want add and edit validated, list the other 4 as follows:

<interceptor-ref name="validation">
    <param name="excludeMethods">loadEdit,remove,list,execute</param>
</interceptor-ref>

You can read more about it in the Validation Interceptor docs.

Upvotes: 8

Related Questions