001/* 002 * Copyright 2002-2014 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.web.servlet.view.tiles2; 018 019import java.util.Locale; 020import java.util.Map; 021import javax.servlet.ServletContext; 022import javax.servlet.ServletException; 023import javax.servlet.http.HttpServletRequest; 024import javax.servlet.http.HttpServletResponse; 025 026import org.apache.tiles.TilesApplicationContext; 027import org.apache.tiles.TilesContainer; 028import org.apache.tiles.context.TilesRequestContext; 029import org.apache.tiles.impl.BasicTilesContainer; 030import org.apache.tiles.servlet.context.ServletTilesApplicationContext; 031import org.apache.tiles.servlet.context.ServletTilesRequestContext; 032import org.apache.tiles.servlet.context.ServletUtil; 033 034import org.springframework.web.servlet.support.JstlUtils; 035import org.springframework.web.servlet.support.RequestContext; 036import org.springframework.web.servlet.view.AbstractUrlBasedView; 037 038/** 039 * {@link org.springframework.web.servlet.View} implementation that retrieves a 040 * Tiles definition. The "url" property is interpreted as name of a Tiles definition. 041 * 042 * <p>This class builds on Tiles, which requires JSP 2.0. 043 * JSTL support is integrated out of the box due to JSTL's inclusion in JSP 2.0. 044 * <b>Note: Spring 4.0 requires Tiles 2.2.2.</b> 045 * 046 * <p>Depends on a TilesContainer which must be available in 047 * the ServletContext. This container is typically set up via a 048 * {@link TilesConfigurer} bean definition in the application context. 049 * 050 * <p><b>NOTE: Tiles 2 support is deprecated in favor of Tiles 3 and will be removed 051 * as of Spring Framework 5.0.</b>. 052 * 053 * @author Juergen Hoeller 054 * @author Sebastien Deleuze 055 * @since 2.5 056 * @see #setUrl 057 * @see TilesConfigurer 058 * @deprecated as of Spring 4.2, in favor of Tiles 3 059 */ 060@Deprecated 061public class TilesView extends AbstractUrlBasedView { 062 063 private boolean alwaysInclude = false; 064 065 066 /** 067 * Specify whether to always include the view rather than forward to it. 068 * <p>Default is "false". Switch this flag on to enforce the use of a 069 * Servlet include, even if a forward would be possible. 070 * @since 4.1.2 071 * @see TilesViewResolver#setAlwaysInclude 072 */ 073 public void setAlwaysInclude(boolean alwaysInclude) { 074 this.alwaysInclude = alwaysInclude; 075 } 076 077 078 @Override 079 public boolean checkResource(final Locale locale) throws Exception { 080 TilesContainer container = ServletUtil.getContainer(getServletContext()); 081 if (!(container instanceof BasicTilesContainer)) { 082 // Cannot check properly - let's assume it's there. 083 return true; 084 } 085 BasicTilesContainer basicContainer = (BasicTilesContainer) container; 086 TilesApplicationContext appContext = new ServletTilesApplicationContext(getServletContext()); 087 TilesRequestContext requestContext = new ServletTilesRequestContext(appContext, null, null) { 088 @Override 089 public Locale getRequestLocale() { 090 return locale; 091 } 092 }; 093 return (basicContainer.getDefinitionsFactory().getDefinition(getUrl(), requestContext) != null); 094 } 095 096 @Override 097 protected void renderMergedOutputModel( 098 Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception { 099 100 ServletContext servletContext = getServletContext(); 101 TilesContainer container = ServletUtil.getContainer(servletContext); 102 if (container == null) { 103 throw new ServletException("Tiles container is not initialized. " + 104 "Have you added a TilesConfigurer to your web application context?"); 105 } 106 107 exposeModelAsRequestAttributes(model, request); 108 JstlUtils.exposeLocalizationContext(new RequestContext(request, servletContext)); 109 if (this.alwaysInclude) { 110 ServletUtil.setForceInclude(request, true); 111 } 112 container.render(getUrl(), request, response); 113 } 114 115}