Docsity

Configuring Validation Rules for JavaScript in Struts: A Comprehensive Guide, Slides of Java Programming

Partial preview of the text

Download Configuring Validation Rules for JavaScript in Struts: A Comprehensive Guide and more Slides Java Programming in PDF only on Docsity!

Configuring validation-rules.xml for

JavaScript

 A custom tag is used to generate client-side validation

based on a javascript attribute being present within the

element in the validation-rules.xml file

 Use the custom tag in your JSP page

 The text from element is written to the JSP

page to provide client-side validation

Configuring validation-rules.xml for

JavaScript

**

Things You Have to Do in your JSP

Page

 You will need to include the tag with

the name of the ActionForm that it's going to validate

against

 The formName attribute is used to look up the set of

validation rules to include as JavaScript in the page

logon.jsp Page (from struts-example

sample code)

Things You Have to Do in your JSP

Page

 The tag generates a function with the

name validateXXX( ) , where XXX is the name of the

ActionForm

 Thus, if your ActionForm is called logonForm, the javascript

tag will create a JavaScript function called

validateLogonForm( ) that executes the validation logic

 This is why the onsubmit( ) event handler called the

validateLogonForm( ) function.

logon.jsp Page (from struts-example

sample code)

:

Advanced Features

 Creating custom validation rules

 Using programmatic validation along with Validator

Creating Custom Validation Rules

 Create a new validation method

 Modify the validation-rules.xml file to rules. xml

contain the new validation method

 Modify the validation.xml file to use the new

validation rule rule.

Create a new validation method

public static boolean validateExactLength( Object bean, ValidatorAction va, Field field, ActionErrors errors,

HttpServletRequest request) {

String value = null;

if (isString( bean)) {

value = (String) bean;

} else {

value = ValidatorUtil. getValueAsString( bean, field. getProperty());

String exactLength = field. getVarValue(" exactlength");

if (! GenericValidator. isBlankOrNull( value)) {

try {

int length = Integer. parseInt( exactLength);

if ( value. length() != length ) {

errors. add( field. getKey(), Resources. getActionError( request, va, field));

return false;

} catch (Exception e) { errors. add( field. getKey(), Resources. getActionError( request, va, field));

return false;

} return true;

Modify validation.xml

** exactlength**

** 10 **

** mask**

^[ a- zA- Z]$*

Best Practice Guidelines

 Separate validation XML configuration files for each

major functional areas of your application

 Configure them in a similar manner in which multiple

Struts configuration files are configured

Advaced Validation

Techniques

(From Struts Cookbook

written by Bill Siggelkow)

QUIZ

 Differentiate between MVC Model 1 and MVC Model 2 (

marks)

 Why a Java Programmer should use Struts. (5 marks)

 With help of a diagram describe how struts works. What

new technology struts has introduced in J2EE. (15 marks)

 If you are required to develop a form with auto generated

ActionForms but need to develop some custom validators

what will be your approach.

(5 marks)