View Javadoc

1   package org.apache.velocity.tools.view.jsp;
2   
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  import javax.servlet.http.HttpServletRequest;
23  import javax.servlet.http.HttpServletResponse;
24  import javax.servlet.jsp.PageContext;
25  import org.apache.velocity.app.VelocityEngine;
26  import org.apache.velocity.tools.view.ViewToolContext;
27  
28  /**
29   * <p>Velocity context implementation specific to the JSP environment.</p>
30   *
31   * @author Nathan Bubna
32   * @version $Id: ViewContext.java 514727 2007-03-05 16:49:03Z nbubna $
33   */
34  public class JspToolContext extends ViewToolContext
35  {
36      public static final String PAGE_CONTEXT_KEY = "pageContext";
37  
38      private final PageContext pageContext;
39  
40      public JspToolContext(VelocityEngine velocity,
41                            PageContext pageContext)
42      {
43          super(velocity,
44                (HttpServletRequest)pageContext.getRequest(),
45                (HttpServletResponse)pageContext.getResponse(),
46                pageContext.getServletContext());
47  
48          this.pageContext = pageContext;
49      }
50  
51      protected void putToolProperties()
52      {
53          putToolProperty(PAGE_CONTEXT_KEY, getPageContext());
54  
55          super.putToolProperties();
56      }
57  
58      public PageContext getPageContext()
59      {
60          return this.pageContext;
61      }
62  
63      protected Object getServletApi(String key)
64      {
65          if (key.equals(PAGE_CONTEXT_KEY))
66          {
67              return getPageContext();
68          }
69          return super.getServletApi(key);
70      }
71  
72      public Object getAttribute(String key)
73      {
74          Object o = getPageContext().getAttribute(key);
75          if (o == null)
76          {
77              o = super.getAttribute(key);
78          }
79          return o;
80      }
81  }