Question

My applications have previously used jsp 2.0 custom tags extensively. We preferred these over jsp:includes because we didn't have to manually set objects into request scope. However the custom tags have one major limitation for us, which is not being able to invoke them dynamically. In an application of our size, this forces us to use jsp:includes instead.

So we have a lot of code that looks like this:

<c:set var="product" value="${product}" scope="request" />
<jsp:include page="${product.name}.jsp" />
<c:remove var="product" scope="request"/>

Apart from looking ugly its error prone so I wanted to encapsulate this into a tag to get the best of both worlds, a custom tag that invokes a dynamic jsp behind the scenes. I've tried a custom tag called 'include' like this:

<%@ attribute name="name" rtexprvalue="true" required="true" %>
<%@ attribute name="value" rtexprvalue="true" required="true" %>
<%@ attribute name="page" rtexprvalue="true" required="true" type="java.lang.Object" %>

<c:set var="${name}" value="${value}" scope="request" />
<jsp:include page="${page}" />
<c:remove var="${name}" scope="request"/>

But it doesn't work because c:set does not allow expressions in the var tag. How can I make this work? I'd be willing to resort to scriptlets in this tag just because its worth the value for our application.

Ideally though, I would love to be able to write a tag that accepts unlimited number of parameters like so:

<i:include product="${product}" order="${order}" page="${product.name}.jsp"/>

But I'm not sure how to do that. Any advice would be great. By the way this example is just for show, so don't get hung up on it, its just the idea I want to get across.

Was it helpful?

Solution

Well I went with scriptlets for lack of a better implementation:

<%@ attribute name="page" rtexprvalue="true" required="true" %>
<%@ attribute name="name" rtexprvalue="true" required="true" %>
<%@ attribute name="value" rtexprvalue="true" required="true" type="java.lang.Object" %>

<% request.setAttribute(name, value); %>
<jsp:include page="${page}" />
<% request.removeAttribute(name); %>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top