View Javadoc

1   /*
2    * $Id: StrutsSpringObjectFactory.java 439747 2006-09-03 09:22:46Z mrdon $
3    *
4    * Copyright 2006 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.struts2.tiles;
19  
20  import org.apache.tiles.TilesUtilImpl;
21  import org.apache.struts2.views.freemarker.FreemarkerResult;
22  import org.apache.struts2.ServletActionContext;
23  
24  import javax.servlet.jsp.PageContext;
25  import javax.servlet.ServletException;
26  import javax.servlet.http.HttpServletRequest;
27  import java.io.IOException;
28  
29  import com.opensymphony.xwork2.ActionInvocation;
30  import freemarker.template.TemplateException;
31  
32  /***
33   *
34   * Default implementation of TilesUtil.
35   * This class contains default implementation of utilities. This implementation
36   * is intended to be used without Struts.
37   *
38   * TilesUtilImpl implementation used to intercept .ftl requests and
39   * ensure that they are setup properly to take advantage of the
40   * {@link FreemarkerResult}.
41   *
42   * @version $Id$
43   *
44   */
45  public class StrutsTilesUtilImpl extends TilesUtilImpl {
46  
47      /***
48       * The mask used to detect requests which should be intercepted.
49       */
50      private String mask;
51  
52      /***
53       * Default constructor.
54       * Sets the mask to '.ftl'
55       */
56      public StrutsTilesUtilImpl() {
57          mask = ".ftl";
58      }
59  
60      /***
61       * Optional constructor used to specify a specific mask.
62       * @param mask
63       */
64      public StrutsTilesUtilImpl(String mask) {
65          this.mask = mask;
66      }
67  
68      /***
69       * Enhancement of the default include which allows for freemarker
70       * templates to be intercepted so that the FreemarkerResult can
71       * be used in order to setup the appropriate model.
72       *
73       * @param string the included resource
74       * @param pageContext the current page context
75       * @param b whether or not a flush should occur
76       * @throws IOException
77       * @throws ServletException
78       * @throws Exception 
79       */
80      public void doInclude(String string, PageContext pageContext, boolean b) throws Exception {
81          if(string.endsWith(".ftl")) {
82              HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
83              ActionInvocation invocation = ServletActionContext.getActionContext(request).getActionInvocation();
84              FreemarkerResult result = new FreemarkerResult();
85              try {
86                  result.doExecute(string, invocation);
87              } catch (TemplateException e) {
88                  log.error("Error invoking Freemarker template", e);
89                  throw new ServletException("Error invoking Freemarker template.", e);
90              }
91          }
92          else {
93              super.doInclude(string, pageContext, b);
94          }
95      }
96  }