Dev Cafe

Friday, April 09, 2010

'Extends' in Struts Validation.

In my last post I had over simplified what we as a team were doing. There were lot of constraints in implementation on such constraint was validation. Its well know among struts developers that Struts provides one of the robust and easily configurable validation framework.


Struts validation provides extension and override mechanism. Struts can inherent bunch of properties from any form and you can add additional form property validation. For example If we have form F1 and F2. And If both form F1 and F2 have property X,Y

<form name="/F1">
<field property="X" depends="required,maxlength,mask">
<arg key="Xmsg" position="0">
<arg name="maxlength" key="${var:maxlength}" resource="false" position="1">
<var>
<var-name>maxlength
<var-value>${92float}</var>
</var>


</field>

<field property="Y" depends="required,maxlength,mask">
<arg key="Ymsg" position="0">
<arg name="maxlength" key="${var:maxlength}" resource="false" position="1">
<var>
<var-name>maxlength
<var-value>${92float}</var>
</var>
</field>
</form>

To reuse the validation that is already available in form F1.

<form name="/F2" extends="F1">
</form>

So all the validation properties available in F1 is now available in F2 with extends keyword. Basically we are establishing a hierarchy here.

Now assume if we have a scenario were field X is not a required for form F2 but required to F1. Since we have a inheritance we can as well override Just the X property in F2 only

<form name="/F2" extends="F1">
<field property="X" depends="maxlength,mask">
<arg key="Xmsg" position="0">
<arg name="maxlength" key="${var:maxlength}" resource="false" position="1">
<var>
<var-name>maxlength
<var-value>${92float}</var>
</var>
</field>
</form>

So, struts provides extension mechanism at form level and override option at property level.

Labels:

Thursday, April 08, 2010

Java and HTML dont go together.

In the current project which I am working is an interesting one. We have one gigantic jsp page with close to 100 fields on a average. Out of the 100 fields, 60 of them are constant fields and rest being driven by 6 parameters. And each parameter has 4 possibilities so basically 4*(6!) combination.

So, we started out with creating components that can reused when the combination repeats. For the first couple of releases we had great success, we shrunk the development time; the page that used to take 3 days now took only 1 day or less. We implemented around 100 combination quickly.

After 3 or 4th release now we had a new business requirement that wanted a change... Oops... the business said that these never changes.... (Lesson learned:What ever we develop always changes.. Always have provision for change). Now we had to revisit the components which were assumed to be common are not common any more now.

We started the second iteration. this time around we pushed all the sturts controls in the jsp. We came up with only one taglibrary this time. We separated the Java Code and HTML. Each html component for example Telephone Number component (A label and 3 text field) is wrapped by this custom tag-library. The custom tag's main task is determine whether to doBody or skipBody. The custom tag now uses a Util class's static method which returns a boolean value. Based on the boolean value doBody or skipBody is invoked.

This way we have one generic solution, Adding or removing an existing control in the page is just a change in the xml file. So this enable to do maximum re-use of components and easy to maintain.

Labels: