Validators: Introducing Struts Validator Framework - Provide the appropriate ActionForm Class
(Page 4 of 4 )
The next part is providing and configuring the ActionForm. I am using the DynaActionForm, so the approach is declarative. The declaration of the form bean in struts-config.xml is:
<form-bean name="loginFormBean" type="org.apache.struts.validator.DynaValidatorActionForm">
<form-property name="form_login" type="java.lang.String"/>
<form-property name="form_password" type="java.lang.String"/>
</form-bean>
The only difference is with the type attribute. With normal forms it is “org.apache.struts.action.ActionForm,” whereas here it is ”org.apache.struts.validator.DynaValidatorActionForm."
Enable the error handling in JSP page
To use the server-side validation, the <html:errors> tag has to be used. But to enable client side validation certain additional tags (both HTML as well as Struts HTML) have to be supplied.
First the <javascript> custom tag of the HTML tags category in Struts must be placed within the <head> tag.
<head>
<link rel="stylesheet" href="css/print.css" type="text/css"
media="print">
<link rel="stylesheet" href="css/styles.css" type="text/css">
<link rel="stylesheet" href="css/forms.css" type="text/css">
<html:javascript formName=" loginFormBean" />
</head>
Next add an onSubmit handler to the <html:form> tag.
<html:form action="LoginAction.do" onSubmit=”return validateLoginFormBean(this);”>
:
:
</html:form>
The framework generates the required JavaScript code automatically. But the naming convention is such that the validate prefix is used with the form bean name to generate the onSubmit() handler. Hence the snippet ”return validateLoginFormBean(this);”.
Hook the Validator with the Struts framework
The last step is to configure the struts-config.xml so that the Struts framework can communicate with the Validator framework. This is done using the plugin tag of struts-config.xml. First declare the plug-in using the <plugin> tag. The className attribute is used to specify the class implementing the plug-in functionality. Since the Validator framework has to be used the className has the value “org.apache.struts.validator.ValidatorPlugIn”
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
:
:
</plug-in>
Next the path where the XML files are placed must be given. For this use property and value attributes of <set-property> tag.
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property property="pathnames"
value="/WEB-INF/validator-rules.xml,/WEB-INF/validations.xml"/>
</plug-in>
The value of the property is pathnames and the value of value is the path where the XML files are kept. Now the application is ready to be used.
This brings us to the end of the first part of the Validator framework tutorial. So what are other features provided by this framework and how can they be utilized to reduce development time? I will give details in the next part. Till next time.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |