001: /*
002: * Copyright 2002-2006 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: * http://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:
017: package org.springframework.web.servlet.view.freemarker;
018:
019: import java.io.IOException;
020: import java.util.List;
021:
022: import javax.servlet.ServletContext;
023:
024: import freemarker.cache.ClassTemplateLoader;
025: import freemarker.ext.jsp.TaglibFactory;
026: import freemarker.template.Configuration;
027: import freemarker.template.TemplateException;
028:
029: import org.springframework.beans.factory.InitializingBean;
030: import org.springframework.context.ResourceLoaderAware;
031: import org.springframework.ui.freemarker.FreeMarkerConfigurationFactory;
032: import org.springframework.web.context.ServletContextAware;
033:
034: /**
035: * JavaBean to configure FreeMarker for web usage, via the "configLocation"
036: * and/or "freemarkerSettings" and/or "templateLoaderPath" properties.
037: * The simplest way to use this class is to specify just a "templateLoaderPath";
038: * you do not need any further configuration then.
039: *
040: * <pre>
041: * <bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
042: * <property name="templateLoaderPath"><value>/WEB-INF/freemarker/</value></property>
043: * </bean></pre>
044: *
045: * This bean must be included in the application context of any application
046: * using Spring's FreeMarkerView for web MVC. It exists purely to configure FreeMarker.
047: * It is not meant to be referenced by application components but just internally
048: * by FreeMarkerView. Implements FreeMarkerConfig to be found by FreeMarkerView without
049: * depending on the bean name the configurer. Each DispatcherServlet can define its
050: * own FreeMarkerConfigurer if desired.
051: *
052: * <p>Note that you can also refer to a preconfigured FreeMarker Configuration
053: * instance, for example one set up by FreeMarkerConfigurationFactoryBean, via
054: * the "configuration" property. This allows to share a FreeMarker Configuration
055: * for web and email usage, for example.
056: *
057: * <p>This configurer registers a template loader for this package, allowing to
058: * reference the "spring.ftl" macro library (contained in this package and thus
059: * in spring.jar) like this:
060: *
061: * <pre>
062: * <#import "spring.ftl" as spring/>
063: * <@spring.bind "person.age"/>
064: * age is ${spring.status.value}</pre>
065: *
066: * Note: Spring's FreeMarker support requires FreeMarker 2.3 or higher.
067: *
068: * @author Darren Davison
069: * @author Rob Harrop
070: * @since 03.03.2004
071: * @see #setConfigLocation
072: * @see #setFreemarkerSettings
073: * @see #setTemplateLoaderPath
074: * @see #setConfiguration
075: * @see org.springframework.ui.freemarker.FreeMarkerConfigurationFactoryBean
076: * @see FreeMarkerView
077: */
078: public class FreeMarkerConfigurer extends
079: FreeMarkerConfigurationFactory implements FreeMarkerConfig,
080: InitializingBean, ResourceLoaderAware, ServletContextAware {
081:
082: private Configuration configuration;
083:
084: private TaglibFactory taglibFactory;
085:
086: /**
087: * Set a preconfigured Configuration to use for the FreeMarker web config, e.g. a
088: * shared one for web and email usage, set up via FreeMarkerConfigurationFactoryBean.
089: * If this is not set, FreeMarkerConfigurationFactory's properties (inherited by
090: * this class) have to be specified.
091: * @see org.springframework.ui.freemarker.FreeMarkerConfigurationFactoryBean
092: */
093: public void setConfiguration(Configuration configuration) {
094: this .configuration = configuration;
095: }
096:
097: /**
098: * Initialize the {@link TaglibFactory} for the given ServletContext.
099: */
100: public void setServletContext(ServletContext servletContext) {
101: this .taglibFactory = new TaglibFactory(servletContext);
102: }
103:
104: /**
105: * Initialize FreeMarkerConfigurationFactory's Configuration
106: * if not overridden by a preconfigured FreeMarker Configuation.
107: * <p>Sets up a ClassTemplateLoader to use for loading Spring macros.
108: * @see #createConfiguration
109: * @see #setConfiguration
110: */
111: public void afterPropertiesSet() throws IOException,
112: TemplateException {
113: if (this .configuration == null) {
114: this .configuration = createConfiguration();
115: }
116: }
117:
118: /**
119: * This implementation registers an additional ClassTemplateLoader
120: * for the Spring-provided macros, added to the end of the list.
121: */
122: protected void postProcessTemplateLoaders(List templateLoaders) {
123: templateLoaders.add(new ClassTemplateLoader(
124: FreeMarkerConfigurer.class, ""));
125: logger
126: .info("ClassTemplateLoader for Spring macros added to FreeMarker configuration");
127: }
128:
129: /**
130: * Return the Configuration object wrapped by this bean.
131: */
132: public Configuration getConfiguration() {
133: return this .configuration;
134: }
135:
136: /**
137: * Return the TaglibFactory object wrapped by this bean.
138: */
139: public TaglibFactory getTaglibFactory() {
140: return this.taglibFactory;
141: }
142:
143: }
|