2014-02-25 37 views
2

我是struts2中的新成員,如何使用註釋實現動作(方法)級別驗證。 我的動作類有兩種方法,添加和編輯。 當呼叫添加動作,然後我想要驗證所有字段,但是當我呼叫編輯動作,然後我不想驗證所有字段。 所以我添加了基於添加和編輯方法的基於註釋的驗證。 它的工作增加,但是當我提交編輯它試圖驗證所有字段(添加+編輯方法)。 我已添加類級別validateAnnotatedMethodOnly註釋,但仍然給出相同的問題。struts2使用多種方法在同一動作中進行註釋驗證

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

我怎樣才能解決這個問題,請大家幫我

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

下面是完整的代碼

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

如果你排除它,它是如何工作的? –

+0

如果您在添加客戶時需要驗證密碼,爲什麼您評論它只是將其添加到'requiredStrings'中?在editCustomer上也使用相同的驗證,只需刪除'passowrd'驗證 – Yogesh

+0

我試過了,所以它在驗證密碼的同時添加了客戶。 在editCustomer中,我添加了名稱,移動和電子郵件驗證,但不起作用,因爲它也驗證了密碼。 請任何人都幫助我。 – rajub

回答

0

我得到的解決方案。感謝所有的迴應。 解決方案如下,我只在struts.xml文件中添加了驗證註釋方法,然後它工作正常。

<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> 
相關問題