Package org.springframework.web.servlet.view.xml

# Class MarshallingView

java.lang.Object
org.springframework.context.support.ApplicationObjectSupport
org.springframework.web.context.support.WebApplicationObjectSupport
org.springframework.web.servlet.view.AbstractView
org.springframework.web.servlet.view.xml.MarshallingView

All Implemented Interfaces:
  `org.springframework.beans.factory.Aware`, `org.springframework.beans.factory.BeanNameAware`, `org.springframework.context.ApplicationContextAware`, `org.springframework.web.context.ServletContextAware`, `View`

public class MarshallingView extends AbstractView
Spring-MVC `View` that allows for response context to be rendered as the result of marshalling by a `Marshaller`.

The Object to be marshalled is supplied as a parameter in the model and then detected during response rendering. Users can either specify a specific entry in the model via the `sourceKey` property or have Spring locate the Source object.

Since:
  3.0
Author:
  Arjen Poutsma, Juergen Hoeller

## Field Summary

Fields
Modifier and Type
Field
Description
`static final String`
`DEFAULT_CONTENT_TYPE`
Default content type.

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

`logger`

### Fields inherited from interface org.springframework.web.servlet.View

`PATH_VARIABLES, RESPONSE_STATUS_ATTRIBUTE, SELECTED_CONTENT_TYPE`

## Constructor Summary

Constructors
Constructor
Description
`MarshallingView()`
Construct a new `MarshallingView` with no `Marshaller` set.
`MarshallingView(org.springframework.oxm.Marshaller marshaller)`
Constructs a new `MarshallingView` with the given `Marshaller` set.

## Method Summary

Modifier and Type
Method
Description
`protected void`
`initApplicationContext()`
`protected boolean`
`isEligibleForMarshalling(String modelKey, Object value)`
Check whether the given value from the current view's model is eligible for marshalling through the configured `Marshaller`.
`protected Object`
`locateToBeMarshalled(Map<String,Object> model)`
Locate the object to be marshalled.
`protected void`
`renderMergedOutputModel(Map<String,Object> model, HttpServletRequest request, HttpServletResponse response)`
Subclasses must implement this method to actually render the view.
`void`
`setMarshaller(org.springframework.oxm.Marshaller marshaller)`
Set the `Marshaller` to be used by this view.
`void`
`setModelKey(String modelKey)`
Set the name of the model key that represents the object to be marshalled.

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

`addStaticAttribute, createMergedOutputModel, createRequestContext, createTemporaryOutputStream, exposeModelAsRequestAttributes, formatViewName, generatesDownloadContent, getAttributesMap, getBeanName, getContentType, getRequestContextAttribute, getRequestToExpose, getStaticAttributes, isExposePathVariables, prepareResponse, render, setAttributes, setAttributesCSV, setAttributesMap, setBeanName, setContentType, setExposeContextBeansAsAttributes, setExposedContextBeanNames, setExposePathVariables, setRequestContextAttribute, setResponseContentType, toString, writeToResponse`

### 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, wait, wait, wait`

## Field Details

### DEFAULT_CONTENT_TYPE

public static final String DEFAULT_CONTENT_TYPE
Default content type. Overridable as bean property.

See Also:
  - Constant Field Values

## Constructor Details

### MarshallingView

public MarshallingView()
Construct a new `MarshallingView` with no `Marshaller` set. The marshaller must be set after construction by invoking `setMarshaller(org.springframework.oxm.Marshaller)`.

### MarshallingView

public MarshallingView(org.springframework.oxm.Marshaller marshaller)
Constructs a new `MarshallingView` with the given `Marshaller` set.

## Method Details

### setMarshaller

public void setMarshaller(org.springframework.oxm.Marshaller marshaller)
Set the `Marshaller` to be used by this view.

### setModelKey

public void setModelKey(String modelKey)
Set the name of the model key that represents the object to be marshalled. If not specified, the model map will be searched for a supported value type.

See Also:
  - `Marshaller.supports(Class)`

### initApplicationContext

protected void initApplicationContext()

Overrides:
  `initApplicationContext` in class `org.springframework.context.support.ApplicationObjectSupport`

### renderMergedOutputModel

protected void renderMergedOutputModel(Map<String,Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception
Description copied from class: `AbstractView`
Subclasses must implement this method to actually render the view.

The first step will be preparing the request: In the JSP case, this would mean setting model objects as request attributes. The second step will be the actual rendering of the view, for example including the JSP via a RequestDispatcher.

Specified by:
  `renderMergedOutputModel` in class `AbstractView`
Parameters:
  `model` - combined output Map (never `null`), with dynamic values taking precedence over static attributes
  `request` - current HTTP request
  `response` - current HTTP response
Throws:
  `Exception` - if rendering failed

### locateToBeMarshalled

@Nullable protected Object locateToBeMarshalled(Map<String,Object> model) throws IllegalStateException
Locate the object to be marshalled.

The default implementation first attempts to look under the configured model key, if any, before attempting to locate an object of supported type.

Parameters:
  `model` - the model Map
Returns:
  the Object to be marshalled (or `null` if none found)
Throws:
  `IllegalStateException` - if the model object specified by the model key is not supported by the marshaller
See Also:
  - `setModelKey(String)`

### isEligibleForMarshalling

protected boolean isEligibleForMarshalling(String modelKey, Object value)
Check whether the given value from the current view's model is eligible for marshalling through the configured `Marshaller`.

The default implementation calls `Marshaller.supports(Class)`, unwrapping a given `JAXBElement` first if applicable.

Parameters:
  `modelKey` - the value's key in the model (never `null`)
  `value` - the value to check (never `null`)
Returns:
  whether the given value is to be considered as eligible
See Also:
  - `Marshaller.supports(Class)`