jsf入门实例
In this section, we will see how to use the default JSF validators to shoot out the built in error messages to the users.
在本节中,我们将看到如何使用默认的JSF验证器向用户发出内置的错误消息。
Some of the points to keep in mind before we write the xhtml file are:
在编写xhtml文件之前,需要记住以下几点:
The h:message
tag is used to display all the error messages related to the UI components.
h:message
标记用于显示与UI组件相关的所有错误消息。
This h:message
has the following attributes
此h:message
具有以下属性
id attribute is the unique identifier for a ui component.
id属性是ui组件的唯一标识符。
style displays the style information like color,font etc
样式显示样式信息,例如颜色,字体等
for attribute describes the component name applicable for the form.
for属性描述适用于表单的组件名称。
Let’s look in detail with an example as how to display the error messages
让我们以一个示例的方式详细查看如何显示错误消息
Create a JSF page named error.xhtml
as
创建一个名为error.xhtml
的JSF页面, error.xhtml
:
error.xhtml
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>
In the above JSF page the required=true is set so that the field is mandatory and the validation message is displayed from a file called messages.properties
which is present in the JSF jars.
在上面的JSF页面中,设置了required = true ,以便该字段为必填字段,并且从JSF jar中存在的名为messages.properties
的文件中显示验证消息。
For minimum and maximum length validation we use <c:validateLength> tag which validates against the minimum and maximum number of characters and prints the validations from the messages.properties
file.
对于最小和最大长度验证,我们使用<c:validateLength>标记,该标记针对最小和最大字符数进行验证,并从messages.properties
文件中打印验证。
The validation for price field works in a similar way the difference being that the entered value is checked for double data type.
价格字段的验证以类似的方式工作,不同之处在于检查输入值是否为double数据类型。
The radio button validator checks if the user has specified one of Petrol or Diesel engine and if none, an error is flashed on the screen. The date validator checks if a correct date format was entered by the user. In all these cases, we don’t have to write our own logic of field validation since JSF provides built in features for these kinds of common cases.
单选按钮验证器检查用户是否指定了汽油或柴油发动机之一,如果没有,则在屏幕上闪烁一个错误。 日期验证器检查用户是否输入了正确的日期格式。 在所有这些情况下,由于JSF为这些常见情况提供了内置功能,因此我们不必编写自己的字段验证逻辑。
Now let’s create a managed bean Car.java
as
现在让我们创建一个托管bean Car.java
如下所示:
package com.journaldev.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;
}
}
Once done with this changes run the code and following output should be produced in the browser
完成此更改后,运行代码,并在浏览器中生成以下输出
The project structure is as below image.
项目结构如下图。
This post is all about handling JSF Error Messages with the help of JSF validators. We will be looking into JSF Page Navigation in the coming tutorial. In the mean time, you can download the project from below link and play around with it to learn more.
这篇文章是关于在JSF验证程序的帮助下处理JSF错误消息的。 在接下来的教程中,我们将研究JSF页面导航。 同时,您可以从下面的链接下载该项目并进行试用以了解更多信息。
翻译自: https://www.journaldev.com/6701/jsf-error-messages-example-tutorial
jsf入门实例