The AMSeT Project Blog

Saturday Sep 26, 2009

Pathy News

Or... Status Symbols for the Java Developer.

This might save someone 10 minutes or so.

Here's the problem. You have nested arrays that you want to loop over to display in a jasper and bind these to form elements using Spring.

Specifically, the SWORD bean hierarchy in AMSeT contains a branch consisting of

serviceDocumentContext.serviceDocument.service.workflowList.collections.

The workflowList and collections objects are lists and each have a number of simple datatype fields that need to be bound to a form input element using Spring.

Using JSTL the basic construct is nested <c:forEach/> elements. Normally you would write something like

<c:forEach items="${serviceDocumentContext.serviceDocument.service.workflowList}" var="workflow">

and then loop using $workflow as the representative indexed item.

Now, each workflow has a title. The title of the first workflow would be accessed using the following variable:

${serviceDocumentContext.serviceDocument.service.workflowList[0].title}

Each collection has a title, which for the first worklow and first collection would be found at

${serviceDocumentContext.serviceDocument.service.workflowList[0].collections[0].title}

Both workflow and collections have several fields and you want to output them all. Some index variable is needed to label the iterations. This is where the varStatus attribute of the <c:forEach/> comes into play.

Forget about var for a while - this justs confuses the issue when it comes to Spring which doesn't recognise the usage.

The varStatus attribute represents a LoopTagStatus object, one of whose fields is index which is a
zero-start counter for the iteration.

So the forEach becomes:

<c:forEach items="${../../...workflowList}" varStatus="wloop" ></c:forEach>

For the inner collections loop we have:

<c:forEach items="${../../...collections}" varStatus="cloop"></forEach>

The general title field of collections then becomes:

${serviceDocumentContext.serviceDocument.service.workflowList[wloop.index].collections[cloop.index].title}

Note no dollars on the index.

In order to bind a form element to Spring the following construct is required:

<spring:bind path="serviceDocumentContext.serviceDocument.service.workspaceList[${wloop.index}].collections[${cloop.index}].title">

<input type="hidden" name="<c:out value='${status.expression}'/>" value="<c:out value='${status.value}'/>" />

</spring:bind>

Note the dollar variable notation for the index in the Spring path.

The ${status.expression} and ${status.value} variables apply to the object pointed to in the Spring path. The expression is the precise path expression of the object bound to the form element and the value is its value, a string or integer.

Cock-a-doodle-do.

Calendar

Search

Navigation