Validation Introduction
JavaServer Faces 1.x does not explicitly support client-side validation and only provides a minimal set of server-side validators. On the other hand, Apache Commons Validator supports both client- and server-side validators and comes with the following useful validators:
- Credit Card
- Date
- Email
- Generic
- ISBN
- URL
All of the preceding validators can be executed either on the client or the server, or both. Shale integrates JavaServer Faces and the Commons Validator.
For example:
Server Validate Demo:
- <h:inputText id="amount"
- value="#{validate$test.amount}"
- size="7">
-
- <f:convertNumber
- minFractionDigits="2"/>
-
- <val:commonsValidator
- type="floatRange"
- min="10"
- max="1000"
- arg="#{messages['validate.test.amount']}"
- server="true"
- client="false"/>
-
- </h:inputText>
Client side Validate demo:
- <h:inputText id="date"
- value="#{validate$test.expirationDate}">
-
- <f:convertDateTime
- pattern="MM/dd/yyyy"/>
-
- <val:commonsValidator
- type="required"
- arg="#{messages['prompt.expirationDate']}"
- server="false"
- client="true"/>
-
- <val:commonsValidator
- type="date"
- datePatternStrict="MM/dd/yyyy"
- message="#{messages['validate.test.bad.expiration.date']}"
- arg="#{messages['prompt.expirationDate']}"
- server="false"
- client="true"/>
-
- </h:inputText>
Services Provided
Shale provides three JSP tags that let you use the Commons Validator: val:commonsValidator
, val:validatorVar
, and val:validatorScript
. The first two lets you attach a commons validator to a JSF input component and the third generates JavaScript validation code for validating each JSF component that has one or more Commons validators in a particular form. You can attach as many Commons validators to a single JSF input component as you wish.
Using Commons Validator Integration
Here's what you need to do to use Shale validation:
- Add an
onsubmit
attribute to your h:form
tag that calls the JavaScript validation function generated by val:validatorScript
.
- Add Commons validators to JSF input components with
val:commonsValidator
and, optionally, val:validatorVar
.
- Add an
val:validatorScript
tag at the end of the h:form
tag's body.
Here's an example:
- <%@ taglib uri="http://shale.apache.org/core" prefix="val" %>
- ...
- <h:form onsubmit="return validateForm(this);">
-
- <h:inputText id="creditCardNumber"
- size="16"
- value="#{userContext.creditCardNumber}">
-
- <val:commonsValidator type="required"
- arg="#{msgs.creditCardNumberPrompt}"
- server="true"
- client="true"/>
-
- <val:commonsValidator type="mask"
- arg="#{msgs.creditCardNumberPrompt}"
- server="true"
- client="true">
- <val:validatorVar name="mask" value="[4-6].*"/>
- </val:commonsValidator>
-
- <val:commonsValidator type="creditCard"
- arg="#{msgs.creditCardNumberPrompt}"
- server="true"/>
- </h:inputText>
-
- <h:message for="creditCardNumber" styleClass="errors"/>
-
- <val:validatorScript functionName="validateForm"/>
- </h:form>
In the preceding example, we've attached three Commons validators to a single JSF input component. To pass validation, the field must have a value that starts with a number between 4 and 6 inclusive and that value must be a valid credit card number as verified by the Luhn algorithm. Two of the validations are performed on both client and server and one is performed on the server only.
Note: At the present time, you have the option to forego server-side validation, which is considered very bad practice. Users can turn off JavaScript, so you should always backup client-side validation with server-side validation. In the future, Shale may enforce server-side validation if it's not explicitly specified.
分享到:
相关推荐
在JSF中,默认并不包含客户端验证机制,但Shale通过集成Apache Commons Validator库,为开发者提供了便捷的客户端验证解决方案。这使得开发者能够在不增加太多复杂性的情况下,实现高效且用户友好的表单验证。 在...
这些源码对于开发者来说是一份宝贵的资源,可以帮助他们理解Shale内部的工作机制,包括请求处理、视图渲染、模型绑定等。通过阅读和分析源码,开发者可以学习到如何自定义Shale的行为,以及如何与其他框架集成。 4....
shale-remoting 1.0.4
首先,我们需要理解JSF中的验证机制。JSF是一种用于构建Web用户界面的MVC(模型-视图-控制器)框架,它内置了数据验证支持。验证通常发生在两个阶段:客户端验证和服务器端验证。客户端验证在用户提交表单前进行,...
开发JSF或Shale开发用的包,自己准备研究这两个东西却发现需要很多包一下子又找不全,总算找了差不多了,拿出来与大家分享.其中包含:shale-core.jar commons-beanutils.jar commons-chain.jar commons-codec....
MRST-Shale:一个开源的页岩气模拟器 王斌( ) 美国路易斯安那州立大学Craft.io与霍金斯大学石油工程系 OpenShale是一个轻量级的开源库,它基于SINTEF ICT( )。 它要求通过运行“ startup.m”将MRST添加到...
**ETL工具 beeload (shale)免费版 详细使用教程** **1. ETL工具简介** ETL,即Extract, Transform, Load,是数据处理领域中的关键环节,用于从不同源系统抽取数据,经过清洗、转换,再加载到目标系统。beeload是一...
jar包,官方版本,自测可用
jar包,官方版本,自测可用
页岩 一种优雅且可扩展的反向抛光符号编程语言。 因其强大和简单而优雅。 通过库可扩展。 当前这些包括但不限于 创建线程,信号量和互斥量的线程库 ...有关详细信息,请参见shale -h和shale -s,以及examples /目录。
674 Interbedded shale and limestone (shale dominant)(2st option) 页岩与石灰岩互层(页岩为主)(2) 675 Interbedded calcareous shale and limestone (shale dominant) 钙质页岩与石灰岩互层(页岩为主) ...
Pyrolysis characteristics of a North Korean oil shale and its pyrolysates were investigated in this paper. The pyrolysis experiments were conducted below 600 °C at a heating rate of 10, 15, 20 and 25...
页岩(Shale)是基于Amber框架的一个强大的分页器组件,专为Crystal语言的Web开发设计。Amber是一个全面的、现代化的Web开发框架,它借鉴了Ruby on Rails的许多优秀特性,并针对Crystal语言进行了优化。在Amber框架中...
### 中国东南部四川盆地寒武纪龙马溪页岩的岩石相与沉积特征 #### 概述 本文探讨了中国东南部四川盆地寒武纪龙马溪页岩(SLS)的岩石相与沉积特征,这对于该地区的页岩气勘探与开发具有重要意义。...
Oil shale is an important potential energy source, consisting of an inorganic mineral matrix containing organic matter. The organic matter is generally divided into two fractions: bitumen and kerogen....
这个【描述】提及了书中第十二章涉及Seam和Shale的部分,这两个都是在早期JSF生态系统中重要的辅助框架。 Seam是一个开源的、集成的JavaEE开发框架,它将JSF与EJB、CDI(Contexts and Dependency Injection)、...
尽管对南襄盆地泌阳凹陷古近系核桃园组湖相页岩采用大型压裂初产已获高产油气流,但是对该湖相页岩储集层特征尚未进行系统研究。文中应用薄片分析及全岩X衍射分析,结合陆相页岩矿物组成特点,将泌阳凹陷湖相页岩划分为...
标题中的“dual-porosity MRST模拟”指的是使用多相流模拟工具(Multiphase Reservoir Simulator Toolkit,简称MRST)对具有双重介质特性的油藏进行的数值模拟。这种油藏通常由两种不同渗透率的岩石组成,如基质和...