1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.struts2.components.template;
22
23 import java.util.Iterator;
24 import java.util.List;
25
26 import javax.servlet.http.HttpServletResponse;
27 import javax.servlet.jsp.PageContext;
28
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.apache.struts2.ServletActionContext;
32 import org.apache.struts2.components.Include;
33 import org.apache.struts2.components.UIBean;
34
35 import com.opensymphony.xwork2.util.ValueStack;
36
37 /***
38 * JSP based template engine.
39 */
40 public class JspTemplateEngine extends BaseTemplateEngine {
41 private static final Log LOG = LogFactory.getLog(JspTemplateEngine.class);
42
43 public void renderTemplate(TemplateRenderingContext templateContext) throws Exception {
44 Template template = templateContext.getTemplate();
45
46 if (LOG.isDebugEnabled()) {
47 LOG.debug("Trying to render template " + template + ", repeating through parents until we succeed");
48 }
49 UIBean tag = templateContext.getTag();
50 ValueStack stack = templateContext.getStack();
51 stack.push(tag);
52 PageContext pageContext = (PageContext) stack.getContext().get(ServletActionContext.PAGE_CONTEXT);
53 List templates = template.getPossibleTemplates(this);
54 Exception exception = null;
55 boolean success = false;
56 for (Iterator iterator = templates.iterator(); iterator.hasNext();) {
57 Template t = (Template) iterator.next();
58 try {
59 Include.include(getFinalTemplateName(t), pageContext.getOut(),
60 pageContext.getRequest(), (HttpServletResponse) pageContext.getResponse());
61 success = true;
62 break;
63 } catch (Exception e) {
64 if (exception == null) {
65 exception = e;
66 }
67 }
68 }
69
70 if (!success) {
71 LOG.error("Could not render JSP template " + templateContext.getTemplate());
72
73 if (exception != null) {
74 throw exception;
75 } else {
76 return;
77 }
78 }
79
80 stack.pop();
81 }
82
83 protected String getSuffix() {
84 return "jsp";
85 }
86 }