KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > servlet > view > tiles > TilesView


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.web.servlet.view.tiles;
18
19 import javax.servlet.ServletException JavaDoc;
20 import javax.servlet.http.HttpServletRequest JavaDoc;
21 import javax.servlet.http.HttpServletResponse JavaDoc;
22
23 import org.apache.struts.tiles.ComponentContext;
24 import org.apache.struts.tiles.ComponentDefinition;
25 import org.apache.struts.tiles.Controller;
26 import org.apache.struts.tiles.DefinitionsFactory;
27 import org.apache.struts.tiles.TilesUtilImpl;
28
29 import org.springframework.context.ApplicationContextException;
30 import org.springframework.web.servlet.view.InternalResourceView;
31
32 /**
33  * View implementation that retrieves a Tiles definition.
34  * The "url" property is interpreted as name of a Tiles definition.
35  *
36  * <p>TilesJstlView with JSTL support is a separate class,
37  * mainly to avoid JSTL dependencies in this class.
38  *
39  * <p>Depends on a Tiles DefinitionsFactory which must be available
40  * in the ServletContext. This factory is typically set up via a
41  * TilesConfigurer bean definition in the application context.
42  *
43  * <p>A component controller specified in the Tiles definition will receive
44  * a reference to the current Spring ApplicationContext if it implements
45  * ApplicationContextAware. The ComponentControllerSupport class provides
46  * a convenient base class for such Spring-aware component controllers.
47  *
48  * @author Alef Arendsen
49  * @author Juergen Hoeller
50  * @see #setUrl
51  * @see TilesJstlView
52  * @see TilesConfigurer
53  * @see ComponentControllerSupport
54  * @see org.springframework.context.ApplicationContextAware
55  */

56 public class TilesView extends InternalResourceView {
57
58     /**
59      * Name of the attribute that will override the path of the layout page
60      * to render. A Tiles component controller can set such an attribute
61      * to dynamically switch the look and feel of a Tiles page.
62      * @see #setPath
63      */

64     public static final String JavaDoc PATH_ATTRIBUTE = TilesView.class.getName() + ".PATH";
65
66     /**
67      * Set the path of the layout page to render.
68      * @param request current HTTP request
69      * @param path the path of the layout page
70      * @see #PATH_ATTRIBUTE
71      */

72     public static void setPath(HttpServletRequest JavaDoc request, String JavaDoc path) {
73         request.setAttribute(PATH_ATTRIBUTE, path);
74     }
75
76
77     private DefinitionsFactory definitionsFactory;
78
79     protected void initApplicationContext() throws ApplicationContextException {
80         super.initApplicationContext();
81
82         // get definitions factory
83
this.definitionsFactory =
84                 (DefinitionsFactory) getServletContext().getAttribute(TilesUtilImpl.DEFINITIONS_FACTORY);
85         if (this.definitionsFactory == null) {
86             throw new ApplicationContextException("Tiles definitions factory not found: TilesConfigurer not defined?");
87         }
88     }
89
90     /**
91      * Prepare for rendering the Tiles definition: Execute the associated
92      * component controller if any, and determine the request dispatcher path.
93      */

94     protected String JavaDoc prepareForRendering(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
95             throws Exception JavaDoc {
96
97         // get component definition
98
ComponentDefinition definition = getComponentDefinition(this.definitionsFactory, request);
99         if (definition == null) {
100             throw new ServletException JavaDoc("No Tiles definition found for name '" + getUrl() + "'");
101         }
102
103         // get current component context
104
ComponentContext context = getComponentContext(definition, request);
105
106         // execute component controller associated with definition, if any
107
Controller controller = getController(definition, request);
108         if (controller != null) {
109             if (logger.isDebugEnabled()) {
110                 logger.debug("Executing Tiles controller [" + controller + "]");
111             }
112             executeController(controller, context, request, response);
113         }
114
115         // determine the path of the definition
116
String JavaDoc path = getDispatcherPath(definition, request);
117         if (path == null) {
118             throw new ServletException JavaDoc(
119                     "Could not determine a path for Tiles definition '" + definition.getName() + "'");
120         }
121
122         return path;
123     }
124
125     /**
126      * Determine the Tiles component definition for the given Tiles
127      * definitions factory.
128      * @param factory the Tiles definitions factory
129      * @param request current HTTP request
130      * @return the component definition
131      */

132     protected ComponentDefinition getComponentDefinition(DefinitionsFactory factory, HttpServletRequest JavaDoc request)
133         throws Exception JavaDoc {
134         return factory.getDefinition(getUrl(), request, getServletContext());
135     }
136
137     /**
138      * Determine the Tiles component context for the given Tiles definition.
139      * @param definition the Tiles definition to render
140      * @param request current HTTP request
141      * @return the component context
142      * @throws Exception if preparations failed
143      */

144     protected ComponentContext getComponentContext(ComponentDefinition definition, HttpServletRequest JavaDoc request)
145         throws Exception JavaDoc {
146         ComponentContext context = ComponentContext.getContext(request);
147         if (context == null) {
148             context = new ComponentContext(definition.getAttributes());
149             ComponentContext.setContext(context, request);
150         }
151         else {
152             context.addMissing(definition.getAttributes());
153         }
154         return context;
155     }
156
157     /**
158      * Determine and initialize the Tiles component controller for the
159      * given Tiles definition, if any.
160      * @param definition the Tiles definition to render
161      * @param request current HTTP request
162      * @return the component controller to execute, or <code>null</code> if none
163      * @throws Exception if preparations failed
164      */

165     protected Controller getController(ComponentDefinition definition, HttpServletRequest JavaDoc request)
166             throws Exception JavaDoc {
167         return definition.getOrCreateController();
168     }
169
170     /**
171      * Execute the given Tiles controller.
172      * @param controller the component controller to execute
173      * @param context the component context
174      * @param request current HTTP request
175      * @param response current HTTP response
176      * @throws Exception if controller execution failed
177      */

178     protected void executeController(
179             Controller controller, ComponentContext context, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
180         throws Exception JavaDoc {
181         controller.perform(context, request, response, getServletContext());
182     }
183
184     /**
185      * Determine the dispatcher path for the given Tiles definition,
186      * i.e. the request dispatcher path of the layout page.
187      * @param definition the Tiles definition to render
188      * @param request current HTTP request
189      * @return the path of the layout page to render
190      * @throws Exception if preparations failed
191      */

192     protected String JavaDoc getDispatcherPath(ComponentDefinition definition, HttpServletRequest JavaDoc request)
193         throws Exception JavaDoc {
194         Object JavaDoc pathAttr = request.getAttribute(PATH_ATTRIBUTE);
195         return (pathAttr != null ? pathAttr.toString() : definition.getPath());
196     }
197
198 }
199
Popular Tags