Friday, June 13, 2008

Using JSTL tags with struts and javascript

We can use jstl tag library and struts together to get rich programming

For this you need to import java.sun.com/jstl/core_rt" prefix="c" in your jsp

<c:set var="displayOption" value="${0}"/>

<nested:iterate id="serviceRepStn" name="resultList" property="stations" indexId="indexId">
<c:set var="displayOption" value="${indexId}"/>

<td>
<nested:text property="srrpStops" styleClass="stops" readonly="true"
styleId="stops${displayOption}"/>
</td>


//We can access the text element in javascript as below

var stopsCollect = document.forms[0].elements["stops${displayOption}"];

// If the text fields are dynamically generated and we do not know whether an array of srrpStops or single element will be passed to us in the request, we can handle it as below

var sum = 0;

var stopsCollect = document.forms[0].elements["stops${displayOption}"];
if(stopsCollect.length == undefined)
{
stopsCollect = document.forms[0].stops${displayOption-1};
if (!isNaN(parseFloat(stopsCollect.value)))
{
sum += parseFloat(stopsCollect.value);
alert("Sum : " + sum);
}
}
else
{
for (var i = 0; i < stopsCollect.length; i++)
{
if (!isNaN(parseFloat(stopsCollect[i].value)))
{
sum += parseFloat(stopsCollect[i].value);
}
}

}

No comments: