2010-07-22 51 views
4

我的Action类有以下几种方法,如何排除验证在Struts2的操作方法

1.add 
2.edit 
3.loadEdit 
4.remove 
5.list 
6.execute 
在这方面,我需要申请验证的添加和edit..how确实需要在struts.xml中以配置

。我跟着,

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

当我像这样配置它,删除操作方法不会被调用。我不明白这个问题。请协助。

回答

8

只需列出所有不需要的方法想要通过excludeMethods参数中的验证框架运行。由于您只想addedit确认,列表中的其他4如下:

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

您可以在Validation Interceptor docs阅读更多关于它。

+0

它不工作。我对每种方法都有不同的操作。我需要在所有操作中指定这个excludethods配置吗? – Jothi 2010-07-23 04:21:01

+1

不,您可以在struts.xml的顶部定义自己的defaultStack。如果您在此页面上查看“defaultStack”,您会看到当前正在运行的内容。将其复制到您自己的配置中并更改所需内容:http://struts.apache.org/2.0.14/docs/struts-defaultxml.html – Pat 2010-07-23 10:23:39

9

你也可以初始化方法之前,动作类

例如使用@SkipValidation

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