官术网_书友最值得收藏!

  • Jakarta EE Cookbook
  • Elder Moraes
  • 209字
  • 2021-06-24 16:12:44

How it works...

Let's check each declared constraint:

    @NotBlank (message = "Name should not be blank")
@Size (min = 4, max = 10,message = "Name should be between
4 and 10 characters")
private String name;

The @NotBlank annotation will deny not only null values, but also whitespace values, and @Size speaks for itself:

    @Email (message = "Invalid e-mail format")
@NotBlank (message = "E-mail shoud not be blank")
private String email;

The @Email constraint will check the email string format:

    @PastOrPresent (message = "Created date should be past or present")
@NotNull (message = "Create date should not be null")
private LocalDate created;

@PastOrPresent will constrain LocalDate to be in the past or until the present date. It can't be in the future.

Here, we can't use @NotBlank as there is no blank date, only null, so we avoid this by using @NotNull:

    @Future (message = "Expires should be a future date")
@NotNull (message = "Expires should not be null")
private LocalDate expires;

This is the same as the preceding constraint, but constraints for a future date.

In our UI, there are two places we should take a careful look at:

 <h:inputText id="created" value="#{user.created}">
<f:convertDateTime type="localDate" pattern="dd/MM/uuuu" />
</h:inputText>

...

<h:inputText id="expire" value="#{user.expires}">
<f:convertDateTime type="localDate" pattern="dd/MM/uuuu" />
</h:inputText>

Here, we are using convertDateTime to automatically convert the data that's inputted into inputText according to the dd/MM/uuuu pattern.

主站蜘蛛池模板: 河津市| 嘉义市| 汤阴县| 平遥县| 巴中市| 利津县| 沙湾县| 清水河县| 岳池县| 柏乡县| 万载县| 湄潭县| 定结县| 四子王旗| 舞阳县| 荔浦县| 晴隆县| 通州区| 武定县| 泾阳县| 称多县| 武清区| 奇台县| 长武县| 富顺县| 锦屏县| 监利县| 东港市| 景宁| 博白县| 上犹县| 额尔古纳市| 通城县| 比如县| 安陆市| 中西区| 宁化县| 高阳县| 遵义市| 兴化市| 蕉岭县|