Reputation: 35790
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:
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.
Upvotes: 2
Views: 4633
Reputation: 105
In response to part 3, I've been able to find a hacky way of allowing tag files to have scripting content. You'll need three files: two jsp files and a tag file.
page.jsp
<%@ taglib tagdir='/WEB-INF/tags' prefix='t' %>
<t:site page="page-content.jsp"></t:site>
site.tag:
<%@ attribute name="page" required="true" %>
<html>
<body>
<jsp:include page="${page}" />
</body>
</html>
page-content.jsp
<!-- Insert all scripts here -->
PS. I've just inherited a super old website that sounds similar to what you've run into. It's probably best to not have any scriptlets, but this might be an okay step towards decoupling a legacy application where that's not an option.
PPS. Make sure that your web.xml declares the servlet version as 2.4+ or the tag file won't interpret ${page}
. See here.
Upvotes: 1
Reputation: 104178
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.
Upvotes: 3
Reputation: 29240
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.
Upvotes: 2