post7: Struts Validator Framework Example This tutorial explains - TopicsExpress



          

post7: Struts Validator Framework Example This tutorial explains the Struts Validation Framework. In this example we create a Login page using DynaValidatorForm and validate the form fields using the Struts Validation Framework. The Validator Framework in Struts consist of two XML configuration files. The first one is the validator-rules.xml file which contains the default Struts pluggable validator definitions. You can add new validation rules by adding an entry in this file. The second one is the validation.xml file which contain details regarding the validation routines that are applied to the different Form Beans. These two configuration file should be place somewhere inside the /WEB-INF folder of the application. To use the validator pluggin the first step is to add it in the Struts configuration files as shown below. The pluggin should be added after any message resource elements in the struts configuration file as shown below. Lets see a simple login example using the DynaValidatorForm. First we need to create a From Bean that extends org.apache.struts.validator. DynaValidatorForm. Next step is to add validations to the form fields in the validation.xml file. Our Form name is "LoginForm" and field names are "userName" and "password". The validation.xml file contains the following code. minlength 6 Each tag can contain multiple tags. Specify the form name that you want to associate with the Validation Framework as the value of the name attribute of the form tag. Name of the form specified here should be same as the one specified in the struts-config.xml file. Now you can associate each properties of the form bean with one or more predefined validation rules . The depends attribute of the field tag takes comma-delimited list of rules to associate with each property. The userName property is associated with the "required" rule which means that the value cannot be empty. The error message to be displayed when a particular rule is not satisfied is specified in the ApplicationResource.properties file. LoginForm.userName = User Name errors.required={0} is required. We pass the key value as "LoginForm.userName" in the arg tag. The value for this key will be fetched from the ApplicationResource.properties file and this value will be used to generate the errors.required message. In our case if the userName is not entered, the error message will be displayed as "User Name is requierd." The only entry we need to make in the ApplicationResource.properties file is "LoginForm.userName = User Name", the other entry is already provided by the framework. Inorder to associate more than one validation rule to the property we can specify a comma-delimited list of values. The first rule in the list will be checked first and then the next rule and so on. Now lets see how the validation works. Click the Login button without entering any values, the following error messages are displayed to the user. hunk-->java
Posted on: Sun, 04 Aug 2013 16:39:15 +0000

Trending Topics



ss="sttext" style="margin-left:0px; min-height:30px;"> LORD JESUS SEEMS LIKE I AM A MANIQUIN THAT STANDS JUST IN OUR
Without opening the endeavor to competitive bidding, the U.S.
OK, we got an ask CB thread. We got an ask Kevin Hendrix thread.
WE NEED ONLINE ADVERTISER WHO WANT TO WORK FULL TIME OR
9771 hello admin ...bhai jitna Paisa bolega tere a/c me dal
Pray-Concern for my Safety: - I have been receiving life

Recently Viewed Topics




© 2015