But the clean way of doing server side validation in struts has amazed me.
It has such straightforward and clean implementation(with very hard to find documentation). So I thought I should write a small example.
There are a few simple things to do and you are all set
Your form bean should extend ValidatorActionForm.
The form should implement the validate method
public ActionErrors validate(){}
for anything that you want to return a validation error
your code can look like
if(myName==null)
errors.add("myName", "My Name cannot be null");
return errors
On your jsp you should have a tag to display these errors
<html:error property=\"myName\">
wherever you want to display this particular error message.
If you want to display the error message at the top of the page or display all the error messages regardless of their location
alternatively you can use the applicationResources.properties file to parse the error messages for you To do that add the following in your applicationResources.properties
error.myName = <img src="error.jpg" /><b>MyName is required</b>
and when you add the error do the following
errors.add("myName", "error.myName");
Custom and generic error messages can also be created and displayed.
Struts is so simple.. Boo to all those who hate struts...:P
Reading Now:
The short history of nearly everything - Bill Bryson
The world is flat - Thomas L Friedman
Angels and Daemons - Dan Brown
Recently learnt :
Streamlined for Rails from Jake Scruggs at TW (http://streamlined.relevancellc.com/) - Crud is Simple
No comments:
Post a Comment