Sunday, December 11, 2005

Passing Values Out from a JSP 2.0 .tag file

This is really poorly documented on the web, so here goes:

There seem to be two ways to get data OUT of a tag.

Method #1:

Use the request scope. Inside the .tag file, try this:


<%

String led = "zeppelin";

request.setAttribute("led", led);

%>


If you need to vary the name of the request Attribute, pass it in to the .tag file in an attribute then use jspContext.getAttribute(nameOfYourTagAttributeHere) to grab it into a String.

Anyhow, now you can access it in the calling JSP using EL.


<c:out value="${led}"/>


Method #2:

I've only read about this one, haven't tried it, but here goes:

A tag receives input through its attributes. In addition to the output text produced by the tag file, a tag can also "output" data by creating EL variables. A tag file might return a value to the page that called it as follows:

<%@ variable name-given="filesincluded" scope="AT_END" %>

The "name-given" provides the name of the variable to set in the page after the tag has completed. "AT_END" specifies that the variable is set when the tag file completes.

No comments:

Labels

Blog Archive

Contributors