Java对象校验框架之Oval

编写登录 注册等功能的时候后台经行校验,可以使用oval框架,简单实用

package com.meadin.main.request;


import com.meadin.module.base.BaseRequest;


import net.sf.oval.constraint.Length;
import net.sf.oval.constraint.NotEmpty;
import net.sf.oval.constraint.NotNull;


public class VisitorRequest extends BaseRequest {
@NotNull
@NotEmpty
private String contacts;
@NotNull
@NotEmpty
@Length(min = 11, max = 11)//电话号码长度的验证
private String phone;
@NotNull
@NotEmpty
private String company;
@NotNull
@NotEmpty
private String duty;
@NotNull
@NotEmpty
private long systemCode;
@NotNull
@NotEmpty

@Length( max = 100)
private String remak;


public String getContacts() {
return contacts;
}


public void setContacts(String contacts) {
this.contacts = contacts == null ? null : contacts.trim();
}


public String getPhone() {
return phone;
}


public void setPhone(String phone) {
this.phone = phone == null ? null : phone.trim();
}


public String getCompany() {
return company;
}


public void setCompany(String company) {
this.company = company == null ? null : company.trim();
}


public String getDuty() {
return duty;
}


public void setDuty(String duty) {
this.duty = duty == null ? null : duty.trim();
}


public long getSystemCode() {
return systemCode;
}


public void setSystemCode(long systemCode) {
this.systemCode = systemCode;
}


public String getRemak() {
return remak;
}


public void setRemak(String remak) {
this.remak = remak == null ? null : remak.trim();
}


}

校验的代码



@RequestMapping(value = "/")
@ResponseBody
public Object submit(VisitorRequest visitor, String callback) throws Exception {
// 对传入的对象经行验证
Validator validator = new Validator();
List list = validator.validate(visitor);

//只要返回集合中有元素就说明有不合法的参数
if (list.size() > 0) {
String result = callback + "(" + JSONObject.toJSON(ApiResult.build(0, "参数错误!!!")).toString() + ");";
return result;
}

}

你可能感兴趣的:(Java对象校验框架之Oval)