@RequiredStringValidator(message = "Please enter your name.") public void setName(String name) { this.name = name; }In the code snippet above, the setter method is annotated by the @RequiredStringValidator annotation which will ensure that the user doesn’t leave the field empty when submitting the form. The text message “Please enter your name.” will be shown in case validation fails. Some other validation annotations provided by Struts are:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Login Form - Struts2 Validation Example (Annotations)</title> </head> <body> <div align="center"> <h2>Login</h2> <h3>Struts2 Validation Example (Using Annotations)</h3> <s:form action="login" method="post" validate="true"> <s:textfield label="E-mail" name="email" /> <s:password label="Password" name="password" /> <s:submit value="Login" /> </s:form> </div> </body> </html>We can see that this page presents a login form with two fields: email and password. The attribute validate=”true” of the <s:form> tag specifies that Struts will generate Javascript code to perform client-side validation for this form.
package net.codejava.struts; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.validator.annotations.EmailValidator; import com.opensymphony.xwork2.validator.annotations.RequiredStringValidator; public class LoginAction extends ActionSupport { private String email; private String password; public String execute() { if (email != null && email.equals("admin@codejava.net")) { return SUCCESS; } else { return INPUT; } } @RequiredStringValidator(message = "Please enter your e-mail address.") @EmailValidator(message = "Please enter a valid e-mail address.") public void setEmail(String email) { this.email = email; } @RequiredStringValidator(message = "Please enter your password.") public void setPassword(String password) { this.password = password; } public String getEmail() { return email; } public String getPassword() { return password; } }The action method execute() will redirect the client to SUCCESS page if the e-mail equals to admin@codejava.net, or to the INPUT page otherwise. Notice that the setters of the fields email and password are annotated by two validation annotation types @RequiredStringValidator and @EmailValidator to specify validation rules and error messages for those fields.
@RequiredStringValidator(message = "Please enter your e-mail address.") @EmailValidator(message = "Please enter a valid e-mail address.") public void setEmail(String email) { this.email = email; }
@RequiredStringValidator(message = "Please enter your password.") public void setPassword(String password) { this.password = password; }
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Login Success</title> </head> <body> <div align="center"> <h2>Welcome, you have logged in successfully!</h2> </div> </body> </html>The action class will redirect the client to this page if the login form passes validation checks as the user enters the desired e-mail address and a password.
http://localhost:8080/Struts2ValidationBasicAnnotations/login-input
The login form gets displayed:Leave both fields empty and hit Login button, validation error messages appear above the fields like this:Try to enter a wrong-formatted email address and an arbitrary password:Now try to enter the email as admin@codejava.net with an arbitrary password:Voila, that works! Download the Eclipse project and ready-to-deploy WAR file in the Attachments section below. Related Struts Form Validation Tutorials: