---
title: "DispatchAction 和DynaValidateActionForm 结合使用时的问题"
author: "NicholasBugs"
date: "2008-05-30T18:03:51Z"
tags: []
categories: ["企业架构"]
slug: "dispatchaction-dynavalidateactionform"
draft: false
lastmod: "2008-05-30T18:03:51Z"
id: 198774
url: "https://nicholasbugs.iteye.com/blog/198774"
---

使用DispatchAction使得程序员能够大大减少action class的数量，但是在和Validation框架配合使用时，可能会遇到这样的问题：
程序员的本意是只有在action中的某个方法中才使用validation框架校验，但是只要把struts-config.xml中 <action />标签中的validate设为true,那么进入该action的所有方法之前都会进行校验，很痛苦。现在有两个办法：
1，在stuts-config.xml中对一个class 配多个<action/>标签， 比如：
<action path="/saveUser" validate=true .....  type="UserAction" />
<action path="/initUser" validate=false.....  type="UserAction" />
只在需要的action里将validate配成true
当然这只是权宜之计。
2：在validation.xml 中将form的name设置成需要的进行校验的action的path

```java

<formset>
 <form name="/user.do?method=save">
  <field>.... </filed>
 </form>
</formset>

```

这样只有在save（）方法调用时才会校验。

update:除了这个，还有另外一个解决方法：
令struts-config.xml中的validate=false，然后在自己的action中需要校验的地方追加以下代码：

```java

ActionMessages errors = form.validate(mapping, request);//在需要的地方调用validate()方法，其中validate（）方法的校验规则已经定义在validation.xml中了
if(!errors.isEmpty()){
    saveErrors(reqeust, errors);
   return mapping.getInputForward(); 
}

```