Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2005

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Duncan - 20 Dec 2005 11:08 GMT
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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.