Tuesday, February 07, 2006

Printing server-side JSP scriptlet code to output HTML conditionally from a custom JSP tag

Background:
The Java Web Application (WAR) that I am developing uses a content management system (Fatwire CMS) that executes all JSP code during the publish process and spits the resultant HTML out into static files in the published output WAR.

Problem Description
I needed to be able to conditionally either execute JSP code or write the JSP to the output.

Problem Solution:
The JSP bodycontent attribute of the tag config in the web.xml can be set to either 'JSP' or 'tagdependent' (as per this article) ie:

<bodycontent>tagdependent</bodycontent>


When bodycontent is set to 'JSP' in the web.xml, the following JSP code:
<%= "a" + "b" %>

outputs to the result stream:
ab

Wraps any 'dynamic' server side processing JSP code that is to
be deployed to the application server without being parsed by
the Fatwire publication process.

Also, allows evaluation of the code in preview mode of Fatwire.

however, when bodycontent is set to 'tagdependent' in the web.xml, the same JSP code outputs:
<%= "a" + "b" %>

The question now is how to conditionally determine whether to set bodycontent to 'JSP' or 'tagdependent'

Resolution: Was unable to find a JSP API call that dynamically modified the bodycontent property of the custom tag. As a work-around, implemented a custom version of the Fatwire tag render:sattelitepage (containing if/else logic that either parsed the page in preview mode using ics.ReadPage() or otherwise printed out the include contents if in publish mode)

Note: bodycontent should be set to 'tagdependent' not 'tagdependant' (that typo in the web.xml caused this error for me: jasper.error.bad.bodycontent.type)
Using a custom JSP tag, the conditional
saaj.jar fr

0 Comments:

Post a Comment

<< Home