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
Duncan - 20 Dec 2005 11:53 GMT
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;
}
>Hi
>
[quoted text clipped - 47 lines]
>Cheers
>Duncan