⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 customvalidation.jsp

📁 IBM RSA下的JSF开发示例
💻 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>&lt;h:inputText styleClass=&quot;inputText&quot; id=&quot;lengthInput&quot; required=&quot;true&quot; value=&quot;#{sessionScope.string}&quot;&gt;<BR>
						&lt;f:validateLength minimum=&quot;5&quot;&gt;&lt;/f:validateLength&gt;<BR>
						&lt;hx:inputHelperAssist errorClass=&quot;inputText_Error&quot; /&gt;<BR>
						&lt;/h:inputText&gt;</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>&lt;h:inputText styleClass=&quot;inputText&quot; id=&quot;rangeInput&quot; value=&quot;#{sessionScope.number}&quot;&gt;<BR>
						&lt;f:convertNumber /&gt;<BR>
						&lt;f:validateDoubleRange minimum=&quot;1.0&quot; maximum=&quot;20.0&quot;&gt;&lt;/f:validateDoubleRange&gt;<BR>
						&lt;/h:inputText&gt;</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>&lt;h:inputText styleClass=&quot;inputText&quot; id=&quot;evenInput&quot; value=&quot;#{sessionScope.even}&quot; required=&quot;true&quot;&gt;<BR>
						&lt;f:validator validatorId=&quot;CustomValidator&quot; /&gt;<BR>
						&lt;/h:inputText&gt;</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>&lt;h:inputText styleClass=&quot;inputText&quot; id=&quot;end&quot; validator=&quot;#{pc_CustomValidation.validateDate}&quot; required=&quot;true&quot; value=&quot;#{sessionScope.end}&quot;&gt;<BR>
						&lt;f:convertDateTime pattern=&quot;MM/dd/yyyy&quot; /&gt;<BR>
						&lt;hx:inputHelperDatePicker /&gt;<BR>
						&lt;/h:inputText&gt;</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 &quot;Display validation error message in an error message control&quot; 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>
			&lt;validator&gt;<BR>		&lt;validator-id&gt;CustomValidator&lt;/validator-id&gt;<BR>		&lt;validator-class&gt;com.ibm.samples.validation.CustomValidator&lt;/validator-class&gt; <BR>	&lt;/validator&gt;</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 - &lt;message-bundle&gt;com.ibm.samples.validation.messages&lt;/message-bundle&gt;.<BR>4. Add the message bundle to the JSP - &lt;f:loadBundle basename=&quot;com.ibm.samples.validation.messages&quot; var=&quot;msgs&quot; /&gt;.<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 + -