views:

51

answers:

1

I am creating a custom JSP tag by extending SimpleTagSupport. Normally in the view, I could do something like <c:out value="${key.attr}" /> using jstl to pull data from the model. My question is: when creating custom tags (by using SimpleTagSupport), how do I gain access to the data from the model? Thanks.

Just to clarify, I need a custom tag to do some formatting on a piece of data in the model. I am open to suggestions if a custom tag is not the best approach for this (but would still love an answer to my original question).

Update: based on Bozhos answers it appears that I can grab the data from the model by using the JSP context. Is this the typical way it is usually done (i.e. when a custom tag is used to manipulate a peice of data in the model before display)? Or is it okay to pass the data as a parameter to the custom tag logic (i.e. via the tag attributes)? I'm just trying to figure out the pros and cons of each method. Thanks.

+1  A: 

All the data that JSTL manipulates is stored in the jsp context attributes. So you can get, for example, the key variable, by calling:

getJspContext().getAttribute("key");

There are four scopes for these attributes - page, request, session and application, so be careful where they are set. There is an overloaded method to provide a specific scope.

Of course, you can define a tag attribute instead and pass it that way.

Bozho
so is this a better way of doing it than passing the data to the custom tag via a parameter?
es11
now, that's a different story. You can, of course, pass it as parameter. But you can access other values from the page context as well.
Bozho