2014-02-25 33 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> 
相关问题