View Javadoc

1   /*
2    * $Id: JspTemplateEngine.java 651946 2008-04-27 13:41:38Z apetrelli $
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  
22  package org.apache.struts2.components.template;
23  
24  import java.util.List;
25  
26  import javax.servlet.http.HttpServletResponse;
27  import javax.servlet.jsp.PageContext;
28  
29  import org.apache.struts2.ServletActionContext;
30  import org.apache.struts2.components.Include;
31  import org.apache.struts2.components.UIBean;
32  
33  import com.opensymphony.xwork2.util.ValueStack;
34  import com.opensymphony.xwork2.util.logging.Logger;
35  import com.opensymphony.xwork2.util.logging.LoggerFactory;
36  
37  /***
38   * JSP based template engine.
39   */
40  public class JspTemplateEngine extends BaseTemplateEngine {
41      private static final Logger LOG = LoggerFactory.getLogger(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<Template> templates = template.getPossibleTemplates(this);
54          Exception exception = null;
55          boolean success = false;
56          for (Template t : templates) {
57              try {
58                  Include.include(getFinalTemplateName(t), pageContext.getOut(),
59                          pageContext.getRequest(), (HttpServletResponse) pageContext.getResponse());
60                  success = true;
61                  break;
62              } catch (Exception e) {
63                  if (exception == null) {
64                      exception = e;
65                  }
66              }
67          }
68  
69          if (!success) {
70              LOG.error("Could not render JSP template " + templateContext.getTemplate());
71  
72              if (exception != null) {
73                  throw exception;
74              } else {
75                  return;
76              }
77          }
78  
79          stack.pop();
80      }
81  
82      protected String getSuffix() {
83          return "jsp";
84      }
85  }