Home >> Struts
Date validation can be performed using validator
framework while using Apache Struts 1.x version.
I have successfully executed following example
with a very simple date validation for required
and date pattern and date pattern strict.
One of the difference between datePattern and
datePatternStrict is with datePattern only the
pattern is matched and not the total length and
whether a valid date or not a date is validated.
While datePatternStrict is used to check
for valid date and total length of the field value
as well.
|
|  |
|
This example is having a very simple date field
and the corresponding validation XML file is
as follows:
---------------------------------------------------------
<form-validation>
<global>
</global>
<formset>
<form name="exampleForm">
<field property="name" depends="required,mask">
<arg0 key="exampleForm.name.text"/>
<var>
<var-name>mask</var-name>
<var-value>^[a-zA-Z]+$</var-value>
</var>
</field>
<field property="dob" depends="required,date">
<arg0 key="exampleForm.dob.text"/>
<var>
<var-name>datePatternStrict</var-name>
<var-value>mm - dd - yyyy</var-value>
</var>
</field>
</form>
</formset>
</form-validation>
---------------------------------------------------------
so the actionform is defined as exampleForm in the
struts-config.xml file:
---------------------------------------------------------
<form-bean name="exampleForm"
type="action.ExampleForm"/>
---------------------------------------------------------
index.jsp file is as follows:
---------------------------------------------------------
<%@ page language="java" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<html:html locale="true">
<head>
<title><bean:message key="index.title.label"/></title>
<html:base/>
</head>
<body bgcolor="white">
<logic:messagesPresent>
<html:messages id="errors">
<%= errors %>
</html:messages>
</logic:messagesPresent>
<table>
<html:form method="post" action="example">
<tr><td><bean:message key="index.name.label"/>
</td><td><html:text property="name"/></td>
</tr>
<tr><td><bean:message key="index.dob.label"/></td>
<td><html:text property="dob" /></td>
</tr>
<tr><td><html:submit property="butSubmit"
value="Submit"/></td>
<td><html:button property="butReset"
value="Reset"/></td>
</tr>
</html:form>
</table>
</body>
</html:html>
---------------------------------------------------------
Following the above JSP file, the action mapping in
struts-config XML file is as follows:
---------------------------------------------------------
<action-mappings>
<action path="/example"
type="action.ExampleAction"
name="exampleForm"
scope="request" validate="true" input="/index.jsp">
<forward name="success" path="/preview.jsp"/>
</action>
</action-mappings>
---------------------------------------------------------
and the next file is a preview page, which can be a very
simple page witha println statement only, just to know
that this page is called after successful validation happening.
Hope this helps.
If you have any further queries and need to discuss
please write to me here in this page.
If anything missed out , please let me know at
techienjoy at yahoo . com
References :
Tags: Struts 1 Struts2 plugin
Tags: struts jsf integrate
Tags: struts result chain
Tags: struts tiles i18n
Tags: Struts validation struggle
Tags: Struts
Tags: struts1 date validation
Tags: struts1 struts2 validation
Tags: struts2 dojo 1
Tags: struts2 dojo 2
Tags: struts2 dojo 3
Tags: struts2 dojo 4
Tags: Struts2 example steps Tag usage
Tags: Struts2 example steps
Tags: struts2 own validator
Tags: struts2 pdf result
Tags: struts2 radio tag example
Tags: struts2 result plaintext
Tags: struts2 tags example
Tags: struts2 tiles i18n
Tags: struts2 upload example
Tags: struts2 validation 1
Tags: struts2 validation expression
Tags: struts2 validation
Tags: struts2 xslt article
DISCLAIMER :
The content provided in this page is not warranted and/or guaranteed by techienjoy.com.
techienjoy.com is not liable for any negative consequences that may result/arise from
implementing directly/indirectly any information covered in these pages/articles/tutorials.
All contents of this site is/are written and provided on an "AS IS" basis,
without WARRANTIES or conditions of any kind, either express or implied, including, without
limitation, merchantability, or fitness for a particular purpose. You are solely responsible
for determining the appropriateness of using or refering this and assume any risks associated
with this.
In spite of all precautions taken to avoid any typo in these pages, there might be some
issues like grammatical mistakes and typos being observed in these pages, techienjoy.com
extends sincerest apologies to all our visitors for the same.
|