struts


yureka

This another day to get into struts. See, I have an input type as image and onclick function.

<input type=”image” name=”" src=”/teller/images/close.gif” onclick=”del(200);”>

The 200 given as parameter should be passed from struts form bean. Different type of combinations lead to error. finally defining a variable helped as follows.

<bean:define id=”myID” name=”resultI” property=”id”></bean:define>

<html:image src=”/teller/images/close.gif”  onclick=”<%=”del(“+myID+”);” %>”></html:image>

hurray

Today I had downloaded struts 1.2.7 and tried to write an application. (I know I am selecting a bit old.) I configured the datasource in struts. Alas. When I tried to start my server, tomcat 6.0, I got the error saying "java.lang.ClassNotFoundException: org.apache.commons.dbcp.BasicDataSource". With Googling I found struts-legacy.jar need to downloaded and copied to lib folder of Tomcat. (I couldnt get the recent version of struts-legacy. I found something in archive). Happily restarted the server. I saw java.lang.NoClassDefFoundError: org/apache/commons/pool/impl/GenericObjectPool.. oof.. again I need to download commons-dbcp-1.2.2.jar and commons-pool.jar.

My search come to an end :) It is working fine now.


I was trying to look at the validator plugin for struts. Struts counted the number of its beginners by one, ya. that’s me!!

Previously I have a simple collection of jsps and classes. something like get employee number, and display the employee details. employee number is in the form of xxx-xx-xxxx, where an x is a number. I had my form bean that has a validate method, that checks whether the number has been entered and the number entered is in the specified format. see below!

// Validate format of social security number.
private static boolean isValidSsNum(String ssNum) {
if (ssNum.length() < 11) {
return false;
}

for (int i = 0; i < 11; i++) {
if (i == 3 || i == 6) {
if (ssNum.charAt(i) != ‘-’) {
return false;
}
} else if (“0123456789″.indexOf(ssNum.charAt(i)) == -1) {
return false;
}
}

return true;
}

if it returns false, then new ActionError will be added errors and returned from validate() method. then comes the validator chapter :) actually, they tries to keep validation routines in a centralised location and call whenever there is a need.

Though it took time to keep track of the control flow, i feel it is a good practice, which i want to follow. They define the validation rules in an xml (validator-rules.xml) and map these rules to actions with another xml (validation.xml). Necessary changes have been done to FormBeans/DynaBean declarations.

Because of the validator plugin, both client-side and server-side validations becomes easier now.

Forexample, validator-rules.xml has an validator name ‘required’, which check the existance of data in form fields. the javascript code needed to perform this has been written in the xml file itself. So, it is directly embedded in the jsp whereever needed and executed.

Each definition is declared with validator tag. it is used to assign logical name to the routine with name attribute, and class and method for the routine.

Changes to Form Beans
If you have a Form Bean that extends ActionForm, make it to extend ValidatorForm. remove the reset() and validate() methods since they are implicitly given by the Validator framework.

import org.apache.struts.validator.ValidatorForm; public class MyForm extends ValidatorForm { }

The Form bean configuration in struts-config.xml is not changed.



type=”org.grassfield.MyForm”/>

The logical name ‘myForm’ is the one we will use for making validations. keep a note of that!

Then lets go for validation.xml, where we will map the forms with validations.




property=”username” depends=”required”>

If you have used Dynamic Form Beans, then your entries may go like this.


type=”org.apache.struts.validator.DynaValidatorForm”>

yes, you are right. you will be using DynaValidatorForm instead of DynaActionForm.

now coming back to my page… I need to do

1. Change the form to extend ValidatorForm (done!)
2. add a validator-rules.xml file. (yes, it is there )
3. create a validation.xml file with the following entry
I need to validate whether my ssNum is in the form of xxx-xx-xxxx format. so new entry has been added to validation.xml


property=”ssNum” depends=”mask”> mask ^\d{3}-\d{2}-\d{4}$
4. Add the validator plugin to struts-config.xml file

value=”/WEB-INF/validator-rules.xml, /WEB-INF/validation.xml”/>

has been added.

5. Add validation error message to Application Resources. (done)
6. recomplile, repackage and run the webapp!! (done)
it is working fine :)

yes. got a basic idea of validator module. I need to go through how to embed javascript for client side validation and how to internationalise the error messages. then validator may be complete!

See You!
————————-

By default, struts comes with many validations preconfigured. please see http://struts.apache.org/1.2.4/userGuide/dev_validator.html for more details!

Next Page »