JSF错误消息示例教程
时间:2020-02-23 14:33:43 来源:igfitidea点击:
在本节中,我们将看到如何使用默认的JSF验证器向用户发出内置的错误消息。
在编写xhtml文件之前,需要记住以下几点:
h:message标签用于显示与UI组件相关的所有错误消息。
这个h:message具有以下属性
id属性是ui组件的唯一标识符。
样式显示样式信息,例如颜色,字体等
for属性描述适用于表单的组件名称。
让我们通过一个示例详细显示如何显示错误消息
创建一个名为error.xhtml的JSF页面,如下所示:
error.xhtml
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="https://www.w3.org/1999/xhtml"
xmlns:h="https://java.sun.com/jsf/html"
xmlns:c="https://java.sun.com/jsf/core">
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
<h1>Standard Error messages</h1>
<h:form>
<h:panelGrid columns="3">
<h:outputLabel value="Car Id" for="Id"></h:outputLabel>
<h:inputText value="#{car.id}" id="Id" required="true" label="Car Id"></h:inputText>
<h:message for="Id" style="color: red"></h:message>
<h:outputLabel value="Car Name"></h:outputLabel>
<h:inputText value="#{car.cname}" id="cname" label="Car Name">
<c:validateLength minimum="5" maximum="10" for="cname" id="cname"
</h:inputText>
<h:message for="cname" style="color: red"></h:message>
<h:outputLabel value="Mfd Date" for="mfddate"></h:outputLabel>
<h:inputText value="#{car.mfddate}" id="mfddate" label="Mfd Date">
<c:convertDateTime
</h:inputText>
<h:message for="mfddate" style="color:red"></h:message>
<h:outputLabel value="Price" for="price"></h:outputLabel>
<h:inputText value="#{car.price}" id="price" label="Price">
<c:validateDoubleRange minimum="3.25" maximum="15.45" for="price"></c:validateDoubleRange>
</h:inputText>
<h:message for="price" style="color:red"></h:message>
<h:outputLabel value="Engine" for="engine"></h:outputLabel>
<h:selectOneRadio value="#{car.engine}" id="engine" required="true"
label="engine">
<c:selectItem itemValue="Petrol" itemLabel="Petrol"
<c:selectItem itemValue="Diesel" itemLabel="Diesel"
</h:selectOneRadio>
<h:message id="message" for="engine" style="color:red"></h:message>
<h:commandButton action="#{car.id}" value="Submit"></h:commandButton>
<br
<br
</h:panelGrid>
</h:form>
</h:body>
</html>
在上面的JSF页面中,设置了required = true,以便该字段为必填字段,并且从JSF jar中存在的名为messages.properties的文件中显示验证消息。
对于最小和最大长度验证,我们使用<c:validateLength>标记,该标记针对最小和最大字符数进行验证,并打印来自messages.properties文件的验证。
价格字段的验证以类似的方式工作,不同之处在于检查输入值是否为double数据类型。
单选按钮验证器检查用户是否指定了汽油或者柴油发动机之一,如果没有,则在屏幕上闪烁一个错误。
日期验证器检查用户是否输入了正确的日期格式。
在所有这些情况下,我们都不必编写自己的字段验证逻辑,因为JSF为这些常见情况提供了内置功能。
现在,我们创建一个托管beanCar.java,如下所示:
package com.theitroad.jsf.beans;
import java.util.Date;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.validation.constraints.NotNull;
@ManagedBean
@SessionScoped
public class Car {
private String cname;
private String color;
private String Id;
private String model;
private String regno;
private Date mfddate;
private Double price;
private String description;
@NotNull(message="Please select the engine type")
private String engine;
public String getEngine() {
return engine;
}
public void setEngine(String engine) {
this.engine = engine;
}
public String getColor() {
return color;
}
public void setColor(String color) {
this.color = color;
}
public String getCname() {
System.out.println("car name is"+cname);
return cname;
}
public void setCname(String cname) {
this.cname = cname;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getRegno() {
return regno;
}
public void setRegno(String regno) {
this.regno = regno;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public String getId() {
return Id;
}
public void setId(String Id) {
this.Id = Id;
}
public Date getMfddate() {
return mfddate;
}
public void setMfddate(Date mfddate) {
this.mfddate = mfddate;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
}
完成此更改后,运行代码.

