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

How to do it...

You need to perform the following steps to complete this recipe:

  1. 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

...
  1. 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", ""));
}
  1. 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!

主站蜘蛛池模板: 淳化县| 秀山| 乌鲁木齐市| 林周县| 祁连县| 班戈县| 铜川市| 剑阁县| 双峰县| 富锦市| 栾城县| 无棣县| 建瓯市| 固阳县| 天柱县| 诸暨市| 日喀则市| 怀柔区| 天门市| 西和县| 兴和县| 确山县| 惠州市| 务川| 隆尧县| 霍林郭勒市| 牡丹江市| 闽侯县| 弥渡县| 高阳县| 大田县| 芜湖市| 香格里拉县| 洱源县| 水城县| 疏勒县| 仲巴县| 达州市| 民县| 句容市| 泰和县|