Question

I'm new to JSP and JSTL and am having an issue where code in a <%= .. %> block is not being evaluated when it forms only part of a custom tag attribute

<myForm:text myBean="${myBean}" inputClass="form-class" inputName="pageNumber"
    label="Go to page" size="1"
    onkeypress="javascript:checkKey(<%= formName %>, '<%=listRequestFor%>', <%=recPerPage%>, <%=numPages%>);" 
    onkeydown="javascript:numbersOnly();" 
    onchange="javascript:goToPage(<%=formName%>, '<%=listRequestFor%>', <%=recPerPage%>, <%=numPages%>);" />

And this produces HTML something like the following:

<label for="" class="">
    Go to page 
</label>

<input type="text" id="" class="form-control" name="pageNumber"
    value="" size="1"     
    onkeypress="javascript:checkKey(&lt;%= formName %&gt;, '&lt;%=listRequestFor%&gt;', &lt;%=recPerPage%&gt;, &lt;%=numPages%&gt;);" 
    onkeydown="javascript:numbersOnly();" 
    onchange="javascript:goToPage(&lt;%=formName%&gt;, '&lt;%=listRequestFor%&gt;', &lt;%=recPerPage%&gt;, &lt;%=numPages%&gt;);">

It looks like the <%= .. %> tags are being escaped and therefore the JSP expression is not evaluated before the custom tag is evaluated... any ideas how to get around this?

Was it helpful?

Solution

As a dirty hack (no less than JSP deserves) I've defined variables which can be evaluated before being passed to the custom tag:

<%
String onkeypress = "javascript:checkKey("+formName+", '"+listRequestFor+"', "+recPerPage+", "+numPages+");";
String onchange = "javascript:goToPage("+formName+", '"+listRequestFor+"', "+recPerPage+", "+numPages+");";
%>

<myForm:text myBean="${myBean}" inputClass="form-class" inputName="pageNumber"
    label="Go to page" size="1"
    onkeypress="<%= onkeypress %>" 
    onkeydown="javascript:numbersOnly();" 
    onchange="<%= onchange %>" />

This seems to have resolved the issue although it would be nice to understand why/what is going on

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top