01: /*
02: * $Id: JspTemplateEngine.java 560894 2007-07-30 09:05:32Z rgielen $
03: *
04: * Licensed to the Apache Software Foundation (ASF) under one
05: * or more contributor license agreements. See the NOTICE file
06: * distributed with this work for additional information
07: * regarding copyright ownership. The ASF licenses this file
08: * to you under the Apache License, Version 2.0 (the
09: * "License"); you may not use this file except in compliance
10: * with the License. You may obtain a copy of the License at
11: *
12: * http://www.apache.org/licenses/LICENSE-2.0
13: *
14: * Unless required by applicable law or agreed to in writing,
15: * software distributed under the License is distributed on an
16: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17: * KIND, either express or implied. See the License for the
18: * specific language governing permissions and limitations
19: * under the License.
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
39: .getLog(JspTemplateEngine.class);
40:
41: public void renderTemplate(TemplateRenderingContext templateContext)
42: throws Exception {
43: Template template = templateContext.getTemplate();
44:
45: if (LOG.isDebugEnabled()) {
46: LOG.debug("Trying to render template " + template
47: + ", 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(
53: ServletActionContext.PAGE_CONTEXT);
54: List<Template> templates = template.getPossibleTemplates(this );
55: Exception exception = null;
56: boolean success = false;
57: for (Template t : templates) {
58: try {
59: Include
60: .include(getFinalTemplateName(t), pageContext
61: .getOut(), pageContext.getRequest(),
62: (HttpServletResponse) pageContext
63: .getResponse());
64: success = true;
65: break;
66: } catch (Exception e) {
67: if (exception == null) {
68: exception = e;
69: }
70: }
71: }
72:
73: if (!success) {
74: LOG.error("Could not render JSP template "
75: + templateContext.getTemplate());
76:
77: if (exception != null) {
78: throw exception;
79: } else {
80: return;
81: }
82: }
83:
84: stack.pop();
85: }
86:
87: protected String getSuffix() {
88: return "jsp";
89: }
90: }
|