首页 > 代码库 > 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; } }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。