View Javadoc

1   /*
2    * $Id: JspTemplateEngine.java 471756 2006-11-06 15:01:43Z husted $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "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 java.util.Iterator;
24  import java.util.List;
25  
26  import javax.servlet.http.HttpServletResponse;
27  import javax.servlet.jsp.PageContext;
28  
29  import org.apache.commons.logging.Log;
30  import org.apache.commons.logging.LogFactory;
31  import org.apache.struts2.ServletActionContext;
32  import org.apache.struts2.components.Include;
33  import org.apache.struts2.components.UIBean;
34  
35  import com.opensymphony.xwork2.util.ValueStack;
36  
37  /***
38   * JSP based template engine.
39   */
40  public class JspTemplateEngine extends BaseTemplateEngine {
41      private static final Log LOG = LogFactory.getLog(JspTemplateEngine.class);
42  
43      public void renderTemplate(TemplateRenderingContext templateContext) throws Exception {
44          Template template = templateContext.getTemplate();
45  
46          if (LOG.isDebugEnabled()) {
47              LOG.debug("Trying to render template " + template + ", 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(ServletActionContext.PAGE_CONTEXT);
53          List templates = template.getPossibleTemplates(this);
54          Exception exception = null;
55          boolean success = false;
56          for (Iterator iterator = templates.iterator(); iterator.hasNext();) {
57              Template t = (Template) iterator.next();
58              try {
59                  Include.include(getFinalTemplateName(t), pageContext.getOut(),
60                          pageContext.getRequest(), (HttpServletResponse) pageContext.getResponse());
61                  success = true;
62                  break;
63              } catch (Exception e) {
64                  if (exception == null) {
65                      exception = e;
66                  }
67              }
68          }
69  
70          if (!success) {
71              LOG.error("Could not render JSP template " + templateContext.getTemplate());
72  
73              if (exception != null) {
74                  throw exception;
75              } else {
76                  return;
77              }
78          }
79  
80          stack.pop();
81      }
82  
83      protected String getSuffix() {
84          return "jsp";
85      }
86  }