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="org.hibernate.validator.HibernateValidator"/>  
	</bean>


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="org.hibernate.validator.HibernateValidator"/>  
	</bean>




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;
	}

}



你可能感兴趣的:(springMvc 整合hibernate-validator)