2010-07-22 54 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; 
}