JSF 验证模型定义了一套用于验证 UI 组件的标准类。 JSF 库定义了一组核心标签,该标签与 javax.faces.validator.Validator
实现相匹配。除了标准错误消息验证模型,我们可以定义自定义验证。
JSF 验证 - 声明验证器
使用 JSF 标准验证器或 Bean 验证器进行的验证属于声明类型。JSF 标准验证器的例子是长度验证器、必要验证器等。
1<?xml version='1.0' encoding='UTF-8' ?>
2<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3<html xmlns="https://www.w3.org/1999/xhtml"
4 xmlns:h="https://java.sun.com/jsf/html">
5<h:head>
6</h:head>
7<h:body>
8 <h3>Add Mobile Details</h3>
9 <h:form>
10 <h:panelGrid columns="3">
11 <h:outputLabel for="mname">Mobile Name:</h:outputLabel>
12 <h:inputText id="mname" required="true"
13 requiredMessage="Mobile Name is mandatory"></h:inputText>
14 <br>
15 <br>
16
17 <h:outputLabel for="color">Color:</h:outputLabel>
18 <h:inputText id="color" required="true"></h:inputText>
19 <br>
20 <br>
21
22 <h:outputLabel for="model">Model Number:</h:outputLabel>
23 <h:inputText id="model"></h:inputText>
24 <br>
25 <br>
26
27 <h:commandButton value="Submit"></h:commandButton>
28 </h:panelGrid>
29 </h:form>
30</h:body>
31</html>
Here we are setting the required attribute to true which makes the field mandatory and fires the custom message value is required
for color field and user defined message for mobile name field as the message is specified in the requiredmessage attribute. Run the application and you will see the below output on pressing submit button.
JSF强制性验证
标准验证消息在所有情况下都不够,有时可能需要复杂的验证。
- 从 Bean 方法 射击验证2.在运行时使用类中的 @FacesValidator 注释
从 Bean 方法执行验证 在这种类型的验证中,我们在 Bean 中写一种方法来验证 UIComponents,并通过 inputText 标签中的验证属性从 jsf 页面调用这种方法。
1<?xml version='1.0' encoding='UTF-8' ?>
2<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3<html xmlns="https://www.w3.org/1999/xhtml"
4 xmlns:h="https://java.sun.com/jsf/html">
5<h:head>
6</h:head>
7<h:body>
8 <h3>Add Mobile Details</h3>
9 <h:form>
10 <h:outputLabel for="mno">Model Number:</h:outputLabel>
11 <h:inputText id="mno" value="#{mobile.mno}" required="true" size="4"
12 disabled="#{mobile.mno}" validator="#{mobile.validateModelNo}">
13 </h:inputText>
14 <h:commandButton value="Submit"></h:commandButton>
15 </h:form>
16</h:body>
17</html>
在本页面中,我们正在调用 validateModelno 方法的 java 豆在验证标签属性中。
1package com.journaldev.jsf.bean;
2
3import java.io.Serializable;
4import javax.faces.application.FacesMessage;
5import javax.faces.bean.ManagedBean;
6import javax.faces.bean.SessionScoped;
7import javax.faces.component.UIComponent;
8import javax.faces.component.UIInput;
9import javax.faces.context.FacesContext;
10
11@ManagedBean
12@SessionScoped
13public class Mobile implements Serializable {
14
15 private static final long serialVersionUID = -7250065889869767422L;
16
17 // @NotNull(message="Please enter the model number")
18 private String mno;
19
20 public String getMno() {
21 return mno;
22 }
23
24 public void setMno(String mno) {
25 this.mno = mno;
26 }
27
28 public void validateModelNo(FacesContext context, UIComponent comp,
29 Object value) {
30
31 System.out.println("inside validate method");
32
33 String mno = (String) value;
34
35 if (mno.length() < 4) {
36 ((UIInput) comp).setValid(false);
37
38 FacesMessage message = new FacesMessage(
39 "Minimum length of model number is 4");
40 context.addMessage(comp.getClientId(context), message);
41
42 }
43
44 }
45
46}
Here we are checking for the length of the model no and if the length is less than 4 we are specifying the message as Minimum length of model number is 4
. Now Run the application which produces the following output.
在 Bean 中使用 @FacesValidator - 自定义 JSF 验证器
在此方法中,我们使用 @FacesValidator 注释,指定验证器的名称,并通过超级验证方法来执行验证器。
1<?xml version='1.0' encoding='UTF-8' ?>
2<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3<html xmlns="https://www.w3.org/1999/xhtml"
4 xmlns:h="https://xmlns.jcp.org/jsf/html"
5 xmlns:f="https://java.sun.com/jsf/core">
6<h:head>
7</h:head>
8<h:body>
9 <h:form>
10 <h3>Validation using Faces validator</h3>
11 <h:outputLabel for="mno" value="Model Number: " />
12 <h:inputText id="mno" value="#{mobileValidator.mno}">
13 <f:validator validatorId="mobileValidator" />
14 </h:inputText>
15 <h:message for="mno" style="color:blue" />
16 <p></p>
17 <h:commandButton value="Submit"></h:commandButton>
18 </h:form>
19</h:body>
20</html>
在此,我们正在调用名为mobileValidator
的自定义验证器,在 <f:validator> 标签的 validatorId 属性中。
1package com.journaldev.jsf.bean;
2
3import javax.faces.application.FacesMessage;
4import javax.faces.bean.ManagedBean;
5import javax.faces.bean.SessionScoped;
6import javax.faces.component.UIComponent;
7import javax.faces.context.FacesContext;
8import javax.faces.validator.FacesValidator;
9import javax.faces.validator.Validator;
10import javax.faces.validator.ValidatorException;
11
12@ManagedBean
13@SessionScoped
14@FacesValidator("mobileValidator")
15public class MobileValidator implements Validator {
16
17 private String mno;
18
19 public String getMno() {
20 return mno;
21 }
22
23 public void setMno(String mno) {
24 this.mno = mno;
25 }
26
27 int maximumlength = 6;
28
29 public MobileValidator() {
30 }
31
32 @Override
33 public void validate(FacesContext fc, UIComponent uic, Object obj)
34 throws ValidatorException {
35
36 String model = (String) obj;
37
38 if (model.length() > 6) {
39 FacesMessage msg = new FacesMessage(
40 " Maximum Length of 6 is exceeded.Please enter values within range");
41 msg.setSeverity(FacesMessage.SEVERITY_ERROR);
42
43 throw new ValidatorException(msg);
44 }
45
46 }
47
48}
Here we override the standard validate method and implement our own logic for validating the input fields. Run the application and see the output as shown below. Finally below image shows the project structure.
You can download the project from below link and play around with it to learn more.