📄 customvalidation.jsp
字号:
<%-- tpl:insert page="/template/jsfTemplate.jtpl" --%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%@taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<%@taglib uri="http://www.ibm.com/jsf/html_extended" prefix="hx"%>
<HTML>
<HEAD>
<META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<META name="GENERATOR" content="IBM Software Development Platform">
<META http-equiv="Content-Style-Type" content="text/css">
<LINK href="../theme/Master.css" rel="stylesheet" type="text/css">
<LINK href="../theme/new.css" rel="stylesheet">
<LINK rel="stylesheet" type="text/css" href="../theme/stylesheet.css" title="Style">
<%-- tpl:put name="headarea" --%>
<title>customValidation.jsp</title>
<f:loadBundle basename="com.ibm.samples.validation.messages" var="msgs" />
<%-- /tpl:put --%>
<LINK rel="stylesheet" type="text/css" href="/JSFandSDO/theme/stylesheet.css" title="Style">
</HEAD>
<f:view>
<BODY>
<TABLE class="title" cellpadding="0">
<TBODY>
<TR>
<TD class="noBorder" background="../images/gradient.jpg"
align="CENTER"><IMG border="0" src="../images/title.jpg"></TD>
</TR>
<TR>
<TD class="noBorder" align="RIGHT"><A href="/JSFandSDO">Return to Main Menu</A></TD>
</TR>
</TBODY>
</TABLE>
<%-- tpl:put name="bodyarea" --%>
<hx:scriptCollector id="scriptCollector1">
<h:form styleClass="form" id="form1">
<H2>Validating Input</H2>
This page demonstrates the use of validators to check user input before model values are updated. If any field in a form does not properly validate, the page is redisplayed with an error message and the integrity of back end system is preserved. JavaServer Faces comes with a set of standard validators and you can create your own custom validators to suit the specific needs of your application.<BR>
<BR>
<TABLE border="1">
<TBODY>
<TR>
<TD width="50%" valign="top" colspan="2" align="center"><h:outputText styleClass="outputText" id="text4" value="Standard Validators: " style="font-weight: bold"></h:outputText><BR>
The JSF standard validators can validate the length of a
String, the range of a number, and if a value is required. To set up a standard validator, select the input component you want to valid and open its Properties View. Click Validation to see the validation options.<BR></TD>
</TR>
<TR>
<TD class="tdColor25"><h:outputText styleClass="outputText" id="text5" value="Enter a String 5 or more characters in length:"></h:outputText><BR>
<h:inputText styleClass="inputText" id="lengthInput" required="true" value="#{sessionScope.string}">
<f:validateLength minimum="5"></f:validateLength>
<hx:inputHelperAssist errorClass="inputText_Error" />
</h:inputText> <h:message for="lengthInput" styleClass="errorMessage"></h:message><BR>
Current Value: <h:outputText styleClass="outputText" id="text3" value="#{sessionScope.string}"></h:outputText></TD>
<TD class="td75">(Required Field) Uses a length validator which is set
to require a string to be at least 5 characters in length.<BLOCKQUOTE><P class="code">Example:<BR><h:inputText styleClass="inputText" id="lengthInput" required="true" value="#{sessionScope.string}"><BR>
<f:validateLength minimum="5"></f:validateLength><BR>
<hx:inputHelperAssist errorClass="inputText_Error" /><BR>
</h:inputText></P></BLOCKQUOTE>
</TD>
</TR>
<TR>
<TD class="tdColor25"><h:outputText styleClass="outputText" id="text6" value="Enter a number between 1 and 20:"></h:outputText><BR>
<h:inputText styleClass="inputText" id="rangeInput" value="#{sessionScope.number}">
<f:convertNumber />
<f:validateDoubleRange minimum="1.0" maximum="20.0"></f:validateDoubleRange>
</h:inputText> <h:message for="rangeInput" styleClass="errorMessage"></h:message><BR>
Current Value: <h:outputText styleClass="outputText" id="text9" value="#{sessionScope.number}"></h:outputText></TD>
<TD class="td75">(Not a Required Field) Uses a range validator which is set to require a
number to be between 1 and 20.<BLOCKQUOTE><P class="code">Example:<BR><h:inputText styleClass="inputText" id="rangeInput" value="#{sessionScope.number}"><BR>
<f:convertNumber /><BR>
<f:validateDoubleRange minimum="1.0" maximum="20.0"></f:validateDoubleRange><BR>
</h:inputText></P></BLOCKQUOTE>
</TD>
</TR>
<TR>
<TD valign="top" colspan="2" align="center"><h:outputText styleClass="outputText" id="text2" value="Custom Validation: " style="font-weight: bold"></h:outputText><BR>
You can create your own custom validators by creating a class that
implements the javax.faces.validator.Validator interface or by including a
validate method in the page code file for this JSP.<BR>
<BR>
</TD>
</TR>
<TR>
<TD class="tdColor25"><h:outputText styleClass="outputText"
id="text1" value="Enter an Even Number Between 2 and 100,000:"></h:outputText><BR>
<h:inputText styleClass="inputText" id="evenInput" value="#{sessionScope.even}" required="true">
<f:validator validatorId="CustomValidator" />
</h:inputText><h:message for="evenInput" styleClass="errorMessage"></h:message><BR>
Current Value: <h:outputText styleClass="outputText" id="text10" value="#{sessionScope.even}"></h:outputText></TD>
<TD class="td75">(Required Field) Uses a custom validator class which requires the
number to be even and between 2 and 100,000.<BLOCKQUOTE><P class="code">Example:<BR><h:inputText styleClass="inputText" id="evenInput" value="#{sessionScope.even}" required="true"><BR>
<f:validator validatorId="CustomValidator" /><BR>
</h:inputText></P></BLOCKQUOTE>
</TD>
</TR>
<TR>
<TD class="tdColor25"><h:outputText styleClass="outputText" id="text19" value="Enter a Starting Date (MM/dd/yyyy):"></h:outputText><BR>
<h:inputText styleClass="inputText" id="start" required="true" value="#{sessionScope.start}">
<f:convertDateTime pattern="MM/dd/yyyy" />
<hx:inputHelperDatePicker />
</h:inputText> <h:message for="start" styleClass="errorMessage"></h:message><BR>
Current Value: <h:outputText styleClass="outputText" id="text11" value="#{sessionScope.start}">
<f:convertDateTime pattern="MM/dd/yyyy" />
</h:outputText></TD>
<TD class="td75">(Required Field) A custom validator attached to the next Input Box will use this date as part of its multi-field validation.<BR>
</TD>
</TR>
<TR>
<TD class="tdColor25"><h:outputText styleClass="outputText" id="text20" value="Enter an Ending Date (MM/dd/yyyy):"></h:outputText><BR>
<h:inputText styleClass="inputText" id="end" validator="#{pc_CustomValidation.validateDate}" required="true" value="#{sessionScope.end}">
<f:convertDateTime pattern="MM/dd/yyyy" />
<hx:inputHelperDatePicker />
</h:inputText> <h:message for="end" styleClass="errorMessage"></h:message><BR>
Current Value: <h:outputText styleClass="outputText" id="text12" value="#{sessionScope.end}">
<f:convertDateTime pattern="MM/dd/yyyy" />
</h:outputText></TD>
<TD class="td75">(Required Field) Uses a custom validator method in the page code
file to check that the date is this box comes after the date in the
Starting box. - Required Field.<BLOCKQUOTE><P class="code">Example:<BR><h:inputText styleClass="inputText" id="end" validator="#{pc_CustomValidation.validateDate}" required="true" value="#{sessionScope.end}"><BR>
<f:convertDateTime pattern="MM/dd/yyyy" /><BR>
<hx:inputHelperDatePicker /><BR>
</h:inputText></P></BLOCKQUOTE>
</TD>
</TR>
<TR>
<TD class="tdColor25"><hx:commandExButton type="submit" value="Submit" styleClass="commandExButton2" id="button1"></hx:commandExButton></TD>
<TD class="td75"><BR>
</TD>
</TR>
</TBODY>
</TABLE>
<H4>How It works:</H4>
To add standard validators to your page:<BR>1. Open the Properties view, then select the input field you want to validate.<BR>2. Click Validation. There you will be able to select the types of validation to use.<BR>3. Optionally, click "Display validation error message in an error message control" if you want to display any validation errors that occur.<BR>
<BR>
If you create a custom validation class, you must register it in the faces-config.xml file and nest the Validator tag inside the input tag you wish to validate. If you write a validation method then you need to include the validate attribute with the input tag and bind its value to your validation method.<BLOCKQUOTE>faces-config.xml Example:<BR>
<validator><BR> <validator-id>CustomValidator</validator-id><BR> <validator-class>com.ibm.samples.validation.CustomValidator</validator-class> <BR> </validator></BLOCKQUOTE>To overwrite the error messages for the standard validators, you need to create a message bundle for this application and supply it with the messages you would like displayed:<BR>
<BR>1. Create a .properties file.<BR>2. Overwrite the messages you want to change.<BR>3. Define the file in your faces-config.xml file - <message-bundle>com.ibm.samples.validation.messages</message-bundle>.<BR>4. Add the message bundle to the JSP - <f:loadBundle basename="com.ibm.samples.validation.messages" var="msgs" />.<H4>Files
of Interest:</H4>
<UL>
<LI><B>WebContent/validation/custonValidation.jsp</B> - (This Page) Where the JSF tags to perform the validation are located.</LI>
<LI><B>src/com.ibm.samples.validation.CustomValidator.java</B> - The custom validation class.</LI>
<LI><B>src/pagecode.validation.CustomValidation.java</B> - Where the custom validation method is located.</LI>
<LI><B>src/com.ibm.samples.validation.messages.properties</B> - The properties file to hold validation error messages.</LI>
</UL>
</h:form>
</hx:scriptCollector>
<%-- /tpl:put --%>
<TABLE class="title" cellpadding="0">
<TBODY>
<TR>
<TD class="noBorder" align="RIGHT"><A href="/JSFandSDO">Return to Main Menu</A></TD>
</TR>
</TBODY>
</TABLE>
</BODY>
</f:view>
</HTML><%-- /tpl:insert --%>
<%-- jsf:pagecode language="java" location="/src/pagecode/validation/CustomValidation.java" --%><%-- /jsf:pagecode --%>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -