Saturday, July 18, 2009

JSP Text Custom Tag

Which is the more proper (i.e. acceptable to Sun, now Oracle's new division)
  • JSP Text Custom Tag, or
  • Text JSP Custom Tag?
The source is at
http://code.google.com/p/synthfuljava/source/browse/#svn/trunk/jsp/org/synthful/jsp/tags/Text.

The TLD is at
http://code.google.com/p/synthfuljava/source/browse/#svn/trunk/jsp/org/resources/WEB-INF.

Anyway, this is a custom tag I wrote because during ancient times I programmed in Perl and when I moved to Java I had to do this Java

String query =
"SELECT NAME, ADDRESS, JOBID " +
"FROM EMPLOYEE E, JOBS J " +
"WHERE E.EID = J.EID AND" +
"E.STATE = '" + state + "' AND " +
"current_date - E.STARTDATE > " + minexp;


instead of, which Perl allows me,

query =
SELECT NAME, ADDRESS, JOBID
FROM EMPLOYEE E, JOBS J
WHERE E.EID = J.EID AND
E.STATE = \'$state\' AND
current_date - E.STARTDATE > $minexp
;



With the Text JSP Custom Tag, I have been doing this

<h2g2j:text id="query" scope="session">
SELECT NAME, ADDRESS, JOBID
FROM EMPLOYEE E, JOBS J
WHERE E.EID = J.EID AND
E.STATE = '<%=state%>' AND
current_date - E.STARTDATE > <%=minExp%>
</h2g2j:text>

  • The contents of the body of the tag is stored in a StringBuffer.
  • The ID="query" attribute associates the StringBuffer to Java variable "query".
  • Specifying the ID attribute clears the StringBuffer before storing the tag's contents into the StringBuffer.
  • Instead of using the ID attribute, the REF attribute should be used to invoke the tag identified by "query" without clearing the StringBuffer.

<h2g2j:text ref="query" scope="session"/>


There isn't actually much you could do reinvoking a text tag, unless you wish to append to the StringBuffer.


<h2g2j:text ref="query" scope="session">
AND J.DESC = '<%=jobDesc%>'
</h2g2j:text>



Which would be equivalent to doing

<h2g2j:text id="query" scope="session">
SELECT NAME, ADDRESS, JOBID
FROM EMPLOYEE E, JOBS J
WHERE E.EID = J.EID AND
E.STATE = '<%=state%>' AND
current_date - E.STARTDATE > <%=minExp%>
AND J.DESC = '<%=jobDesc%>'
</h2g2j:text>


Since the StringBuffer of the tag is associated with the java variable "query", you would invoke the StringBuffer with that variable:

<%
jdbcConnection.submit(query);
%>

No comments:

Post a Comment