首页 > 代码库 > springMvc 整合hibernate-validator

springMvc 整合hibernate-validator

maven 引入

<dependency>
		  <groupId>org.hibernate</groupId>
		  <artifactId>hibernate-validator</artifactId>
		  <version>4.0.1.GA</version>
		</dependency>
		<dependency>
		  <groupId>javax.validation</groupId>
		  <artifactId>validation-api</artifactId>
		  <version>1.1.0.Final</version>
		</dependency>




spring mvc 配置

<mvc:annotation-driven validator="validator"/>  
	  
	<!-- 以下 validator  ConversionService 在使用 mvc:annotation-driven 会 自动注册-->  
	<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">  
	    <property name="providerClass" value=http://www.mamicode.com/"org.hibernate.validator.HibernateValidator"/>  > 


controller

<!-- 指定自己定义的validator -->  
	
	<mvc:annotation-driven validator="validator"/>  
	  
	<!-- 以下 validator  ConversionService 在使用 mvc:annotation-driven 会 自动注册-->  
	<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean">  
	    <property name="providerClass" value=http://www.mamicode.com/"org.hibernate.validator.HibernateValidator"/>  > 




orm bean

@Entity
@org.hibernate.annotations.Entity(selectBeforeUpdate = true,dynamicUpdate=true)
@Table(name = "test1")
@Cache(usage=CacheConcurrencyStrategy.READ_WRITE)
@CRUD
public class Test1 extends PO{
	private String id;
	@NotEmpty(message = "姓名不能为空!")
	@GenericGenerator(name = "generator", strategy = "uuid.hex")
	@Id
	@GeneratedValue(generator = "generator")
	@Column(name = "id", unique = true, nullable = false)
	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

}