0% found this document useful (0 votes)
2 views

Struts_Validate_Method_Notes

The `validate()` method in Struts is used for validating form data submitted by users before the execution of the Action class. It checks for errors, such as required fields and valid formats, and returns any found errors to be displayed on the form. This method promotes centralized validation logic and integrates well with internationalized error messages.

Uploaded by

arshiya91456
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Struts_Validate_Method_Notes

The `validate()` method in Struts is used for validating form data submitted by users before the execution of the Action class. It checks for errors, such as required fields and valid formats, and returns any found errors to be displayed on the form. This method promotes centralized validation logic and integrates well with internationalized error messages.

Uploaded by

arshiya91456
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

What is the `validate` Method in Struts and How to Use It?

-----------------------------------------------------------

In the Struts framework, the `validate()` method is used to check the validity of form data submitted

by the user.

It is defined inside a class that extends `ActionForm`. When a user submits the form, Struts

automatically calls

this method to perform form validation before the `execute()` method in the Action class runs.

If the `validate()` method finds any errors, the form is re-displayed to the user with appropriate error

messages.

Example: Using `validate()` Method

----------------------------------

Let's say we have a `UserForm` with two fields: `name` and `email`. We want to validate that:

- `name` is required (not empty)

- `email` must follow a valid format

Here's how we write the `validate()` method:

```java

import org.apache.struts.action.ActionErrors;

import org.apache.struts.action.ActionError;

import org.apache.struts.action.ActionForm;

import org.apache.struts.action.ActionMapping;
import javax.servlet.http.HttpServletRequest;

public class UserForm extends ActionForm {

private String name;

private String email;

// Getters and Setters

public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {

ActionErrors errors = new ActionErrors();

// Check if name is empty

if (name == null || name.trim().equals("")) {

errors.add("name", new ActionError("error.name.required"));

// Check if email is in valid format

if (email == null || !email.matches("\\w+@\\w+\\.\\w+")) {

errors.add("email", new ActionError("error.email.invalid"));

return errors;

```

How It Works:
-------------

1. If `name` is empty or null, it adds a validation error using `ActionError`.

2. If `email` doesn't match a basic regex pattern, another error is added.

3. If `errors` is not empty, the form is shown again with error messages on the JSP page.

Why Use `validate()`?

----------------------

- Centralized validation logic (clean and maintainable)

- Automatic call by Struts before the Action class executes

- Works well with internationalized error messages using `ApplicationResources.properties`

Resources:

----------

- https://websparrow.org/struts/struts2-validation-example-using-validate-method-and-xml-file

- https://www.tutorialspoint.com/struts_2/struts_validations.htm

You might also like