Apache Struts 2 Documentation > Home > Guides > Core Developers Guide > Validation > Using Field Validators |
The followings show a simple example using Struts's Field Validators
Create the jsp page
<h3>All Field Errors Will Appear Here</h3> <s:fielderror /> <hr/> <h3>Field Error due to 'Required String Validator Field' Will Appear Here</h3> <s:fielderror> <s:param value="%{'requiredStringValidatorField'}" /> </s:fielderror> <hr/> <h3>Field Error due to 'String Length Validator Field' Will Appear Here</h3> <s:fielderror> <s:param>stringLengthValidatorField</s:param> </s:fielderror> <hr/> <s:form action="submitFieldValidatorsExamples" namespace="/validation" method="POST" theme="xhtml"> <s:textfield label="Required Validator Field" name="requiredValidatorField" /> <s:textfield label="Required String Validator Field" name="requiredStringValidatorField" /> <s:textfield label="Integer Validator Field" name="integerValidatorField" /> <s:textfield label="Date Validator Field" name="dateValidatorField" /> <s:textfield label="Email Validator Field" name="emailValidatorField" /> <s:textfield label="URL Validator Field" name="urlValidatorField" /> <s:textfield label="String Length Validator Field" name="stringLengthValidatorField" /> <s:textfield label="Regex Validator Field" name="regexValidatorField"/> <s:textfield label="Field Expression Validator Field" name="fieldExpressionValidatorField" /> <s:submit label="Submit" /> </s:form>
Create the actoin class
public class FieldValidatorsExampleAction extends AbstractValidationActionSupport { private static final long serialVersionUID = -4829381083003175423L; private String requiredValidatorField = null; private String requiredStringValidatorField = null; private Integer integerValidatorField = null; private Date dateValidatorField = null; private String emailValidatorField = null; private String urlValidatorField = null; private String stringLengthValidatorField = null; private String regexValidatorField = null; private String fieldExpressionValidatorField = null; public Date getDateValidatorField() { return dateValidatorField; } public void setDateValidatorField(Date dateValidatorField) { this.dateValidatorField = dateValidatorField; } public String getEmailValidatorField() { return emailValidatorField; } public void setEmailValidatorField(String emailValidatorField) { this.emailValidatorField = emailValidatorField; } public Integer getIntegerValidatorField() { return integerValidatorField; } public void setIntegerValidatorField(Integer integerValidatorField) { this.integerValidatorField = integerValidatorField; } public String getRegexValidatorField() { return regexValidatorField; } public void setRegexValidatorField(String regexValidatorField) { this.regexValidatorField = regexValidatorField; } public String getRequiredStringValidatorField() { return requiredStringValidatorField; } public void setRequiredStringValidatorField(String requiredStringValidatorField) { this.requiredStringValidatorField = requiredStringValidatorField; } public String getRequiredValidatorField() { return requiredValidatorField; } public void setRequiredValidatorField(String requiredValidatorField) { this.requiredValidatorField = requiredValidatorField; } public String getStringLengthValidatorField() { return stringLengthValidatorField; } public void setStringLengthValidatorField(String stringLengthValidatorField) { this.stringLengthValidatorField = stringLengthValidatorField; } public String getFieldExpressionValidatorField() { return fieldExpressionValidatorField; } public void setFieldExpressionValidatorField( String fieldExpressionValidatorField) { this.fieldExpressionValidatorField = fieldExpressionValidatorField; } public String getUrlValidatorField() { return urlValidatorField; } public void setUrlValidatorField(String urlValidatorField) { this.urlValidatorField = urlValidatorField; } }
Create the validator.xml.
An error occurred: http://svn.apache.org/repos/asf/struts/struts2/trunk/apps/showcase/src/main/java/org/apache/struts2/showcase/validation/FieldValidatorsExampleAction-submitFieldValidatorsExamples-validation.xml. The system administrator has been notified.