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页面导航。 同时,您可以从下面的链接下载该项目并进行试用以了解更多信息。

Download JSF Error Messages Project下载JSF错误消息项目

翻译自: https://www.journaldev.com/6701/jsf-error-messages-example-tutorial

jsf入门实例

jsf入门实例_JSF错误消息示例教程相关推荐

  1. jsf入门实例_JSF selectManyListBox示例教程

    jsf入门实例 JSF allows users to select multiple values for a single field with the help of <h:selectM ...

  2. servlet 异常处理_Servlet异常和错误处理示例教程

    servlet 异常处理 有时候我写了一篇有关Java异常处理的文章,但是当涉及到Web应用程序时,我们需要的不仅仅是Java中的异常处理. Servlet异常 如果您注意到,doGet()和doPo ...

  3. Servlet异常和错误处理示例教程

    有时我写了一篇有关Java异常处理的文章,但是当涉及到Web应用程序时,我们需要的不仅仅是Java中的异常处理. Servlet异常 如果您注意到,doGet()和doPost()方法将抛出Servl ...

  4. jsf集成spring_JSF Spring Hibernate集成示例教程

    jsf集成spring Welcome to JSF Spring Hibernate Integration example tutorial. In our last tutorial, we s ...

  5. jsf标签_JSF Facelet标签示例教程

    jsf标签 JSF provides a special set of tags that gives the flexibility to manage common tags/parts in o ...

  6. jsf 导航_JSF页面导航示例教程

    jsf 导航 Page navigation is the redirection of a page based on the events performed for instance – on ...

  7. jsf 导航_JSF导航规则示例教程

    jsf 导航 JSF Navigation rules specifies the navigation between the pages on click of button or hyperli ...

  8. java ldap操作实例_Java Spring Security示例教程中的2种设置LDAP Active Directory身份验证的方法...

    java ldap操作实例 LDAP身份验证是世界上最流行的企业应用程序身份验证机制之一,而Active Directory (Microsoft为Windows提供的LDAP实现)是另一种广泛使用的 ...

  9. servlet过滤器 实例_Java Servlet过滤器示例教程

    servlet过滤器 实例 Java Servlet Filter is used to intercept the client request and do some pre-processing ...

最新文章

  1. 20145221 《信息安全系统设计基础》第3周学习总结
  2. 一文读懂全系列树莓派!
  3. HTML第十章作业代码,HTML教程10第十章.doc
  4. C++多态的基本概念
  5. timthumb.php外链,如何解决WordPress多站点不支持timthumb.php?
  6. 给你的shell终端添上一道靓丽的风景
  7. mysql性能优化方案总结
  8. 林祖宁《ISO20000-12011 认证合格判定基础》
  9. adb启动app_ADB 命令大全
  10. 根据IP获取国家代码
  11. make menuconfig快速查找
  12. 雅加达出差(8月24日到25日)
  13. linux 光标切换快捷键,光标操作快捷键,光标快捷键
  14. MTK平台如何切换SIM卡槽
  15. mysql:innodb存储引擎之表结构
  16. dd指令打包iso文件 linux_Linux_如何在Linux操作系统下创建ISO镜像文件,1、用dd命令#dd if=/dev/cdrom - phpStudy...
  17. Sporadic IOException: Failed to persist config
  18. [012量化交易] python 最高价 最低价
  19. 2014 Multi-University Training Contest 5——by Xiaoxu Guo (ftiasch)
  20. 一个浙大教授让人发冷汗的讲演

热门文章

  1. grep配置颜色显示
  2. React Native – 使用 JavaScript 开发原生应用
  3. activereport
  4. [转载] Python: ord()函数
  5. [转载] 详细介绍Python函数中的默认参数
  6. [转载] python将int转为string_python – 在Pandas中将列名从int转换为string
  7. [转载] python中join的使用
  8. [转载] python 语言基础 - 字符串常用函数及操作
  9. [转载] 【Python进阶】4-2 多态 | 什么是多态 / 静态语言vs动态语言 / python中多态
  10. 清空visual studio 开发缓存