Before I move on to spares and strikes, it would be nice to see how the score sheet looks. In an earlier post, I claimed that one of the reasons for integrating the UI early is to make sure the domain model will satisfy the requirements of the user interface. Let's see if it does.
The JSTL code that I wrote assumes a Game
object in the page context.
I should be able to fake that quite easily so we can get a preview of how the score card will look. Let's remind ourselves what the code to render the score card is like:
<table>
<tr>
<c:forEach varStatus='loop' items='${game.frames}'>
<td colspan='2'>${loop.count}</td>
</c:forEach>
</tr>
<tr>
<c:forEach var='frame' items='${game.frames}'>
<td>${frame.firstBall}</td>
<td>${frame.secondBall}</td>
</c:forEach>
</tr>
<tr>
<c:forEach var='frame' items='${game.frames}'>
<td colspan='2'>${frame.score}</td>
</c:forEach>
</tr>
</table>
I can easily create a Game
object and fill it with data using a scriptlet. I probably wouldn't do this for production code
but, for a throwaway example, it's the quickest way to get something up and running without getting lost in the
intricacies of the Model 2 architecture for J2EE. Here's a scriptlet that creates a game with 10 frames of a 4
followed by a 5...
<%
Game game = new Game();
for (int i = 0; i < 10; i++) {
game.bowl(4);
game.bowl(5);
}
pageContext.setAttribute("game", game );
%>
...which I load in my browser. Drum roll please...
javax.servlet.ServletException: Don't know how to iterate over supplied "items" in <forEach>
Oh dear! A little Googling shows that the forEach
loop in JSTL knows how to iterate over all kinds of things:
Iterable
. Sigh. I guess that the JSTL spec predates Java5.
A little hacking will make it work...
ArrayList list = new ArrayList();
for(Frame frame : game){
list.add(frame);
}
pageContext.setAttribute("game", list );
...and the resulting score sheet looks like this...
1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | ||||||||||
4 | 5 | 4 | 5 | 4 | 5 | 4 | 5 | 4 | 5 | 4 | 5 | 4 | 5 | 4 | 5 | 4 | 5 | 4 | 5 |
9 | 18 | 27 | 36 | 45 | 54 | 63 | 72 | 81 | 90 |
...which is entirely good enough for the stories so far.
Posted by Kevin Lawrence at March 19, 2007 09:16 AM
TrackBack URL for this entry: