001/* 002 * Copyright 2002-2012 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.velocity; 018 019import org.springframework.web.servlet.view.AbstractTemplateViewResolver; 020import org.springframework.web.servlet.view.AbstractUrlBasedView; 021 022/** 023 * Convenience subclass of {@link org.springframework.web.servlet.view.UrlBasedViewResolver} 024 * that supports {@link VelocityView} (i.e. Velocity templates) and custom subclasses of it. 025 * 026 * <p>The view class for all views generated by this resolver can be specified 027 * via the "viewClass" property. See UrlBasedViewResolver's javadoc for details. 028 * 029 * <p><b>Note:</b> When chaining ViewResolvers, a VelocityViewResolver will 030 * check for the existence of the specified template resources and only return 031 * a non-null View object if the template was actually found. 032 * 033 * @author Juergen Hoeller 034 * @since 13.12.2003 035 * @see #setViewClass 036 * @see #setPrefix 037 * @see #setSuffix 038 * @see #setRequestContextAttribute 039 * @see #setExposeSpringMacroHelpers 040 * @see #setDateToolAttribute 041 * @see #setNumberToolAttribute 042 * @see VelocityView 043 * @deprecated as of Spring 4.3, in favor of FreeMarker 044 */ 045@Deprecated 046public class VelocityViewResolver extends AbstractTemplateViewResolver { 047 048 private String dateToolAttribute; 049 050 private String numberToolAttribute; 051 052 private String toolboxConfigLocation; 053 054 055 public VelocityViewResolver() { 056 setViewClass(requiredViewClass()); 057 } 058 059 /** 060 * Requires {@link VelocityView}. 061 */ 062 @Override 063 protected Class<?> requiredViewClass() { 064 return VelocityView.class; 065 } 066 067 068 /** 069 * Set the name of the DateTool helper object to expose in the Velocity context 070 * of this view, or {@code null} if not needed. DateTool is part of Velocity Tools 1.0. 071 * @see org.apache.velocity.tools.generic.DateTool 072 * @see VelocityView#setDateToolAttribute 073 */ 074 public void setDateToolAttribute(String dateToolAttribute) { 075 this.dateToolAttribute = dateToolAttribute; 076 } 077 078 /** 079 * Set the name of the NumberTool helper object to expose in the Velocity context 080 * of this view, or {@code null} if not needed. NumberTool is part of Velocity Tools 1.1. 081 * @see org.apache.velocity.tools.generic.NumberTool 082 * @see VelocityView#setNumberToolAttribute 083 */ 084 public void setNumberToolAttribute(String numberToolAttribute) { 085 this.numberToolAttribute = numberToolAttribute; 086 } 087 088 /** 089 * Set a Velocity Toolbox config location, for example "/WEB-INF/toolbox.xml", 090 * to automatically load a Velocity Tools toolbox definition file and expose 091 * all defined tools in the specified scopes. If no config location is 092 * specified, no toolbox will be loaded and exposed. 093 * <p>The specified location string needs to refer to a ServletContext 094 * resource, as expected by ServletToolboxManager which is part of 095 * the view package of Velocity Tools. 096 * <p><b>Note:</b> Specifying a toolbox config location will lead to 097 * VelocityToolboxView instances being created. 098 * @see org.apache.velocity.tools.view.servlet.ServletToolboxManager#getInstance 099 * @see VelocityToolboxView#setToolboxConfigLocation 100 */ 101 public void setToolboxConfigLocation(String toolboxConfigLocation) { 102 this.toolboxConfigLocation = toolboxConfigLocation; 103 } 104 105 106 @Override 107 protected void initApplicationContext() { 108 super.initApplicationContext(); 109 110 if (this.toolboxConfigLocation != null) { 111 if (VelocityView.class == getViewClass()) { 112 logger.info("Using VelocityToolboxView instead of default VelocityView " + 113 "due to specified toolboxConfigLocation"); 114 setViewClass(VelocityToolboxView.class); 115 } 116 else if (!VelocityToolboxView.class.isAssignableFrom(getViewClass())) { 117 throw new IllegalArgumentException( 118 "Given view class [" + getViewClass().getName() + 119 "] is not of type [" + VelocityToolboxView.class.getName() + 120 "], which it needs to be in case of a specified toolboxConfigLocation"); 121 } 122 } 123 } 124 125 126 @Override 127 protected AbstractUrlBasedView buildView(String viewName) throws Exception { 128 VelocityView view = (VelocityView) super.buildView(viewName); 129 view.setDateToolAttribute(this.dateToolAttribute); 130 view.setNumberToolAttribute(this.numberToolAttribute); 131 if (this.toolboxConfigLocation != null) { 132 ((VelocityToolboxView) view).setToolboxConfigLocation(this.toolboxConfigLocation); 133 } 134 return view; 135 } 136 137}