tags:

views:

834

answers:

2

The "new" JSP 2.0 tag file tags are incredibly useful, and for most cases (at least that I've seen) they provide a far more readable format than a Java class. However, while tag files can do almost everything a Java class tag can do, there is one giant thing they can't do: have "scripting" body content (ie. "<%" stuff between the start and end tags).

So, my question is multi-part:

  1. Does anyone know why (from a design standpoint) this is? Is it inherently impossible for tag files to have such content due to the way they are implemented or something?
  2. Does anyone know if there are any future plans to allow for body-content="JSP" in tag files (in a future version of the JSP standard maybe)?
  3. Does anyone know of any way to "hack" tag files to allow them to have scripting content (like by having a body-content of scriptless, and then processing the contents somehow)?

Basically my dream is to someday write tag files which don't result in exceptions if someone puts a

<%= myVar %>

inside, and I'm just trying to determine whether that dream is hopeless or not.

+1  A: 

They can, if you're using old style tags, i.e. derived from BodyTag. SimpleTag implementations can't however.

Because JSP fragments do not support scriptlets, the of a SimpleTag cannot be "JSP"

Personally I'd avoid using Java to write tags at all if you can, and just use the new .tag style. You can incorporate Java code into such tag files, though in general if you are forced to do that you're probably doing something wrong.

Jherico
"They can, if you're using old style tags"Heh, I know; that's why I know about body-content JSP at all :-) But I'd like to achieve similar functionality using tag files; as you noted they are a much better mechanism for creating tags than Java classes.As for "doing something wrong" because I have JSP content in my JSPs ... I don't follow. ELScript and JSTL can do some stuff, but not everything. Moreover, I'm stuck with tons of legacy pages that use scripting, so even if I could re-do everything without scripting bits ... I can't, because it would take way too long.
machineghost
+1  A: 

The JSP tags were designed this way so that there will be a complete separation of concerns between the presentation layer and the code behind. JSP pages are supposed to be edited by designers and therefore using Java code isn't appropriate.

I suppose that you need to adapt your coding habits in this philosophy. You can move all your Java code to your beans. Do whatever is required there and then pass the appropriate bean to the JSP page. Exception handling should also go there. If this isn't enough, you can always create your own tags to use.

kgiannakakis