Wednesday, December 15, 2010

jQuery Template Markup and JSP Expression Language (EL)

Maybe it is "great minds think alike"; both JSP Expression Language (EL) and jQuery Template use the
${}

as variable placeholders and evaluate it. Since JSP will be complied at server side first, any ${} in JSP file will be evaluated first, even it means for jQuery template. When your result page return to user's browser and jQuery template energy try to complie the template, you will find your ${} had been changed to variable value you store in Page, Session or Application context in your sever side.

Therefore, we need to escape ${} in JSP page which means for jQuery template or turn off Expression Language evaluate. There are two options:

1. escapse ${} in JSP page


In EL, you can use character ${'${'}} to be escapted as ${}

for example,
<script id="clientTemplate" type="text/html">
<li><a href="clients/${id}">${name}</a></li>
</script>

need to be escaped as:


<script id="clientTemplate" type="text/html">
<li><a href="clients/${'${'}id}">${'${'}name}</a></li>
</script>

This approach is good when your template is simple.

2. save jQuery template as separated file and disable its EL evaluation


if you have a lot of jQuery templates, it's better to extract them out to a seperated file.
<%@ page isELIgnored="true" %>
<script id="clientTemplate" type="text/html">
<li><a href="clients/${id}">${name}</a></li>
</script>
<script id="anotherTemplate" type="text/html">
<li><a href="clients/${id}">${name}</a></li>
</script>
Then include this file in your JSP pages need template:
<jsp:include page="jqTemplate.jsp"></jsp:include>

5 comments:

Todd said...

I'm putting together my Christmas card list and see your blog. Nice to see you're doing okay in Australia. The last address I have for you is in Indianapolis. I'd love to catch up with you. You can find my contact info easily on a Google search. Take care,

Todd Muhleman

pieknyroman said...
This comment has been removed by the author.
Fabian said...

what about something like \${id}

dre said...

thank you very much

Derek said...

Thanks for posting this. I wasted about an hour trying to figure out why my data wouldn't populate in a JSP page, but would in an HTML page.