Sunday, May 22, 2005

Tapestry update

Well - as previously mentioned I've startet developing a web application using Tapestry as framework. Tapestry has a clear seperation of concerns which is why I chose to look at it in the first place.

Many JSP developers think that implementing and using custom tags is a clear seperation of concern from layout and logic but I party disagree.
Whether you use a

<c:foreach...>

or a

<% for(Iterator i in coll.iterator;i.hasNext();) %>

is practically the same. You still have logic (looping) in your layout, but using the foreach tag is ofcoz to be preferred (the lesser evil).

Tapestry on the other hand does things alot different which is a step in the right direction in my opinion.
Let me show you an example using the above looping first in JSP, then the tapestry way:


<table border="0">
<c:forEach var="item" items="${myBean.items}">
<tr>
<td> <c:out value="${item.returnString}"/> <td>
<tr>
</c:forEach>
</table>


Extremely simple JSP fragment iteration thru a collection of a context bound bean named "myBean". Each entry in the collection is bound in page-context by name "item" and referenced in the c:out part, where the method "returnString" is called on each item. Nothing fancy, just plain and simple...


<table border="0">
<tr jwcid="foreach">
<td> <span jwcid="string">Some text 1</span> <td>
<tr>
<tr jwcid="$remove$">
<td> <span jwcid="string">Some text 2</span> <td>
<tr>
</table>


The above is pretty much the same as the JSP example except its possible to edit this in a normal HTML editor and there is NO logic what-so-ever in the presentation layer. No saying which property of which bean we're looping thru and which property for each item should be presented in the TD section... All this is done in either the page or component specification (of tapestry).


<component id="foreach" type="Foreach">
<binding name="source" expression="myBean.collection"/>
<binding name="value" expression="value"/>
</component>

<component id="string" type="Insert">
<binding name="value" expression="value.returnString"/>
</component>


Well - You'd probably have to have some knowledge of Tapestry to get the full picture here, but basically its saying:

Theres a component called "foreach" (jwcid="foreach" in HTML) which is "substituted" by a real Foreach component, which will loop thru myBean.collection and each item is bound to a property called value.
Then there's a component called "string" (jwcid="string" in HTML) which is "substituted" by a real Insert component (much like c:out) which will take the value from value.returnString and insert into HTML output.

Damn simple and powerful..

Well - to finish this off I'll say that the above examples are made just-out-of-my-head and might not "compile", but should give a decent picture of the game. As for the jwcid="$remove$" part, look itup in the tapestry users manual.. You'll see why that is smart... :-)

0 Comments:

Post a Comment

<< Home