View Javadoc

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