This blog post intends to answer the following questions :-
The signature of the validate method is a follows :-
public void validate()
{
//your validation code goes here.
}
Happy Programming ;)
Signing Off
Ryan
- How to perform simple validation in Struts 2?
- How to make use of the ValidationAware interface in Struts 2?
- What can the validate method be used for in Struts 2?
Validaton in Struts 2 within your action class can be performed by using the validate method. If your Struts class is a POJO(Plain Old java Object), then you need to implement the Validation aware interface and implement the validate() method so that the Struts 2 framework is able to call an interceptor for your POJO to perform the validation. If your class already extends the ActionSupport class, then you do not need to implement the ValidationAware interface because the ActionSupport class already provides a default implementation of the validate() method that does nothing.
The signature of the validate method is a follows :-
public void validate()
{
//your validation code goes here.
}
Now, the validate method can be used for any kind of validation, such as checking whether the user has not entered any null values into any form field, or if the form field has valid values. But, in my personal opinion, all such minor checks should be performed in the client itself using javascript. Because performing client side validation certainly avoids a round-trip to the server.
However the validate method can come in handy at certain times, when client side validation just cannot get the task done. Suppose that your jsp page has a user registration form. And you need to check that the user does not use a username that already exists in the system. This kind of validation is not possible on the client side alone. here you can use your validate() method to access the required database and then send a response back to the client asking him/her to enter a different username from the one that has just been entered.
Performing validation on the server can be a very frustrating to the client since he/she has to wait a long time until the server informs him of his mistakes. So, i suggest that you keep your server side validation as minimal as possible, and user javascript at the client side to perform simple validations such as those for empty fields or field lengths.
Happy Programming ;)
Signing Off
Ryan