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