1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.struts2.components.template;
19
20 import java.util.Iterator;
21 import java.util.List;
22
23 import javax.servlet.http.HttpServletResponse;
24 import javax.servlet.jsp.PageContext;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.apache.struts2.ServletActionContext;
29 import org.apache.struts2.components.Include;
30 import org.apache.struts2.components.UIBean;
31
32 import com.opensymphony.xwork2.util.ValueStack;
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 templates = template.getPossibleTemplates(this);
51 Exception exception = null;
52 boolean success = false;
53 for (Iterator iterator = templates.iterator(); iterator.hasNext();) {
54 Template t = (Template) iterator.next();
55 try {
56 Include.include(getFinalTemplateName(t), pageContext.getOut(),
57 pageContext.getRequest(), (HttpServletResponse) pageContext.getResponse());
58 success = true;
59 break;
60 } catch (Exception e) {
61 if (exception == null) {
62 exception = e;
63 }
64 }
65 }
66
67 if (!success) {
68 LOG.error("Could not render JSP template " + templateContext.getTemplate());
69
70 if (exception != null) {
71 throw exception;
72 } else {
73 return;
74 }
75 }
76
77 stack.pop();
78 }
79
80 protected String getSuffix() {
81 return "jsp";
82 }
83 }