Tuesday, February 27, 2007

Portlet Resources using clean JSTL

pencil icon, that"s clickable to start editing the post

Since I'm a great fan of XML I prefer to write my JSP pages as XML, since it guarantees wellformed markup and makes it easier to find errors in JSP's at development time (ant task to check for wellformedness) and naturally all the normal advantages of scriptlet free pages. This is also valid for my JSP's for my portlets and JSTL once more proved value to me when I had to reference a static resource (and image). In the test portlet application that comes with WSRP4J it's written as:

<IMG SRC=<%=renderResponse.encodeURL(renderRequest.getContextPath() + "/images/project-logo.jpg")%>" align="TOP" />

Adhering to best practice with URLencoding, this can be achived with JSTL:

<c:url var="imageUrl" context="${renderRequest.contextPath}" value="/images/project-logo.jpg" />
<img src="${imageUrl}" />

Easy and clean just as it should be. The only improvement I can think of would be to ectend the portlet taglib to include an image tag that would do it automatically in one simpel tag, like:

<portlet:img src="/images/project-logo.jpg" />

much like Struts and Spring.