`

Shale Validate 机制初探

阅读更多

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:
  1. <h:inputText     id="amount"  
  2.                    value="#{validate$test.amount}"  
  3.                     size="7">  
  4.   
  5.         <f:convertNumber  
  6.        minFractionDigits="2"/>  
  7.   
  8.         <val:commonsValidator  
  9.                     type="floatRange"  
  10.                      min="10"  
  11.                      max="1000"  
  12.                      arg="#{messages['validate.test.amount']}"  
  13.                   server="true"  
  14.                   client="false"/>  
  15.   
  16.      </h:inputText>  

Client side Validate demo:

  1. <h:inputText     id="date"  
  2.                      value="#{validate$test.expirationDate}">  
  3.   
  4.          <f:convertDateTime  
  5.                     pattern="MM/dd/yyyy"/>  
  6.   
  7.            <val:commonsValidator  
  8.                        type="required"  
  9.                        arg="#{messages['prompt.expirationDate']}"  
  10.                     server="false"  
  11.                     client="true"/>  
  12.   
  13.          <val:commonsValidator  
  14.                       type="date"  
  15.          datePatternStrict="MM/dd/yyyy"  
  16.                    message="#{messages['validate.test.bad.expiration.date']}"  
  17.                        arg="#{messages['prompt.expirationDate']}"  
  18.                     server="false"  
  19.                     client="true"/>  
  20.   
  21.       </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:

  1. <%@ taglib uri="http://shale.apache.org/core" prefix="val" %>  
  2. ...   
  3. <h:form onsubmit="return validateForm(this);">  
  4.   
  5.     <h:inputText id="creditCardNumber"    
  6.                size="16"  
  7.               value="#{userContext.creditCardNumber}">  
  8.   
  9.         <val:commonsValidator type="required"  
  10.                              arg="#{msgs.creditCardNumberPrompt}"  
  11.                           server="true"  
  12.                           client="true"/>  
  13.   
  14.         <val:commonsValidator type="mask"  
  15.                              arg="#{msgs.creditCardNumberPrompt}"  
  16.                           server="true"  
  17.                           client="true">  
  18.             <val:validatorVar name="mask" value="[4-6].*"/>  
  19.         </val:commonsValidator>  
  20.   
  21.         <val:commonsValidator type="creditCard"    
  22.                              arg="#{msgs.creditCardNumberPrompt}"    
  23.                           server="true"/>  
  24.     </h:inputText>    
  25.   
  26.     <h:message for="creditCardNumber" styleClass="errors"/>    
  27.   
  28.     <val:validatorScript functionName="validateForm"/>  
  29. </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.

分享到:
评论

相关推荐

    Shale

    在JSF中,默认并不包含客户端验证机制,但Shale通过集成Apache Commons Validator库,为开发者提供了便捷的客户端验证解决方案。这使得开发者能够在不增加太多复杂性的情况下,实现高效且用户友好的表单验证。 在...

    shale框架源码和文档

    这些源码对于开发者来说是一份宝贵的资源,可以帮助他们理解Shale内部的工作机制,包括请求处理、视图渲染、模型绑定等。通过阅读和分析源码,开发者可以学习到如何自定义Shale的行为,以及如何与其他框架集成。 4....

    shale-remoting jar

    shale-remoting 1.0.4

    shale实现jsf客户端验证例子

    首先,我们需要理解JSF中的验证机制。JSF是一种用于构建Web用户界面的MVC(模型-视图-控制器)框架,它内置了数据验证支持。验证通常发生在两个阶段:客户端验证和服务器端验证。客户端验证在用户提交表单前进行,...

    JSF与Shale开发用包

    开发JSF或Shale开发用的包,自己准备研究这两个东西却发现需要很多包一下子又找不全,总算找了差不多了,拿出来与大家分享.其中包含:shale-core.jar commons-beanutils.jar commons-chain.jar commons-codec....

    MRST_Shale:页岩气模拟环境

    MRST-Shale:一个开源的页岩气模拟器 王斌( ) 美国路易斯安那州立大学Craft.io与霍金斯大学石油工程系 OpenShale是一个轻量级的开源库,它基于SINTEF ICT( )。 它要求通过运行“ startup.m”将MRST添加到...

    ETL工具 beeload (shale)免费版 详细使用教程.pdf

    **ETL工具 beeload (shale)免费版 详细使用教程** **1. ETL工具简介** ETL,即Extract, Transform, Load,是数据处理领域中的关键环节,用于从不同源系统抽取数据,经过清洗、转换,再加载到目标系统。beeload是一...

    com.springsource.org.apache.shale.test-1.0.4.jar

    jar包,官方版本,自测可用

    com.springsource.org.apache.shale.test-sources-1.0.4.jar

    jar包,官方版本,自测可用

    shale:优雅且可扩展的反向抛光表示法语言

    页岩 一种优雅且可扩展的反向抛光符号编程语言。 因其强大和简单而优雅。 通过库可扩展。 当前这些包括但不限于 创建线程,信号量和互斥量的线程库 ...有关详细信息,请参见shale -h和shale -s,以及examples /目录。

    inkscape 地质地层矢量填充符号 patterns.svg

    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 (2014年)

    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...

    页岩:Amber框架的分页器

    页岩(Shale)是基于Amber框架的一个强大的分页器组件,专为Crystal语言的Web开发设计。Amber是一个全面的、现代化的Web开发框架,它借鉴了Ruby on Rails的许多优秀特性,并针对Crystal语言进行了优化。在Amber框架中...

    Lithofacies and sedimentary characteristics of the Silurian Longmaxi Shale in the southeastern Sichuan Basin,China (2013年)

    ### 中国东南部四川盆地寒武纪龙马溪页岩的岩石相与沉积特征 #### 概述 本文探讨了中国东南部四川盆地寒武纪龙马溪页岩(SLS)的岩石相与沉积特征,这对于该地区的页岩气勘探与开发具有重要意义。...

    dual-porosity_mrst模拟_mrst油藏模拟_mrstimbibition_油藏_双重介质_

    标题中的“dual-porosity MRST模拟”指的是使用多相流模拟工具(Multiphase Reservoir Simulator Toolkit,简称MRST)对具有双重介质特性的油藏进行的数值模拟。这种油藏通常由两种不同渗透率的岩石组成,如基质和...

    油页岩干酪根分子模拟

    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....

    JavaServer Faces 核心编程(第二版)源码

    这个【描述】提及了书中第十二章涉及Seam和Shale的部分,这两个都是在早期JSF生态系统中重要的辅助框架。 Seam是一个开源的、集成的JavaEE开发框架,它将JSF与EJB、CDI(Contexts and Dependency Injection)、...

    Reservoir characteristics of lacustrine shale oilof the Paleogene Hetaoyuan Formation in Biyang sag of Nanxiang Basin,Henan Province (2015年)

    尽管对南襄盆地泌阳凹陷古近系核桃园组湖相页岩采用大型压裂初产已获高产油气流,但是对该湖相页岩储集层特征尚未进行系统研究。文中应用薄片分析及全岩X衍射分析,结合陆相页岩矿物组成特点,将泌阳凹陷湖相页岩划分为...

Global site tag (gtag.js) - Google Analytics