Package org.springframework.web.servlet.view.freemarker

# Class FreeMarkerViewResolver

java.lang.Object
org.springframework.context.support.ApplicationObjectSupport
org.springframework.web.context.support.WebApplicationObjectSupport
org.springframework.web.servlet.view.AbstractCachingViewResolver
org.springframework.web.servlet.view.UrlBasedViewResolver
org.springframework.web.servlet.view.AbstractTemplateViewResolver
org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver

All Implemented Interfaces:
  `org.springframework.beans.factory.Aware`, `org.springframework.context.ApplicationContextAware`, `org.springframework.core.Ordered`, `org.springframework.web.context.ServletContextAware`, `ViewResolver`

public class FreeMarkerViewResolver extends AbstractTemplateViewResolver
Convenience subclass of `UrlBasedViewResolver` that supports `FreeMarkerView` (i.e. FreeMarker templates) and custom subclasses of it.

The view class for all views generated by this resolver can be specified via the "viewClass" property. See `UrlBasedViewResolver` for details.

Note: To ensure that the correct encoding is used when the rendering the response, set the content type with an appropriate `charset` attribute — for example, `"text/html;charset=UTF-8"`; however, as of Spring Framework 6.2, it is no longer strictly necessary to explicitly set the content type in the `FreeMarkerViewResolver` if you have set an explicit encoding via either `FreeMarkerView.setEncoding(String)`, `FreeMarkerConfigurationFactory.setDefaultEncoding(String)`, or `Configuration.setDefaultEncoding(String)`.

Note: When chaining ViewResolvers, a `FreeMarkerViewResolver` will check for the existence of the specified template resources and only return a non-null `View` object if the template was actually found.

Note: Spring's FreeMarker support requires FreeMarker 2.3.33 or higher.

Since:
  1.1
Author:
  Juergen Hoeller, Sam Brannen
See Also:
  - `UrlBasedViewResolver.setViewClass(java.lang.Class<?>)`
  - `UrlBasedViewResolver.setPrefix(java.lang.String)`
  - `UrlBasedViewResolver.setSuffix(java.lang.String)`
  - `UrlBasedViewResolver.setContentType(java.lang.String)`
  - `UrlBasedViewResolver.setRequestContextAttribute(java.lang.String)`
  - `AbstractTemplateViewResolver.setExposeSpringMacroHelpers(boolean)`
  - `FreeMarkerView`

## Nested Class Summary

## Nested classes/interfaces inherited from class org.springframework.web.servlet.view.AbstractCachingViewResolver

`AbstractCachingViewResolver.CacheFilter`

## Field Summary

### Fields inherited from class org.springframework.web.servlet.view.UrlBasedViewResolver

`FORWARD_URL_PREFIX, REDIRECT_URL_PREFIX`

### Fields inherited from class org.springframework.web.servlet.view.AbstractCachingViewResolver

`DEFAULT_CACHE_LIMIT`

### Fields inherited from class org.springframework.context.support.ApplicationObjectSupport

`logger`

### Fields inherited from interface org.springframework.core.Ordered

`HIGHEST_PRECEDENCE, LOWEST_PRECEDENCE`

## Constructor Summary

Constructors
Constructor
Description
`FreeMarkerViewResolver()`
Sets the default `view class` to `requiredViewClass()`: by default `FreeMarkerView`.
`FreeMarkerViewResolver(String prefix, String suffix)`
A convenience constructor that allows for specifying `prefix` and `suffix` as constructor arguments.

## Method Summary

Modifier and Type
Method
Description
`protected AbstractUrlBasedView`
`instantiateView()`
Instantiate the specified view class.
`protected View`
`loadView(String viewName, Locale locale)`
Delegates to `super.loadView(viewName, locale)` for standard behavior and then to `postProcessView(FreeMarkerView)` for customization.
`protected void`
`postProcessView(FreeMarkerView freeMarkerView)`
Post process the supplied `FreeMarkerView` after it has been loaded.
`protected Class<?>`
`requiredViewClass()`
Requires `FreeMarkerView`.

### Methods inherited from class org.springframework.web.servlet.view.AbstractTemplateViewResolver

`buildView, setAllowRequestOverride, setAllowSessionOverride, setExposeRequestAttributes, setExposeSessionAttributes, setExposeSpringMacroHelpers`

### Methods inherited from class org.springframework.web.servlet.view.UrlBasedViewResolver

`applyLifecycleMethods, canHandle, createView, getAttributesMap, getCacheKey, getContentType, getExposeContextBeansAsAttributes, getExposedContextBeanNames, getExposePathVariables, getOrder, getPrefix, getRedirectHosts, getRequestContextAttribute, getSuffix, getViewClass, getViewNames, initApplicationContext, isRedirectContextRelative, isRedirectHttp10Compatible, setAttributes, setAttributesMap, setContentType, setExposeContextBeansAsAttributes, setExposedContextBeanNames, setExposePathVariables, setOrder, setPrefix, setRedirectContextRelative, setRedirectHosts, setRedirectHttp10Compatible, setRequestContextAttribute, setSuffix, setViewClass, setViewNames`

### Methods inherited from class org.springframework.web.servlet.view.AbstractCachingViewResolver

`clearCache, getCacheFilter, getCacheLimit, isCache, isCacheUnresolved, removeFromCache, resolveViewName, setCache, setCacheFilter, setCacheLimit, setCacheUnresolved`

### Methods inherited from class org.springframework.web.context.support.WebApplicationObjectSupport

`getServletContext, getTempDir, getWebApplicationContext, initApplicationContext, initServletContext, isContextRequired, setServletContext`

### Methods inherited from class org.springframework.context.support.ApplicationObjectSupport

`getApplicationContext, getMessageSourceAccessor, obtainApplicationContext, requiredContextClass, setApplicationContext`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Constructor Details

### FreeMarkerViewResolver

public FreeMarkerViewResolver()
Sets the default `view class` to `requiredViewClass()`: by default `FreeMarkerView`.

### FreeMarkerViewResolver

public FreeMarkerViewResolver(String prefix, String suffix)
A convenience constructor that allows for specifying `prefix` and `suffix` as constructor arguments.

Parameters:
  `prefix` - the prefix that gets prepended to view names when building a URL
  `suffix` - the suffix that gets appended to view names when building a URL
Since:
  4.3

## Method Details

### requiredViewClass

protected Class<?> requiredViewClass()
Requires `FreeMarkerView`.

Overrides:
  `requiredViewClass` in class `AbstractTemplateViewResolver`
See Also:
  - `UrlBasedViewResolver.instantiateView()`
  - `AbstractUrlBasedView`

### instantiateView

protected AbstractUrlBasedView instantiateView()
Description copied from class: `UrlBasedViewResolver`
Instantiate the specified view class.

The default implementation uses reflection to instantiate the class.

Overrides:
  `instantiateView` in class `UrlBasedViewResolver`
Returns:
  a new instance of the view class
See Also:
  - `UrlBasedViewResolver.setViewClass(java.lang.Class<?>)`

### loadView

@Nullable protected View loadView(String viewName, Locale locale) throws Exception
Delegates to `super.loadView(viewName, locale)` for standard behavior and then to `postProcessView(FreeMarkerView)` for customization.

Overrides:
  `loadView` in class `UrlBasedViewResolver`
Parameters:
  `viewName` - the name of the view to retrieve
  `locale` - the Locale to retrieve the view for
Returns:
  the View instance
Throws:
  `Exception` - if the view couldn't be resolved
Since:
  6.2
See Also:
  - `UrlBasedViewResolver.loadView(String, Locale)`
  - `postProcessView(FreeMarkerView)`

### postProcessView

protected void postProcessView(FreeMarkerView freeMarkerView)
Post process the supplied `FreeMarkerView` after it has been loaded.

The default implementation attempts to override the content type of the view with `"text/html;charset=<encoding>"`, where `<encoding>` is equal to an explicitly configured character encoding for the underlying FreeMarker template file. If an explicit content type has been configured for this view resolver or if no explicit character encoding has been configured for the template file, this method does not modify the supplied `FreeMarkerView`.

Since:
  6.2
See Also:
  - `loadView(String, Locale)`
  - `UrlBasedViewResolver.setContentType(String)`
  - `AbstractView.setContentType(String)`