Calling a jsp from within a jsp tag (is madness far away)

D

Duncan

Hi

I have a custom tag, it's called fragment

I have a number of different types of fragments all of which are text
(plain, image url, html) apart from one which is a jsp type

The jsp fragment actually contains the name of the jsp (e.g
dateTime.jsp)

If I have this code in a jsp

<img src="<fragment:fragment placeHolder="headerRight" />"

and the headerRight alias causes the text 'images/banner.jpg' to be
loaded then the banner image is displayed.

if I have the following jsp called dateTime.jsp


<%
Date d = new Date(System.currentTimeMillis());
out.println(d);
%>

then <jsp:include page="dateTime.jsp"/>
works fine (as expected)

What I actually want to be able to do is something like this

<jsp:include page="<fragment:fragment placeHolder="dtFrag"/>"/>

where the placeHolder dtFrag causes the text dateTime.jsp to be loaded
(or would/could/should do if I could get it to work)

This doesn't compile and nothing I do will work.

I think it may have something to do with the fact that some things are
compiled at runtime and others at compile time but as I'm no JSP
expert I can't be sure

Have I completely lost the plot here ?

Is it possible to cause a jsp to be compiled on the fly from withing a
custom tag.?

Do I have any idea what it is I'm trying to do ?

Should I give up and get a job as a worm farmer ?

Cheers
Duncan
 
D

Duncan

Um, this seems to work (in the tag handler)
Is it sheer madness, is there a better way of doing this ?
Do I care as long as it works ?

Cheers
Duncan

public int doStartTag() throws JspException {
try{
FragmentCache fragments = (FragmentCache)pageContext.getServletContext().getAttribute("fragments");
String fragment = fragments.getFragment(placeHolder);
if(null != fragment){
if(fragment.endsWith(".jsp")){
try{
pageContext.include(fragment);
}
catch(ServletException se){
throw new JspException("Failed miserably when trying to include " + placeHolder);
}
}
else{
JspWriter out = pageContext.getOut();
out.write(fragment);
}
}
else{
throw new JspException("Cannot find a fragment for the placeholder " + placeHolder);
}
}
catch(IOException ioex){
throw new JspException(ioex.getMessage());
}
return SKIP_BODY;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top