Extending Validate and Custom Validation in the validate()

Extending Validate and Custom Validation in the validate()

Previous Home Next

 

In this chapter,We will learn how to work Custom Validation in the validate() method of an Action.There are two main types of form validation.The First type is validation is per field that means the validation takes into account only one field at a time.The Second of form validation which is applied all the field in the form.Form validation is the process of checking that a form has been filled in correctly before it is processed.

 You can easily add custom validators for special requirements.validate() method is used to validate properties after they have been populated; Called before FormBean is handed to Action.This method returns a collection of ActionMessage as ActionErrors.

The org.apache.struts.validator.ValidatorForm class is needed to integrate the validator framework to struts.This class is subclass of ActionForm class and overriding the validate method of ValidatorForm and extending the functionality of the ValidatorForm.

1. Override the validate() method and call the ValidatorForm class  validate()  method.
2. If The  super class produces the errors  then save this reference.
3. We can add additional errors.
4. finally return the errors.

Directory Structure of JavascriptTagExample in Struts 1.3 Using MyEclipse IDE




index.jsp

<%@taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<%@page contentType="text/html" 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>Struts html:javascript tag example</title>
</head>
<body>
<h3>Struts html:javascript tag example</h3>
<html:form action="/javascriptsAction" onsubmit="return validateJavascriptsForm(this);">
<html:javascript formName="javascriptsForm" />
<bean:message key="label.name"/>:
<html:text property="name" name="javascriptsForm"/><br/>
<bean:message key="label.password"/>:
<html:password property="password" name="javascriptsForm"/><br/>
<br/>
<html:submit><bean:message key="label.submit" /></html:submit>
<html:reset><bean:message key="label.reset" /></html:reset>
</html:form>
</body>
</html>

web.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >
 
<web-app>
  <display-name>Maven Struts Examples</display-name> 
  <servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>
        org.apache.struts.action.ActionServlet
    </servlet-class>
    <init-param>
        <param-name>config</param-name>
        <param-value>
         /WEB-INF/struts-config.xml
        </param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet> 
  <servlet-mapping>
       <servlet-name>action</servlet-name>
       <url-pattern>*.do</url-pattern>
  </servlet-mapping> 
</web-app>

struts-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC 
"-//Apache Software Foundation//DTD Struts Configuration 1.3//EN" 
"http://struts.apache.org/dtds/struts-config_1_3.dtd">
<struts-config>
  <form-beans>
    <form-bean name="javascriptsForm" type="org.r4r.struts.JavascriptForm"/>
  </form-beans>
  <global-exceptions />
  <global-forwards />
  <action-mappings >
    <action path="/javascripts" type="org.apache.struts.actions.ForwardAction" parameter="/index.jsp"/>
		<action path="/javascriptsAction" type="org.r4r.struts.JavascriptAction" 
		name="javascriptsForm" input="/index.jsp" validate="true" scope="request">	
         <forward name="success" path="/success.jsp"/>
		</action>
  </action-mappings>
  <message-resources parameter="org.r4r.struts.ApplicationResources" />
  <plug-in className="org.apache.struts.validator.ValidatorPlugIn">
        <set-property
            property="pathnames"
            value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/>
    </plug-in>  
</struts-config>

JavascriptForm.java

package org.r4r.struts;

import org.apache.struts.action.ActionForm;

@SuppressWarnings("serial")
public class JavascriptForm extends ActionForm {
	private String name;
	private String password;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
}

JavascriptAction.java

package org.r4r.struts;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class JavascriptAction extends Action {
	public ActionForward execute(ActionMapping mapping,ActionForm form,
		HttpServletRequest request,HttpServletResponse response) throws Exception{
		@SuppressWarnings("unused")
		JavascriptForm javascriptsForm=(JavascriptForm)form;
		return mapping.findForward("success");
	}

}

ApplicationResources.properties

#label message
label.name = User Name 
label.password = Password
label.submit = Submit
label.reset = Reset
errors.name= Name 
errors.password= Password Length
errors.minlength=Password Length Greater than Six
# -- validator --
errors.invalid={0} is invalid.
errors.maxlength={0} can not be greater than {1} characters.
errors.minlength={0} can not be less than {1} characters.
errors.range={0} is not in the range {1} through {2}.
errors.required={0} is required.
errors.byte={0} must be an byte.
errors.date={0} is not a date.
errors.double={0} must be an double.
errors.float={0} must be an float.
errors.integer={0} must be an integer.
errors.long={0} must be an long.
errors.short={0} must be an short.
errors.creditcard={0} is not a valid credit card number.
errors.email={0} is an invalid e-mail address.

success.jsp

<%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
 
<html>
<head>
<title>Struts html:javascript tag example</title>
</head>
<body>
<h3>Struts html:javascript tag example</h3>
<h4>Welcome , <bean:write name="javascriptsForm" property="name" /></h4>
</body>
</html>

Output




Previous Home Next