KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > servlet > view > freemarker > FreeMarkerConfigurer


1 /*
2  * Copyright 2002-2006 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.freemarker;
18
19 import java.io.IOException JavaDoc;
20 import java.util.List JavaDoc;
21
22 import javax.servlet.ServletContext JavaDoc;
23
24 import freemarker.cache.ClassTemplateLoader;
25 import freemarker.ext.jsp.TaglibFactory;
26 import freemarker.template.Configuration;
27 import freemarker.template.TemplateException;
28
29 import org.springframework.beans.factory.InitializingBean;
30 import org.springframework.context.ResourceLoaderAware;
31 import org.springframework.ui.freemarker.FreeMarkerConfigurationFactory;
32 import org.springframework.web.context.ServletContextAware;
33
34 /**
35  * JavaBean to configure FreeMarker for web usage, via the "configLocation"
36  * and/or "freemarkerSettings" and/or "templateLoaderPath" properties.
37  * The simplest way to use this class is to specify just a "templateLoaderPath";
38  * you do not need any further configuration then.
39  *
40  * <pre>
41  * &lt;bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer"&gt;
42  * &lt;property name="templateLoaderPath"&gt;&lt;value&gt;/WEB-INF/freemarker/&lt;/value>&lt;/property&gt;
43  * &lt;/bean&gt;</pre>
44  *
45  * This bean must be included in the application context of any application
46  * using Spring's FreeMarkerView for web MVC. It exists purely to configure FreeMarker.
47  * It is not meant to be referenced by application components but just internally
48  * by FreeMarkerView. Implements FreeMarkerConfig to be found by FreeMarkerView without
49  * depending on the bean name the configurer. Each DispatcherServlet can define its
50  * own FreeMarkerConfigurer if desired.
51  *
52  * <p>Note that you can also refer to a preconfigured FreeMarker Configuration
53  * instance, for example one set up by FreeMarkerConfigurationFactoryBean, via
54  * the "configuration" property. This allows to share a FreeMarker Configuration
55  * for web and email usage, for example.
56  *
57  * <p>This configurer registers a template loader for this package, allowing to
58  * reference the "spring.ftl" macro library (contained in this package and thus
59  * in spring.jar) like this:
60  *
61  * <pre>
62  * &lt;#import "spring.ftl" as spring/&gt;
63  * &lt;@spring.bind "person.age"/&gt;
64  * age is ${spring.status.value}</pre>
65  *
66  * Note: Spring's FreeMarker support requires FreeMarker 2.3 or higher.
67  *
68  * @author Darren Davison
69  * @author Rob Harrop
70  * @since 03.03.2004
71  * @see #setConfigLocation
72  * @see #setFreemarkerSettings
73  * @see #setTemplateLoaderPath
74  * @see #setConfiguration
75  * @see org.springframework.ui.freemarker.FreeMarkerConfigurationFactoryBean
76  * @see FreeMarkerView
77  */

78 public class FreeMarkerConfigurer extends FreeMarkerConfigurationFactory
79         implements FreeMarkerConfig, InitializingBean, ResourceLoaderAware, ServletContextAware {
80
81     private Configuration configuration;
82
83     private TaglibFactory taglibFactory;
84
85
86     /**
87      * Set a preconfigured Configuration to use for the FreeMarker web config, e.g. a
88      * shared one for web and email usage, set up via FreeMarkerConfigurationFactoryBean.
89      * If this is not set, FreeMarkerConfigurationFactory's properties (inherited by
90      * this class) have to be specified.
91      * @see org.springframework.ui.freemarker.FreeMarkerConfigurationFactoryBean
92      */

93     public void setConfiguration(Configuration configuration) {
94         this.configuration = configuration;
95     }
96
97     /**
98      * Initialize the {@link TaglibFactory} for the given ServletContext.
99      */

100     public void setServletContext(ServletContext JavaDoc servletContext) {
101         this.taglibFactory = new TaglibFactory(servletContext);
102     }
103
104
105     /**
106      * Initialize FreeMarkerConfigurationFactory's Configuration
107      * if not overridden by a preconfigured FreeMarker Configuation.
108      * <p>Sets up a ClassTemplateLoader to use for loading Spring macros.
109      * @see #createConfiguration
110      * @see #setConfiguration
111      */

112     public void afterPropertiesSet() throws IOException JavaDoc, 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 JavaDoc templateLoaders) {
123         templateLoaders.add(new ClassTemplateLoader(FreeMarkerConfigurer.class, ""));
124         logger.info("ClassTemplateLoader for Spring macros added to FreeMarker configuration");
125     }
126
127
128     /**
129      * Return the Configuration object wrapped by this bean.
130      */

131     public Configuration getConfiguration() {
132         return this.configuration;
133     }
134
135     /**
136      * Return the TaglibFactory object wrapped by this bean.
137      */

138     public TaglibFactory getTaglibFactory() {
139         return this.taglibFactory;
140     }
141
142 }
143
Popular Tags