KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > servlet > tags > imports > Import


1 package jodd.servlet.tags.imports;
2
3 import java.io.IOException;
4 import java.util.ArrayList;
5
6 import javax.servlet.RequestDispatcher;
7 import javax.servlet.ServletException;
8 import javax.servlet.http.HttpServletRequest;
9 import javax.servlet.jsp.tagext.TagSupport;
10
11 /**
12  * Includes local resource into the current page and organize templates. It
13  * can be used for including local content into the current page.<p>
14  *
15  * Import tag can be used for managing web templates, macros or tiles,
16  * however it is called. There are 2 different types of usage:
17  *
18  * <ol>
19  *
20  * <li>Without template page. In this case every must have import for each
21  * part of the page that will be loaded. When it is repeated on many pages,
22  * this may become hard to maintain.
23  * </li>
24  *
25  * <li>With templates. In this case, template is specified in an external jsp
26  * file that is imported in every page that will use that template. By using
27  * import parameters it is possible to manage included content.</li>
28  *
29  * </ol>
30  *
31  * Import tag is about 2x faster then default jsp:include (tomcat v5.)
32  *
33  */

34 public class Import extends TagSupport {
35
36     private static String SERVLET_PATH = "jodd.servlet.tags.include.servlet_path";
37
38     private boolean isPageRelative;
39
40     private String page = "";
41
42     public void setPage(String v) {
43         page = v;
44         isPageRelative = (page.startsWith("/") == false);
45     }
46     protected String getPage() {
47         return page;
48     }
49
50     private ArrayList attrs;
51     public void addAttributeName(String s) {
52         attrs.add(s);
53     }
54
55     /**
56      * Evaluate tag content.
57      *
58      * @return EVAL_BODY_INCLUDE
59      */

60     public int doStartTag() {
61         attrs = new ArrayList();
62         return EVAL_BODY_AGAIN;
63     }
64
65
66     /**
67      * Finds and renders imported resource. A resource (page) can be specified
68      * relatively (when it doesn't start with a slash) or absolutely from the
69      * application root (when it starts with the slash). <p>
70      *
71      * This method will be called after tag process any existing param
72      * parameters.
73      *
74      * @return EVAL_PAGE
75      */

76     public int doEndTag() {
77         HttpServletRequest request = (HttpServletRequest)pageContext.getRequest();
78         try {
79             RequestDispatcher rd = null;
80             boolean first = false; // indicates topmost servlet
81
String sp = "";
82             String targetUrl = new String(page);
83             
84             if (isPageRelative == true) {
85                 sp = (String) request.getAttribute(SERVLET_PATH);
86                 if (sp == null) {
87                     first = true;
88                     sp = request.getServletPath();
89                     sp = sp.substring(0, sp.lastIndexOf('/'));
90                 }
91                 targetUrl = sp + '/' + page;
92             }
93             
94             rd = request.getRequestDispatcher(targetUrl);
95             ImportResponseWrapper irw = new ImportResponseWrapper(pageContext.getResponse(), pageContext.getOut());
96
97             String previous_sp = sp;
98             sp = targetUrl.substring(0, targetUrl.lastIndexOf('/'));
99             request.setAttribute(SERVLET_PATH, sp);
100             rd.include(request, irw);
101             if (first == true) {
102                 request.removeAttribute(SERVLET_PATH);
103             } else {
104                 request.setAttribute(SERVLET_PATH, previous_sp);
105             }
106         } catch (IOException ioe) {
107         } catch (ServletException sex) {
108         }
109
110         // end of the page, remove not persistent params....
111
for (int i = 0; i < attrs.size(); i++) {
112             request.removeAttribute((String)attrs.get(i));
113         }
114         return EVAL_PAGE;
115     }
116 }
117
Popular Tags