1.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>
2.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>
3.controller
4.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; } }
转 : https://my.oschina.net/orgsky/blog/296918
=================================================================
SpringMVC 数据验证 Hibernate-Vaildator --->对象验证
数据验证步骤:
1.测试环境的搭建:
2.验证器的注册
在springmvc.xml配置文件中加以下代码:
3.验证注解添加到对应实体类上
4.修改处理器
5.将验证失败信息写入到表单
index.jsp的写法:
转: https://www.cnblogs.com/hwgok/p/8001195.html