action收集页面数据采用的方式是ModelDriven,而不是在action添加属性!
现在我需要对登录信息进行验证,我想采用xml方式进行校验,可是在action中并没有
校验的属性,比如我需要对页面email校验,而action中并没有这个字段属性,
配好action-validation.xml后校验并没起到作用,下边是我的代码!
-----------action-------------
public class RegAction extends BaseAction<Users>{
public UsersService getUsersService() {
return usersService;
}
public void setUsersService(UsersService usersService) {
this.usersService = usersService;
}
@Resource
private UsersService usersService;
public String doReg() {
model.setPassword(new MD5Utils().getMD5(model.getPassword()));
usersService.saveEntity(model);
return "success";
}
public String toRegPage() {
return "toRegPage";
}
-----------------------baseAction-------------------------------
public class BaseAction<T> extends ActionSupport implements
ModelDriven<T>, Preparable {
public T model;
@SuppressWarnings({ "rawtypes", "unchecked" })
public BaseAction() {
try {
// new 是 java 的关键字,用来构造一个类的实例。而 newInstance 是 Class 的一个方法
java.lang.reflect.ParameterizedType type = (java.lang.reflect.ParameterizedType) this
.getClass().getGenericSuperclass();
Class clazz = (Class) type.getActualTypeArguments()[0];
model = (T) clazz.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
}
public void prepare() throws Exception {
}
public T getModel() {
return model;
}
------------------------------RegAction-validator.xml----------------------------
<validators>
<field name="email">
<field-validator type="equiredstring">
<param name="trim">true</param>
<message>用户名不能为空!</message>
</field-validator>
</field>
</validators>
---------------------
------解决方案--------------------
struts2校验太痛苦了。现在好多公司都不用了。。。。
------解决方案--------------------
都用jquery插件验证了。