Beyond trivial JSTL

J

jstorta

I see a lot of stuff about JSTL and how much cleaner it is than
scriptlets. I agree and would very much like to use nothing but JSTL
to create cleaner pages. However, I am having trouble finding
examples that show real world examples of JSTL.

One of the most common examples you see is a foreach loop that loops
through a set of data and prints out a table or some other construct.
This is something I do regularly with a scriptlet and I can see, on
the basic level, how it could be rewritten with JSTL.

But my scriptlets do more than just print out a list of the data.
They interpret that data and adjust the display to match the data.

For instance, let's say I am printing out a list of books by title,
author, and publication date. My scriptlet might do 2 things.
1) Highlight alternating lines in slightly different background colors
so they are easy to distinguish
2) Use a bold font for books written by this month's featured author.

Each of these things has to do with the display of each row so it
really does not belong within the object itself.

Here is an example that I got from JavaWorld and modified a bit for
this discussion.

Scriplet Method
<UL>
<%
String itemStyle = "default";
Book currentBook;

Iterator iterator = list.iterator();
while (iterator.hasNext()) {
currentBook= (Book)iterator.next();
if (currentBook.getAuthorLname().equals("Smith") ) {
itemStyle = "featured";
}
else {
itemStyle = "default";
}
%>
<LI class="<%=itemStyle %>" > <%= currentBook.getTitle() %> </
LI>
<%
}
%>
</UL>


JSTL Method
<UL>
<c:forEach var="currentBook" items="${list}">
<LI> <c:eek:ut value="${currentBook.bookTitle}"/> </LI>
</c:forEach>
</UL>


The question is, how do I incorporate the row formatting from the
scriptlet example into the JSTL sample?


Thanks,
 
D

Daniel Pitts

jstorta said:
I see a lot of stuff about JSTL and how much cleaner it is than
scriptlets. I agree and would very much like to use nothing but JSTL
to create cleaner pages. However, I am having trouble finding
examples that show real world examples of JSTL.

One of the most common examples you see is a foreach loop that loops
through a set of data and prints out a table or some other construct.
This is something I do regularly with a scriptlet and I can see, on
the basic level, how it could be rewritten with JSTL.

But my scriptlets do more than just print out a list of the data.
They interpret that data and adjust the display to match the data.

For instance, let's say I am printing out a list of books by title,
author, and publication date. My scriptlet might do 2 things.
1) Highlight alternating lines in slightly different background colors
so they are easy to distinguish
2) Use a bold font for books written by this month's featured author.

Each of these things has to do with the display of each row so it
really does not belong within the object itself.

Here is an example that I got from JavaWorld and modified a bit for
this discussion.

Scriplet Method
<UL>
<%
String itemStyle = "default";
Book currentBook;

Iterator iterator = list.iterator();
while (iterator.hasNext()) {
currentBook= (Book)iterator.next();
if (currentBook.getAuthorLname().equals("Smith") ) {
itemStyle = "featured";
}
else {
itemStyle = "default";
}
%>
<LI class="<%=itemStyle %>" > <%= currentBook.getTitle() %> </
LI>
<%
}
%>
</UL>


JSTL Method
<UL>
<c:forEach var="currentBook" items="${list}">
<LI> <c:eek:ut value="${currentBook.bookTitle}"/> </LI>
</c:forEach>
</UL>
>
>
> The question is, how do I incorporate the row formatting from the
> scriptlet example into the JSTL sample?
>
>
> Thanks,

Here's the answer. Note the warnings that are put inline.

<%-- HTML tags are lowercase, NOT UPPERCASE! --%>
<ul>
<c:forEach var="currentBook" items="${list}">
<c:choose>
<%-- it would make more sense to have there be an
isFeatured() method on the bean itself, and use
currentBook.featured}" --%>
<c:when test="${currentBook.authorLname == 'Smith'}">
<c:set var='itemStyle' value='featured' />
</c:when>
<c:eek:therwise>
<c:set var='itemStyle' value='default' />
</c:eek:therwise>
</c:choose>
<%-- Make sure to properly escape values! --%>
<li class="${itemStyle}">${fn:escapeXml(currentBook.bookTitle)}</li>
</c:forEach>
</ul>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,994
Messages
2,570,222
Members
46,810
Latest member
Kassie0918

Latest Threads

Top