13.07.2015 Views

JSR 303 的参考实现使用指南 - JBoss

JSR 303 的参考实现使用指南 - JBoss

JSR 303 的参考实现使用指南 - JBoss

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

第 3 章 创 建 自 己 的 约 束 规 则}if(!isValid) {constraintValidatorContext.buildConstraintViolationWithTemplate( "{my.custom.template}" ).addNode( "myProperty" ).addConstraintViolation();}return isValid;3.1.3. 校 验 错 误 信 息最 后 , 我 们 还 需 要 指 定 如 果 @CheckCase 这 个 约 束 条 件 验 证 的 时 候 , 没 有 通 过 的 话 的 校 验 错 误 信 息 .我 们 可 以 添 加 下 面 的 内 容 到 我 们 项 目 自 定 义 的 ValidationMessages.properties ( 参 考 第 2.2.4 节“ 验 证 失 败 提 示 信 息 解 析 ”) 文 件 中 .例 3.6. 为 CheckCase 约 束 定 义 一 个 错 误 信 息com.mycompany.constraints.CheckCase.message=Case mode must be {value}.如 果 发 现 校 验 错 误 了 的 话 , 你 所 使 用 的 Bean Validation 的 实 现 会 用 我 们 定 义 在 @CheckCase 中message 属 性 上 的 值 作 为 键 到 这 个 文 件 中 去 查 找 对 应 的 错 误 信 息 .3.1.4. 应 用 约 束 条 件现 在 我 们 已 经 有 了 一 个 自 定 义 的 约 束 条 件 了 , 我 们 可 以 把 它 用 在 第 1 章 开 始 入 门 中 的 Car 类 上 ,来 校 验 此 类 的 licensePlate 属 性 的 值 是 否 全 都 是 大 写 字 母 .例 3.7. 应 用 CheckCase 约 束 条 件package com.mycompany;import javax.validation.constraints.Min;import javax.validation.constraints.NotNull;import javax.validation.constraints.Size;public class Car {@NotNullprivate String manufacturer;@NotNull@Size(min = 2, max = 14)@CheckCase(CaseMode.UPPER)private String licensePlate;@Min(2)private int seatCount;public Car(String manufacturer, String licencePlate, int seatCount) {this.manufacturer = manufacturer;34

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!