Custom JSP tag file <% @variable ..> not working?

I want to define a custom JSP tag that creates an HTML table with alternating row background colors. Therefore, the CSS classes “odd” and “even” are set depending on the table row index.

The example without the JSP tag works fine, and the question arises: what happens in the example custom tag below? How to pass the variable "rowIndex" through both user tags?

The link http://docs.oracle.com/javaee/1.4/tutorial/doc/JSPTags5.html does not help.

thank

An example of a JSP page that works:

<c:set var="rowIndex" value="1"/>
<table class="datatable">
    <tbody>
        <tr class="${(rowIndex %2 == 0) ? 'odd' : 'even'}"><c:set var="rowIndex" value="${rowIndex+1}"/>
            <td width="200px"><bean:message key='myKey' /></td>
            <td>todoValue</td>
        </tr>
        <tr class=${(rowIndex %2 == 0) ? 'odd' : 'even'}><c:set var="rowIndex" value="${rowIndex+1}"/>
            <td><bean:message key='myKey' /></td>
            <td>todoValue</td>
        </tr>
    </tbody>
</table>

An example JSP page with a custom tag that doesn't work:

JSP Page

<%@taglib tagdir="/WEB-INF/tags" prefix="myApp"%>

....

<myApp:keyValueDataTable>
    <myApp:keyValueDataTableRow />
    <myApp:keyValueDataTableRow />
    <myApp:keyValueDataTableRow />
</myApp:keyValueDataTable>

dataTable.tag

<%@ include file='/tiles/taglibs.inc'%>
<%@tag description="Description" pageEncoding="UTF-8"%>

<%@ variable name-given="rowIndex" %> 
<c:set var="rowIndex" value="1" />

<table class="datatable">
    <tbody>
        <jsp:doBody/>
    </tbody>
</table>

tableRow.tag

<%@ include file='/tiles/taglibs.inc'%>
<%@tag description="Description" pageEncoding="UTF-8"%>

<%@ variable name-given="rowIndex" %> 

        <tr class="${(rowIndex %2 == 0) ? 'odd' : 'even'}">
            <c:set var="rowIndex" value="${rowIndex+1}" />
            <td width="200px"><c:out value="Index=${rowIndex}"></c:out> <bean:message key='myKey' /></td>
            <td>todoValue</td>
        </tr>
+3
2

URI?

<%@ taglib prefix="utils" uri="http://www.example.com/tld/utils"%>
0

. :

<%@ attribute name="rowIndex" required="true" type="java.lang.Integer" %>

jsp, , rowIndex .

0

All Articles