springMVC3 基于注解的输入验证
来源:互联网 发布:sql增量备份还原 编辑:程序博客网 时间:2024/06/06 02:20
在pom.xml中加入,他需要validation-api.jar,hibernate-validator两个开发包
<!-- JSR 303 with Hibernate Validator -->
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.0.0.GA</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>4.0.2.GA</version>
</dependency>
.....再加入
<repositories>
<!-- For JSR 303 and Hibernate Validator only - Encourage JBoss to publish these artifacts to Maven Central! -->
<repository>
<id>org.jboss.repository.maven</id>
<url>http://repository.jboss.org/maven2</url>
<snapshots><enabled>false</enabled></snapshots>
</repository>
</repositories>
类的一个例子
package org.springframework.samples.mvc.basic.account;
import java.math.BigDecimal;
import java.util.Date;
import java.util.concurrent.atomic.AtomicLong;
import javax.validation.constraints.Future;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.format.annotation.DateTimeFormat.ISO;
import org.springframework.format.annotation.NumberFormat;
import org.springframework.format.annotation.NumberFormat.Style;
public class Account {
private Long id;
@NotNull
@Size(min=1, max=25)
private String name;
@NotNull
@NumberFormat(style=Style.CURRENCY)
private BigDecimal balance = new BigDecimal("1000");
@NotNull
@NumberFormat(style=Style.PERCENT)
private BigDecimal equityAllocation = new BigDecimal(".60");
@DateTimeFormat(iso=ISO.DATE)
@Future
private Date renewalDate = new Date(new Date().getTime() + 31536000000L);
public Long getId() {
return id;
}
void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public BigDecimal getBalance() {
return balance;
}
public void setBalance(BigDecimal balance) {
this.balance = balance;
}
public BigDecimal getEquityAllocation() {
return equityAllocation;
}
public void setEquityAllocation(BigDecimal equityAllocation) {
this.equityAllocation = equityAllocation;
}
public Date getRenewalDate() {
return renewalDate;
}
public void setRenewalDate(Date renewalDate) {
this.renewalDate = renewalDate;
}
Long assignId() {
this.id = idSequence.incrementAndGet();
return id;
}
private static final AtomicLong idSequence = new AtomicLong();
}
然后配置你自己的message就可以,我这里是messages_zh_CN.properties
- springMVC3 基于注解的输入验证
- 基于注解的输入校验
- 基于Struts2验证框架的输入验证
- 基于Aviator的注解驱动验证框架
- struts2基于验证框架的输入校验
- struts2 基于xml 配置文件的输入验证
- Springmvc3.0 注解详解
- springMVC3.0后台验证信息不显示的原因初探
- springMVC3.0后台验证信息不显示的原因初探
- 关于Struts2基于验证框架的输入校验的经验
- 输入框中基于javascript的数据格式的验证
- WebWork基于正则表达式(regex)的输入验证
- springMVC 注解方式 验证用户输入
- 第六章 注解式控制器详解——SpringMVC3强大的请求映射规则详解
- 第六章、SpringMVC学习-注解式控制器详解(2)-SpringMVC3强大的请求映射规则详解
- 第六章 注解式控制器详解--SpringMVC3强大的请求映射规则详解
- java SpringMVC3 的例子
- SpringMVC3.X的配置
- Java:编译时与运行时
- springmvc上传
- Ubuntu 用户及组管理
- 读码备忘录: boost:shared_mutex
- 第一个duilib程序 - 实现HelloWorld详解
- springMVC3 基于注解的输入验证
- MoveFileEx移动正在使用的文件(实际上是重启后移动)
- 定制jQuery File Upload为微博式单文件上传
- cp 的强制覆盖
- 使用VS2010进行Django开发
- 【转】socket TCP_DEFER_ACCEPT
- 深入理解MSP430单片机IO
- 共享问题!
- NPAPI插件开发学习:NPAPI的介绍