Before use validating, you must sure that you can create the Struts form. If not, read following article, it's a tutorial how to Create Basic Struts Form.
First, you have to understand that Struts Validation will only work if your form-bean extends org.apache.struts.validator.ValidatorActionForm. So, your form which be validated, will looks like:
public class ExampleForm extends ValidatorActionForm {
...
}
Following lines belows are code of form-bean in this aticle:
package prlamnguyen.struts.form;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.*;
import org.apache.struts.validator.ValidatorActionForm;
/**
* @author Nguyen, Lam Duy
* @link http://prlamnguyen.blogspot.com/2007/02/java-tutorial-validate-form-with-struts.html
*/
/**
* Form bean for the Struts Validation Example.
*
*/
public class ExampleForm extends ValidatorActionForm
{
private String name=null;
private String emailAddress=null;
public void setName(String name){
this.name=name;
}
public String getName(){
return this.name;
}
public void setEmailAddress(String emailAddress){
this.emailAddress=emailAddress;
}
public String getEmailAddress(){
return this.emailAddress;
}
/**
* Reset all properties to their default values.
*
* @param mapping The mapping used to select this instance
* @param request The servlet request we are processing
*/
public void reset(ActionMapping mapping, HttpServletRequest request) {
this.name=null;
this.emailAddress=null;
}
/**
* Validate form input before excuted.
*
* @param mapping The mapping used to select this instance
* @param request The servlet request we are processing
* @return errors
*/
public ActionErrors validate(
ActionMapping mapping, HttpServletRequest request ) {
ActionErrors errors = new ActionErrors();
if( getName() == null || getName().length() < 1 ) {
errors.add("name",new ActionMessage("error.name.required"));
}
if( getEmailAddress() == null || getEmailAddress().length() < 1 ) {
errors.add("emailaddress",new ActionMessage("error.emailAddress.required"));
}
return errors;
}
}
The above class populates the Example Form data and validates it. The validate() method is used to validate the inputs. If any or all of the fields on the form are blank, error messages are added to the ActionMapping object. In Struts 1, ActionError seem to be deprecated and will be removed in Struts 2, so, i'm now using ActionMessage in this article. Ok, for the next, you must create a new Action class for Struts, because of form-bean's name is ExampleForm, Action class must be named as ExampleAction and extends org.apache.struts.action.Action. Form-bean above is Model of web struts application and the action class is Controller. I'll not guide to create an Action class, the previous post, i had created one, see here, config Struts is so easy and it was posted in that post.
Application Resources
An importance when using Struts to validate form is display error messages. Ignore everything about Controller and Struts Config, i'll explaint how to display error messages. First, you have to create ApplicationResources file in Struts Package, in my post, i packaged my model as "prlamnguyen.struts.form", so ApplicationResources will be in "prlamnguyen.struts" with name ApplicationResources.properties.
- ApplicationResources.properties
# Resources for parameter 'prlamnguyen.struts.ApplicationResources'Now, create a jsp file to input data. Note, when displaying error messages in jsp page, you can display individual or group error messages. If individual, add property value for each <html:errors /> else if group errors, only thing to do is adding <html:errors /> to anywhere you want to display error messages.
# Project Example Struts Validation
# This will appear before each individual error.
errors.prefix=<span class="errors">
# This will appear after each individual error.
errors.suffix=</span><br />
errors.name.required=Name is required.
errors.emailAddress.required=Email Address is required.
- Individual
page language="java" pageEncoding="ISO-8859-1" taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean" taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"
<html>
<head>
<title>JSP for ExampleForm form</title>
</head>
<body>
<html:form action="/example">
Name : <html:text property="name"/><html:errors property="name"/><br/>
Email Address : <html:text property="emailAddress"/><html:errors property="emailAddress"/><br/>
<html:submit/><html:cancel/>
</html:form>
</body>
</html>
- Group
page language="java" pageEncoding="ISO-8859-1" taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean" taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"<html>Deploy your project and test with example URL: http://localhost:8080/WebTutorial/form/example.jsp
<head>
<title>JSP for ExampleForm form</title>
</head>
<body>
<html:errors />
<html:form action="/example">
Name : <html:text property="name"/><br/>
Email Address : <html:text property="emailAddress"/><br/>
<html:submit/><html:cancel/>
</html:form>
</body>
</html>
Screen when do nothing is:
Screen when validate() method was excuted
and Group Error Messages
Validation in Struts is so easy, right ^^ ?
Post a Comment