Integrating with Apache Struts
Overview
MetaBoss Datatypes usually perform very strict validation at the time when attempt is made to
create an instance of the datatype. This validation process will throw exception back to caller if
any problem with proposed data is discovered. On the other hand Struts has a declarative form validation mechanism,
which allows to define form validation rules in xml document. If Struts validation is used in web application
(and this seem to be a popular choice) the form is validated and, in case it has validation errors,
all accumulated error messages are automatically displayed back on a source page. All this is
happening well before any custom action java code is executed. This means that MetaBoss datatype validation
inside operation invocation preparation code in the body of the custom action occurs too late and
any validation error encountered at this stage will not be displayed on the form in the way other validation errors are.
In theory it is possible to solve this by simply duplicating MetaBoss datatype
validation rules inside Struts form validation declaration files. However this is very hard work. It is
challenging, if not impossible, to keep both MetaBoss Datatype validation rules and Struts validation rules
in sync and also this does not reuse any of MetaBoss datatype validation code
MetaBoss includes a better solution for this problem. It contains a
custom plug-in validator created in accordance with Struts specification. This validator plug-in can be
configurred via Struts form validation declaration to validate any form field against
any known MetaBoss datatype. Internally this validator delegates actual validation process to the MetaBoss datatype class
and returns all validation erros back to Struts validation framework for displaying amongst other
validation erros in the standard fashion. The advantage of using the MetaBoss datatypes validator is that
field content validation becomes Model Driven and occurs in one place in the Java code. Struts's form validation
definition no longer contains any specific attributes of validation (e.g. matching regular expression, max length, min length etc).
All it contains (with regards to single field content validation) is the mapping between form fields and their corresponding
datatypes !
Brief description of integration process
- Ensure that MetaBossStrutsIntegration.jar is included in your web application distribution.
This jar contains MetaBoss datatypes plug-in validator and can be found in ${metaboss_home}/lib/MetaBossStrutsIntegration.jar
- Define MetaBoss data type validator message in ApplicationResources.properties file in your web application:
# Special MetaBoss datatypes validator message
# Arg0 - usual (for Struts) form field name
# Arg1 - reserved for use by some other validators (e.g. minLength, maxLength etc)
# Arg2 - reserved for use by some other validators (e.g. minLength, maxLength etc)
# Arg3 - the MetaBoss data type validation message. Supplied by MetaBoss data type validator at run time
errors.metaboss={0} is not valid. Reason: {3}
- Define MetaBossDatatypeCheck validation in validator-rules.xml in your web application:
<validator name="MetaBossDatatype"
classname="com.metaboss.enterprise.xi.apachestruts.MetaBossDatatypeCheck"
method="validate"
methodParams="java.lang.Object,
org.apache.commons.validator.ValidatorAction,
org.apache.commons.validator.Field,
org.apache.struts.action.ActionErrors,
javax.servlet.http.HttpServletRequest"
msg="errors.metaboss"/>
- For each field linked to particular MetaBoss Datatype class define
validation mapping in validation.xml in your web application.
Example below defines firstName field to be mandatory by invoking standard
Struts validation and, if value is present, directs Struts to invoke
MetaBossDatatype validator against com.hatmaker.crm.types.core.UserName datatype
<field property="firstName" depends="required,MetaBossDatatype">
<arg0 key="registrationForm.firstname.displayname" />
<var>
<var-name>datatypeclass</var-name>
<var-value>com.hatmaker.crm.types.core.UserName</var-value>
</var>
</field>
Another example below links phone field to com.hatmaker.crm.types.PhoneNo datatype
<field property="phone" depends="MetaBossDatatype">
<arg0 key="registrationForm.phone.displayname" />
<var>
<var-name>datatypeclass</var-name>
<var-value>com.hatmaker.crm.types.PhoneNo</var-value>
</var>
</field>
Additional Notes
MetaBoss Datatypes validator plug-in will throw ClassNotFound exception if
required datatype class is not found. Normally this should not be a problem and
presence of the datatype class is not a specific plug-in requirement. This is because
datatype classes must be made available to web application so it will be able to invoke
operations.
MetaBoss Datatypes validator plug-in outputs some DEBUG level logging messages
(via Apache Commons Logging framework) when it loads datatype classes and when it fails field validation.
At the moment MetaBoss Datatypes validator plug-in does not support internationalisation and
Java Script validation definition.
|
Copyright © 2000-2005 Softaris Pty.Ltd. All Rights Reserved. MetaBoss is the registered trademark of Softaris Pty.Ltd. Java, Enterprise JavaBeans, JDBC, JNDI, JTA, JTS, JCA and other Java related APIs are trademarks or registered trademarks of Sun Microsystems, Inc. MDA, UML, MOF, CORBA and IIOP are trademarks or registered trademarks of the Object Management Group. Microsoft, .NET and C# are trademarks or registered trademarks of the Microsoft Corporation. All other product names mentioned herein are trademarks of their respective owners.
|