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
- Generic
- ISBN
- URL
For example:
- <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
onsubmitattribute to yourh:formtag that calls the JavaScript validation function generated byval:validatorScript. - Add Commons validators to JSF input components with
val:commonsValidatorand, optionally,val:validatorVar. - Add an
val:validatorScripttag at the end of theh:formtag'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.
本文介绍如何在JavaServer Faces (JSF)中使用Apache Shale集成Commons Validator进行客户端和服务端验证。通过示例展示了如何为JSF输入组件添加多种验证器,并确保表单提交前的数据准确性。
1667

被折叠的 条评论
为什么被折叠?



