KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > portal > core > servlet > jsp > taglib > IncludeTag


1 /*****************************************
2  * *
3  * JBoss Portal: The OpenSource Portal *
4  * *
5  * Distributable under LGPL license. *
6  * See terms of license at gnu.org. *
7  * *
8  *****************************************/

9 package org.jboss.portal.core.servlet.jsp.taglib;
10
11 import java.util.LinkedList JavaDoc;
12
13 import javax.servlet.jsp.JspException JavaDoc;
14 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
15
16 import org.jboss.portal.common.context.Context;
17 import org.jboss.portal.common.context.NamedContext;
18 import org.jboss.portal.core.servlet.jsp.PortalJsp;
19
20 /**
21  * Inclusion tag. Used to include a JSP page to another page.
22  * @author <a HREF="theute@jboss.org">Thomas Heute</a>
23  * $Revision: 1.1 $
24  */

25 public class IncludeTag
26       extends TagSupport JavaDoc
27 {
28
29    /**
30     * page attribute to the tag
31     */

32    private String JavaDoc page;
33    
34    /**
35     * Stack of context before changing it for the inclusion
36     */

37    private LinkedList JavaDoc formerContextStack;
38    
39    private Context formerContext;
40    
41    /**
42     * Set the page attribute.
43     * THe filename can be relative or absolute (if starts with a /)
44     * @param filename
45     */

46    public void setPage(String JavaDoc filename) {
47       this.page = filename;
48    }
49    
50    public int doStartTag() throws JspException JavaDoc {
51       
52       // Save the former context stack
53
formerContextStack = (LinkedList JavaDoc)PortalJsp.contextStack.get();
54       formerContext = (Context)pageContext.getRequest().getAttribute(PortalJsp.CTX_REQUEST);
55
56       LinkedList JavaDoc list = (LinkedList JavaDoc)PortalJsp.contextStack.get();
57       LinkedList JavaDoc stack = new LinkedList JavaDoc();
58       if (!list.isEmpty()) {
59          // Change the context attribute to the new context
60
NamedContext ctx = (NamedContext)(list).getLast();
61          pageContext.getRequest().setAttribute(PortalJsp.CTX_REQUEST, ctx.getContext());
62
63          // Change the context stack to the new context
64
stack.addLast(new NamedContext("", ctx.getContext()));
65       }
66       
67       // Include the JSP page
68
try
69       {
70          pageContext.include(page);
71          PortalJsp.contextStack.set(stack);
72       } catch (Exception JavaDoc e) {
73          PortalJsp.log.error("Cannot include page: " + page, e);
74       } finally {
75          // Put back to the original state
76
PortalJsp.contextStack.set(formerContextStack);
77          pageContext.getRequest().setAttribute(PortalJsp.CTX_REQUEST, formerContext);
78       }
79       return SKIP_BODY;
80    }
81
82 }
83
Popular Tags