Java Reference
In-Depth Information
Joe Asks. . .
Where Should I Put the <s:errors/> Tag?
Placing <s:errors/> within the <s:form> tag displays the error
messages associated with that form. When you have more than
one form in a single page, you can display the errors for each
form or place the <s:errors/> outside the <s:form> tag to dis-
play the error messages that occurred in the current action
bean.
I mentioned that in Stripes validations can be implemented as type con-
verters. To use a type converter, you indicate its class in the
converter=
attribute of
@Validate
. The
EmailTypeConverter
validates that the input is
of email address format, so we can use it with
converter=
to validate the
contact email:
@ValidateNestedProperties({
@Validate(field="email", required=
true
, on="save",
converter=EmailTypeConverter.
class
)
})
@Override
public void
setContact(Contact contact) {
super
.setContact(contact);
}
The
EmailTypeConverter
uses JavaMail to validate the email address, so
we'll have to add the library to the
WEB-INF/lib
directory. Unless you
are using Java 6, you will also have to add the JavaBeans Activation
Framework:
WEB-INF/lib/javamail.jar
WEB-INF/lib/activation.jar
Now, entering an invalid email address such as “hello” displays this
error message: “The value (hello) entered is not a valid email address.”
Limiting the Length of Input
Let's add validation rules for the first and last name fields. These fields
are optional, but if a value is entered, we'll enforce these restrictions:
• The first name cannot exceed twenty-five characters.
• The last name cannot exceed forty characters.
• The last name must be at least two characters.
Search WWH ::
Custom Search