Velocity and Struts

Cool. In the contrib section of the Velocity directories on Jakarta's website I found a Velocity/Struts demo that someone whipped up (thank you). It's very cool because it's the same exact app as the example Struts app, except with Velocity on the front end instead of JSP/Tags.

Here's another good example of how bloated, ugly and unmaintainable XML in JSP pages is. Here's the a snippet of functionality from the subscription.jsp page in the Struts example:

<logic:equal name="subscriptionForm" property="action"

      scope="request" value="Create">

 <html:submit>

  <bean:message key="button.save"/>

 </html:submit>

</logic:equal>

<logic:equal name="subscriptionForm" property="action"

      scope="request" value="Delete">

 <html:submit>

  <bean:message key="button.confirm"/>

 </html:submit>

</logic:equal>

<logic:equal name="subscriptionForm" property="action"

      scope="request" value="Edit">

 <html:submit>

  <bean:message key="button.save"/>

 </html:submit>

</logic:equal>

And here's the exact same thing, but using Velocity instead of the tags. Same functionality. Same separation of View and Controller, but 100% more readable:

#if( $strutsaction == "Create")

 <input type="submit" name="submit" value="$message.get("button.save")">

#elseif( $strutsaction == "Delete" )

 <input type="submit" name="submit" value="$message.get("button.confirm")">

#elseif( $strutsaction == "Edit")

 <input type="submit" name="submit" value="$message.get("button.save")">

#end

It's much nicer the second way. In fact, using Velocity gives me much more control over the html than the tags. Notice how in the first example, the tags are going to return to me the HTML that is going to be put on my page. In the Velocity example, I have complete control over the input tags. I can adjust the HTML as I need to - for buggy browsers, or non-standard properties, etc.

I'm getting sold. I need to be REALLY sold to start doing my development in such a non-standard way, but I'm almost there. Close enough to do start a project this way at least.

-Russ

< Previous         Next >