KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > webwork > components > template > JspTemplateEngine


1 package com.opensymphony.webwork.components.template;
2
3 import com.opensymphony.webwork.ServletActionContext;
4 import com.opensymphony.webwork.components.Include;
5 import com.opensymphony.webwork.components.UIBean;
6 import com.opensymphony.xwork.util.OgnlValueStack;
7 import org.apache.commons.logging.Log;
8 import org.apache.commons.logging.LogFactory;
9
10 import javax.servlet.http.HttpServletResponse JavaDoc;
11 import javax.servlet.jsp.PageContext JavaDoc;
12 import java.util.Iterator JavaDoc;
13 import java.util.List JavaDoc;
14
15 /**
16  * JspTemplateEngine
17  * Date: Sep 28, 2004 12:01:59 PM
18  *
19  * @author jcarreira
20  */

21 public class JspTemplateEngine extends BaseTemplateEngine {
22     private static final Log LOG = LogFactory.getLog(JspTemplateEngine.class);
23
24     public void renderTemplate(TemplateRenderingContext templateContext) throws Exception JavaDoc {
25         Template template = templateContext.getTemplate();
26
27         if (LOG.isDebugEnabled()) {
28             LOG.debug("Trying to render template " + template + ", repeating through parents until we succeed");
29         }
30         UIBean tag = templateContext.getTag();
31         OgnlValueStack stack = templateContext.getStack();
32         stack.push(tag);
33         PageContext JavaDoc pageContext = (PageContext JavaDoc) stack.getContext().get(ServletActionContext.PAGE_CONTEXT);
34         List JavaDoc templates = template.getPossibleTemplates(this);
35         Exception JavaDoc exception = null;
36         boolean success = false;
37         for (Iterator JavaDoc iterator = templates.iterator(); iterator.hasNext();) {
38             Template t = (Template) iterator.next();
39             try {
40                 Include.include(getFinalTemplateName(t), pageContext.getOut(),
41                         pageContext.getRequest(), (HttpServletResponse JavaDoc) pageContext.getResponse());
42                 success = true;
43                 break;
44             } catch (Exception JavaDoc e) {
45                 if (exception == null) {
46                     exception = e;
47                 }
48             }
49         }
50
51         if (!success) {
52             LOG.error("Could not render JSP template " + templateContext.getTemplate());
53
54             if (exception != null) {
55                 throw exception;
56             } else {
57                 return;
58             }
59         }
60
61         stack.pop();
62     }
63
64     protected String JavaDoc getSuffix() {
65         return "jsp";
66     }
67 }
68
Popular Tags