Working with Client-Side JavaScript in Validator Framework

Working with Client-Side JavaScript in Validator Framework

Previous Home Next

 

Client Side validation is very useful to validate some fields on the client-side before sending the data to the server for processing.It saves the user a round trip to the server.The <html:javascript> tag to allow front-end validation based on the xml in validation.xml. In this tutorial, We will see how we can achieve client side JavaScript validation using struts validation framework.

 The Validator Framework provides an easy-to-use mechanism for performing client-side validation.The struts validation framework also provide you to validate form data on client side using JavaScript.The struts validation framework generates the JavaScript code for you. By this way We can ensure that the data send to the server is valid.

You need to perform the following step :-

1. Add the <html:javascript> tag in jsp file generates the client site valid.

<html:javascript formName="loginForm" dynamicJavascript="true" staticJavascript="true" />

This code generates the client side java script for the form "loginForm" as defined in the validation.xml file.

2. Add  the onsubmit attribute to the <html:form> tag.

<%@taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%>
<html>
<head>
</head><body>
<html:form action="/LoginAction" onsubmit="validateInputForm(this);">
<html:javascript formName="LoginForm" />
User Name : <html:text name="LoginForm" property="userName" /> 
<br>Password  : <html:password name="LoginForm" property="password" />
<br><html:submit value="Login" />
</html:form>
</body>
</html>

Sometimes the end user will need to handle all of the validation errors before they can submit the form to the server.The validateInputForm  function will display the error message if the form not to submitted.

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