1 /* 2 * Copyright 2007-2012 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.springbyexample.web.servlet.view.tiles2; 18 19 import java.util.Map; 20 21 import javax.servlet.ServletContext; 22 import javax.servlet.ServletException; 23 import javax.servlet.http.HttpServletRequest; 24 import javax.servlet.http.HttpServletResponse; 25 26 import org.apache.tiles.TilesContainer; 27 import org.apache.tiles.access.TilesAccess; 28 import org.springframework.web.servlet.view.AbstractUrlBasedView; 29 30 /** 31 * <p>View implementation that first checks if the request is 32 * a Tiles definition, then if it isn't tries to retrieve a default Tiles 33 * template and uses the url to dynamically set the body.</p> 34 * 35 * <p>The main template name defaults to 'mainTemplate', the 36 * body attribute defaults to to 'content', and the delimiter is '.' 37 * which is used for replacing a '/' in the url.</p> 38 * 39 * <p>If a request is made for '/info/index.html', Spring will pass 'info/index' into the view. 40 * The first thing will be to look for 'info/index' as a Tiles definition. 41 * Then a template definition of '.info.mainTemplate', which if found will dynamically have a 42 * body set on this definition. If the previous aren't found, it is assumed a root definition 43 * exists. This would be '.mainTemplate'. If none of these exist, a TilesException will be thrown.</p> 44 * 45 * <ol> 46 * <li>Check if a Tiles definition based on the URL matches.</li> 47 * <li>Checks if a definition derived from the URL and default template name exists and then dynamically insert the body based on the URL.</li> 48 * <li>Check if there is a root template definition and then dynamically insert the body based on the URL.</li> 49 * <li>If no match is found from any process above a TilesException is thrown.</li> 50 * </ol> 51 * 52 * @author David Winterfeldt 53 */ 54 public class DynamicTilesView extends AbstractUrlBasedView { 55 56 final DynamicTilesViewProcessor dynamicTilesViewProcessor = new DynamicTilesViewProcessor(); 57 58 /** 59 * Main template name. The default is 'mainTemplate'. 60 * 61 * @param tilesDefinitionName Main template name used to lookup definitions. 62 */ 63 public void setTilesDefinitionName(String tilesDefinitionName) { 64 dynamicTilesViewProcessor.setTilesDefinitionName(tilesDefinitionName); 65 } 66 67 /** 68 * Tiles body attribute name. The default is 'body'. 69 * 70 * @param tilesBodyAttributeName Tiles body attribute name. 71 */ 72 public void setTilesBodyAttributeName(String tilesBodyAttributeName) { 73 dynamicTilesViewProcessor.setTilesBodyAttributeName(tilesBodyAttributeName); 74 } 75 76 /** 77 * Sets Tiles definition delimiter. For example, instead of using 78 * the request 'info/about' to lookup the template definition 79 * 'info/mainTemplate', the default delimiter of '.' 80 * would look for '.info.mainTemplate' 81 * 82 * @param tilesDefinitionDelimiter Optional delimiter to replace '/' in a url. 83 */ 84 public void setTilesDefinitionDelimiter(String tilesDefinitionDelimiter) { 85 dynamicTilesViewProcessor.setTilesDefinitionDelimiter(tilesDefinitionDelimiter); 86 } 87 88 /** 89 * Renders output using Tiles. 90 */ 91 protected void renderMergedOutputModel(Map model, 92 HttpServletRequest request, HttpServletResponse response) 93 throws Exception { 94 95 ServletContext servletContext = getServletContext(); 96 TilesContainer container = TilesAccess.getContainer(servletContext); 97 if (container == null) { 98 throw new ServletException("Tiles container is not initialized. " + 99 "Have you added a TilesConfigurer to your web application context?"); 100 } 101 102 exposeModelAsRequestAttributes(model, request); 103 104 dynamicTilesViewProcessor.renderMergedOutputModel(getBeanName(), getUrl(), 105 servletContext, request, response, container); 106 } 107 108 }