1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.apache.struts2.components.template;
23
24 import java.util.List;
25
26 import javax.servlet.http.HttpServletResponse;
27 import javax.servlet.jsp.PageContext;
28
29 import org.apache.struts2.ServletActionContext;
30 import org.apache.struts2.components.Include;
31 import org.apache.struts2.components.UIBean;
32
33 import com.opensymphony.xwork2.util.ValueStack;
34 import com.opensymphony.xwork2.util.logging.Logger;
35 import com.opensymphony.xwork2.util.logging.LoggerFactory;
36
37 /***
38 * JSP based template engine.
39 */
40 public class JspTemplateEngine extends BaseTemplateEngine {
41 private static final Logger LOG = LoggerFactory.getLogger(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<Template> templates = template.getPossibleTemplates(this);
54 Exception exception = null;
55 boolean success = false;
56 for (Template t : templates) {
57 try {
58 Include.include(getFinalTemplateName(t), pageContext.getOut(),
59 pageContext.getRequest(), (HttpServletResponse) pageContext.getResponse());
60 success = true;
61 break;
62 } catch (Exception e) {
63 if (exception == null) {
64 exception = e;
65 }
66 }
67 }
68
69 if (!success) {
70 LOG.error("Could not render JSP template " + templateContext.getTemplate());
71
72 if (exception != null) {
73 throw exception;
74 } else {
75 return;
76 }
77 }
78
79 stack.pop();
80 }
81
82 protected String getSuffix() {
83 return "jsp";
84 }
85 }