- Jakarta EE Cookbook
- Elder Moraes
- 197字
- 2021-06-24 16:12:43
How to do it...
You need to perform the following steps to complete this recipe:
- Let's create a User object that will be attached to our JSF page:
@Named
@RequestScoped
public class User {
@NotBlank (message = "Name should not be blank")
@Size (min = 4, max = 10,message = "Name should be between
4 and 10 characters")
private String name;
@Email (message = "Invalid e-mail format")
@NotBlank (message = "E-mail should not be blank")
private String email;
@PastOrPresent (message = "Created date should be
past or present")
@NotNull (message = "Create date should not be null")
private LocalDate created;
@Future (message = "Expires should be a future date")
@NotNull (message = "Expires should not be null")
private LocalDate expires;
//DO NOT FORGET TO IMPLEMENT THE GETTERS AND SETTERS
...
- Now, we need to define the method that will be fired once all the data is valid:
public void valid(){
FacesContext
.getCurrentInstance()
.addMessage(
null,
new FacesMessage(FacesMessage.SEVERITY_INFO,
"Your data is valid", ""));
}
- Now, our JSF page references each User class field that's been declared, as follows:
<h:body>
<h:form>
<h:outputLabel for="name" value="Name" />
<h:inputText id="name" value="#{user.name}" />
<br/>
<h:outputLabel for="email" value="E-mail" />
<h:inputText id="email" value="#{user.email}" />
<br/>
<h:outputLabel for="created" value="Created" />
<h:inputText id="created" value="#{user.created}">
<f:convertDateTime type="localDate" pattern="dd/MM/uuuu" />
</h:inputText>
<br/>
<h:outputLabel for="expire" value="Expire" />
<h:inputText id="expire" value="#{user.expires}">
<f:convertDateTime type="localDate" pattern="dd/MM/uuuu" />
</h:inputText>
<br/>
<h:commandButton value="submit" type="submit" action="#{user.valid()}" />
</h:form>
</h:body>
If you run this code, all the fields will be validated once you click the Submit button. Try it for yourself!
推薦閱讀
- 零基礎學Scratch少兒編程:小學課本中的Scratch創意編程
- Django:Web Development with Python
- 薛定宇教授大講堂(卷Ⅳ):MATLAB最優化計算
- Hands-On C++ Game Animation Programming
- 小程序開發原理與實戰
- Yocto for Raspberry Pi
- 軟件測試實用教程
- C語言程序設計
- Python機器學習:預測分析核心算法
- Illustrator CC平面設計實戰從入門到精通(視頻自學全彩版)
- 算法設計與分析:基于C++編程語言的描述
- PHP 8從入門到精通(視頻教學版)
- Learning Concurrency in Python
- Developing Java Applications with Spring and Spring Boot
- 一覽眾山小:ASP.NET Web開發修行實錄