View Javadoc

1   /*
2    * $Id: JspTemplateEngine.java 560894 2007-07-30 09:05:32Z rgielen $
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 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.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<Template> templates = template.getPossibleTemplates(this);
51          Exception exception = null;
52          boolean success = false;
53          for (Template t : templates) {
54              try {
55                  Include.include(getFinalTemplateName(t), pageContext.getOut(),
56                          pageContext.getRequest(), (HttpServletResponse) pageContext.getResponse());
57                  success = true;
58                  break;
59              } catch (Exception e) {
60                  if (exception == null) {
61                      exception = e;
62                  }
63              }
64          }
65  
66          if (!success) {
67              LOG.error("Could not render JSP template " + templateContext.getTemplate());
68  
69              if (exception != null) {
70                  throw exception;
71              } else {
72                  return;
73              }
74          }
75  
76          stack.pop();
77      }
78  
79      protected String getSuffix() {
80          return "jsp";
81      }
82  }