Top Description Fields Constructors Methods
org.springframework.web.servlet.view.xml

public Class MarshallingView

extends AbstractView
Class Inheritance
Imports
java.io.ByteArrayOutputStream, java.util.Map, javax.xml.transform.stream.StreamResult, jakarta.servlet.http.HttpServletRequest, .HttpServletResponse, jakarta.xml.bind.JAXBElement, org.springframework.lang.Nullable, org.springframework.oxm.Marshaller, org.springframework.util.Assert, org.springframework.validation.BindingResult, org.springframework.web.servlet.View, org.springframework.web.servlet.view.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.

Authors
Arjen Poutsma, Juergen Hoeller
Since
3.0

Field Summary

Modifier and TypeField and Description
public static final String
private Marshaller
private String

Constructor Summary

AccessConstructor and Description
public
MarshallingView()

Construct a new MarshallingView with no Marshaller set.

public
MarshallingView(Marshaller marshaller)

Constructs a new MarshallingView with the given Marshaller set.

Method Summary

Modifier and TypeMethod and Description
protected void
initApplicationContext()

Overrides org.springframework.context.support.ApplicationObjectSupport.initApplicationContext.

Subclasses can override this for custom initialization behavior.

protected boolean

Returns:

whether the given value is to be considered as eligible
isEligibleForMarshalling
(String
the value's key in the model (never null)
modelKey
,
Object
the value to check (never null)
value
)

Check whether the given value from the current view's model is eligible for marshalling through the configured Marshaller.

protected Object

Returns:

the Object to be marshalled (or null if none found)
locateToBeMarshalled
(Map<String, Object>
the model Map
model
)

Locate the object to be marshalled.

protected void
renderMergedOutputModel(Map<String, Object>
combined output Map (never null), with dynamic values taking precedence over static attributes
model
,
HttpServletRequest
current HTTP request
request
,
HttpServletResponse
current HTTP response
response
)

Implements abstract org.springframework.web.servlet.view.AbstractView.renderMergedOutputModel.

Subclasses must implement this method to actually render the view.

public void
setMarshaller(Marshaller marshaller)

Set the Marshaller to be used by this view.

public void
setModelKey(String modelKey)

Set the name of the model key that represents the object to be marshalled.

Inherited from org.springframework.web.servlet.view.AbstractView:
addStaticAttributecreateMergedOutputModelcreateRequestContextcreateTemporaryOutputStreamexposeModelAsRequestAttributesformatViewNamegeneratesDownloadContentgetAttributesMapgetBeanNamegetContentTypegetRequestContextAttributegetRequestToExposegetStaticAttributesisExposePathVariablesprepareResponserendersetAttributessetAttributesCSVsetAttributesMapsetBeanNamesetContentTypesetExposeContextBeansAsAttributessetExposedContextBeanNamessetExposePathVariablessetRequestContextAttributesetResponseContentTypetoStringwriteToResponse

Field Detail

DEFAULT_CONTENT_TYPEback to summary
public static final String DEFAULT_CONTENT_TYPE

Hides org.springframework.web.servlet.view.AbstractView.DEFAULT_CONTENT_TYPE.

Default content type. Overridable as bean property.

marshallerback to summary
private Marshaller marshaller
Annotations
@Nullable
modelKeyback to summary
private String modelKey
Annotations
@Nullable

Constructor Detail

MarshallingViewback to summary
public MarshallingView()

Construct a new MarshallingView with no Marshaller set. The marshaller must be set after construction by invoking setMarshaller.

MarshallingViewback to summary
public MarshallingView(Marshaller marshaller)

Constructs a new MarshallingView with the given Marshaller set.

Method Detail

initApplicationContextback to summary
protected void initApplicationContext()

Overrides org.springframework.context.support.ApplicationObjectSupport.initApplicationContext.

Doc from org.springframework.context.support.ApplicationObjectSupport.initApplicationContext.

Subclasses can override this for custom initialization behavior.

The default implementation is empty. Called by initApplicationContext(ApplicationContext).

Annotations
@Override
isEligibleForMarshallingback to summary
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:String

the value's key in the model (never null)

value:Object

the value to check (never null)

Returns:boolean

whether the given value is to be considered as eligible

See Also
Marshaller#supports(Class)
locateToBeMarshalledback to summary
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:Map<String, Object>

the model Map

Returns:Object

the Object to be marshalled (or null if none found)

Annotations
@Nullable
Exceptions
IllegalStateException:
if the model object specified by the model key is not supported by the marshaller
See Also
setModelKey(String)
renderMergedOutputModelback to summary
protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception

Implements abstract org.springframework.web.servlet.view.AbstractView.renderMergedOutputModel.

Doc from org.springframework.web.servlet.view.AbstractView.renderMergedOutputModel.

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.

Parameters
model:Map<String, Object>

combined output Map (never null), with dynamic values taking precedence over static attributes

request:HttpServletRequest

current HTTP request

response:HttpServletResponse

current HTTP response

Annotations
@Override
Exceptions
Exception:
if rendering failed
setMarshallerback to summary
public void setMarshaller(Marshaller marshaller)

Set the Marshaller to be used by this view.

setModelKeyback to summary
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)