springMVC封装请求bean
2015-10-14 10:34
543 查看
前台请求发送数据到后台,请求data中的名字与bean中字段相等,可直接使用springMVC封装,并可验证。
control
@Valid 意思是在把数据绑定到@ModelAttribute 后就进行验证。
BindingResult 为验证结果。
bean
control
@RequestMapping("applyShop") @ResponseBody public Object applyShop(@Valid @ModelAttribute TCk ck, BindingResult result) { if (result.hasErrors()) { return CommentUtil.returnResultMap(0, false, "hzc-exceptions:arguments are wrong"); } int rs = ickServer.saveCkInfo(ck); if (rs > 0) { return CommentUtil.returnResultMap(1, true, "申请成功"); } else { return CommentUtil.returnResultMap(0, false, "申请失败"); } }
@Valid 意思是在把数据绑定到@ModelAttribute 后就进行验证。
BindingResult 为验证结果。
bean
public class TCk { @NotEmpty private String realname; public String getRealname() { return realname; } public void setRealname(String realname) { this.realname = realname == null ? null : realname.trim(); } }
下面是主要的验证注解及说明: 注解 适用的数据类型 说明 @AssertFalse Boolean, boolean 验证注解的元素值是false @AssertTrue Boolean, boolean 验证注解的元素值是true @DecimalMax(value=x) BigDecimal, BigInteger, String, byte,short, int, long and the respective wrappers of the primitive types. Additionally supported by HV: any sub-type of Number andCharSequence. 验证注解的元素值小于等于@ DecimalMax指定的value值 @DecimalMin(value=x) BigDecimal, BigInteger, String, byte,short, int, long and the respective wrappers of the primitive types. Additionally supported by HV: any sub-type of Number andCharSequence. 验证注解的元素值小于等于@ DecimalMin指定的value值 @Digits(integer=整数位数, fraction=小数位数) BigDecimal, BigInteger, String, byte,short, int, long and the respective wrappers of the primitive types. Additionally supported by HV: any sub-type of Number andCharSequence. 验证注解的元素值的整数位数和小数位数上限 @Future java.util.Date, java.util.Calendar; Additionally supported by HV, if theJoda Time date/time API is on the class path: any implementations ofReadablePartial andReadableInstant. 验证注解的元素值(日期类型)比当前时间晚 @Max(value=x) BigDecimal, BigInteger, byte, short,int, long and the respective wrappers of the primitive types. Additionally supported by HV: any sub-type ofCharSequence (the numeric value represented by the character sequence is evaluated), any sub-type of Number. 验证注解的元素值小于等于@Max指定的value值 @Min(value=x) BigDecimal, BigInteger, byte, short,int, long and the respective wrappers of the primitive types. Additionally supported by HV: any sub-type of CharSequence (the numeric value represented by the char sequence is evaluated), any sub-type of Number. 验证注解的元素值大于等于@Min指定的value值 @NotNull Any type 验证注解的元素值不是null @Null Any type 验证注解的元素值是null @Past java.util.Date, java.util.Calendar; Additionally supported by HV, if theJoda Time date/time API is on the class path: any implementations ofReadablePartial andReadableInstant. 验证注解的元素值(日期类型)比当前时间早 @Pattern(regex=正则表达式, flag=) String. Additionally supported by HV: any sub-type of CharSequence. 验证注解的元素值与指定的正则表达式匹配 @Size(min=最小值, max=最大值) String, Collection, Map and arrays. Additionally supported by HV: any sub-type of CharSequence. 验证注解的元素值的在min和max(包含)指定区间之内,如字符长度、集合大小 @Valid Any non-primitive type(引用类型) 验证关联的对象,如账户对象里有一个订单对象,指定验证订单对象 @NotEmpty CharSequence,Collection, Map and Arrays 验证注解的元素值不为null且不为空(字符串长度不为0、集合大小不为0) @Range(min=最小值, max=最大值) CharSequence, Collection, Map and Arrays,BigDecimal, BigInteger, CharSequence, byte, short, int, long and the respective wrappers of the primitive types 验证注解的元素值在最小值和最大值之间 @NotBlank CharSequence 验证注解的元素值不为空(不为null、去除首位空格后长度为0),不同于@NotEmpty,@NotBlank只应用于字符串且在比较时会去除字符串的空格 @Length(min=下限, max=上限) CharSequence 验证注解的元素值长度在min和max区间内 @Email CharSequence 验证注解的元素值是Email,也可以通过正则表达式和flag指定自定义的email格式
相关文章推荐
- 我是运营,我没有假期
- DB2数据库的安装
- C#实现把指定数据写入串口
- “传奇”图象数据存储方式
- 修复mysql数据库
- SQLServer 数据导入导出的几种方法小结
- MySQL数据备份之mysqldump的使用详解
- C#实现窗体间传递数据实例
- 给你的数据库文件减肥
- Oracle数据更改后出错的解决方法
- C#将Sql数据保存到Excel文件中的方法
- MFC实现在文件尾追加数据的方法
- 自己简单封装的一个CDialog类实例
- 把excel表格里的数据导入sql数据库的两种方法
- 用文本作数据处理
- 桌面中心(一)创建数据库
- 桌面中心(四)数据显示
- PHP+JS实现大规模数据提交的方法
- PHP类的封装与继承详解
- C#数据绑定(DataBinding)简单实现方法