View Javadoc

1   /*
2    * $Id: JspTemplateEngine.java 451544 2006-09-30 05:38:02Z mrdon $
3    *
4    * Copyright 2006 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
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  }