01: /*
02: * Copyright (c) 2002-2006 by OpenSymphony
03: * All rights reserved.
04: */
05: package com.opensymphony.webwork.components.template;
06:
07: import com.opensymphony.webwork.ServletActionContext;
08: import com.opensymphony.webwork.components.Include;
09: import com.opensymphony.webwork.components.UIBean;
10: import com.opensymphony.xwork.util.OgnlValueStack;
11: import org.apache.commons.logging.Log;
12: import org.apache.commons.logging.LogFactory;
13:
14: import javax.servlet.http.HttpServletResponse;
15: import javax.servlet.jsp.PageContext;
16: import java.util.Iterator;
17: import java.util.List;
18:
19: /**
20: * JSP based template engine.
21: *
22: * @author jcarreira
23: */
24: public class JspTemplateEngine extends BaseTemplateEngine {
25: private static final Log LOG = LogFactory
26: .getLog(JspTemplateEngine.class);
27:
28: public void renderTemplate(TemplateRenderingContext templateContext)
29: throws Exception {
30: Template template = templateContext.getTemplate();
31:
32: if (LOG.isDebugEnabled()) {
33: LOG.debug("Trying to render template " + template
34: + ", repeating through parents until we succeed");
35: }
36: UIBean tag = templateContext.getTag();
37: OgnlValueStack stack = templateContext.getStack();
38: stack.push(tag);
39: PageContext pageContext = (PageContext) stack.getContext().get(
40: ServletActionContext.PAGE_CONTEXT);
41: List templates = template.getPossibleTemplates(this );
42: Exception exception = null;
43: boolean success = false;
44: for (Iterator iterator = templates.iterator(); iterator
45: .hasNext();) {
46: Template t = (Template) iterator.next();
47: try {
48: Include
49: .include(getFinalTemplateName(t), pageContext
50: .getOut(), pageContext.getRequest(),
51: (HttpServletResponse) pageContext
52: .getResponse());
53: success = true;
54: break;
55: } catch (Exception e) {
56: if (exception == null) {
57: exception = e;
58: }
59: }
60: }
61:
62: if (!success) {
63: LOG.error("Could not render JSP template "
64: + templateContext.getTemplate());
65:
66: if (exception != null) {
67: throw exception;
68: } else {
69: return;
70: }
71: }
72:
73: stack.pop();
74: }
75:
76: protected String getSuffix() {
77: return "jsp";
78: }
79: }
|