View Javadoc

1   /*
2    * $Id: VelocityTemplateEngine.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.io.IOException;
25  import java.io.Writer;
26  import java.util.List;
27  import java.util.Map;
28  
29  import javax.servlet.ServletContext;
30  import javax.servlet.http.HttpServletRequest;
31  import javax.servlet.http.HttpServletResponse;
32  
33  import org.apache.struts2.ServletActionContext;
34  import org.apache.struts2.views.velocity.VelocityManager;
35  import org.apache.velocity.app.VelocityEngine;
36  import org.apache.velocity.context.Context;
37  
38  import com.opensymphony.xwork2.inject.Inject;
39  import com.opensymphony.xwork2.util.logging.Logger;
40  import com.opensymphony.xwork2.util.logging.LoggerFactory;
41  
42  /***
43   * Velocity based template engine.
44   */
45  public class VelocityTemplateEngine extends BaseTemplateEngine {
46      private static final Logger LOG = LoggerFactory.getLogger(VelocityTemplateEngine.class);
47      
48      private VelocityManager velocityManager;
49      
50      @Inject
51      public void setVelocityManager(VelocityManager mgr) {
52          this.velocityManager = mgr;
53      }
54  
55      public void renderTemplate(TemplateRenderingContext templateContext) throws Exception {
56          // get the various items required from the stack
57          Map actionContext = templateContext.getStack().getContext();
58          ServletContext servletContext = (ServletContext) actionContext.get(ServletActionContext.SERVLET_CONTEXT);
59          HttpServletRequest req = (HttpServletRequest) actionContext.get(ServletActionContext.HTTP_REQUEST);
60          HttpServletResponse res = (HttpServletResponse) actionContext.get(ServletActionContext.HTTP_RESPONSE);
61  
62          // prepare velocity
63          velocityManager.init(servletContext);
64          VelocityEngine velocityEngine = velocityManager.getVelocityEngine();
65  
66          // get the list of templates we can use
67          List<Template> templates = templateContext.getTemplate().getPossibleTemplates(this);
68  
69          // find the right template
70          org.apache.velocity.Template template = null;
71          String templateName = null;
72          Exception exception = null;
73          for (Template t : templates) {
74              templateName = getFinalTemplateName(t);
75              try {
76                  // try to load, and if it works, stop at the first one
77                  template = velocityEngine.getTemplate(templateName);
78                  break;
79              } catch (IOException e) {
80                  if (exception == null) {
81                      exception = e;
82                  }
83              }
84          }
85  
86          if (template == null) {
87              LOG.error("Could not load template " + templateContext.getTemplate());
88              if (exception != null) {
89                  throw exception;
90              } else {
91                  return;
92              }
93          }
94  
95          if (LOG.isDebugEnabled()) {
96              LOG.debug("Rendering template " + templateName);
97          }
98  
99          Context context = velocityManager.createContext(templateContext.getStack(), req, res);
100 
101         Writer outputWriter = templateContext.getWriter();
102         context.put("tag", templateContext.getTag());
103         context.put("parameters", templateContext.getParameters());
104 
105         template.merge(context, outputWriter);
106     }
107 
108     protected String getSuffix() {
109         return "vm";
110     }
111 }