org.springframework.oxm.support
public class: MarshallingView [javadoc |
source]
java.lang.Object
org.springframework.web.servlet.view.AbstractUrlBasedView
org.springframework.oxm.support.MarshallingView
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 {@linkplain #locateToBeMarshalled(Map)
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.
- author:
Arjen - Poutsma
- since:
1.5.1 -
| Field Summary |
|---|
| public static final String | DEFAULT_CONTENT_TYPE | Default content type. Overridable as bean property. |
| Methods from java.lang.Object: |
|---|
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Method from org.springframework.oxm.support.MarshallingView Detail: |
protected void initApplicationContext() throws BeansException {
Assert.notNull(marshaller, "Property 'marshaller' is required");
}
|
protected Object locateToBeMarshalled(Map model) throws ServletException {
if (this.modelKey != null) {
Object o = model.get(this.modelKey);
if (!this.marshaller.supports(o.getClass())) {
throw new ServletException("Model object [" + o + "] retrieved via key [" + modelKey +
"] is not supported by the Marshaller");
}
return o;
}
for (Iterator iterator = model.values().iterator(); iterator.hasNext();) {
Object o = iterator.next();
if (this.marshaller.supports(o.getClass())) {
return o;
}
}
return null;
}
Locates the object to be marshalled. The default implementation first attempts to look under the configured
{@linkplain #setModelKey(String) model key}, if any, before attempting to locate an object of {@linkplain
Marshaller#supports(Class) supported type}. |
protected void renderMergedOutputModel(Map model,
HttpServletRequest request,
HttpServletResponse response) throws Exception {
Object toBeMarshalled = locateToBeMarshalled(model);
if (toBeMarshalled == null) {
throw new ServletException("Unable to locate object to be marshalled in model: " + model);
}
ByteArrayOutputStream bos = new ByteArrayOutputStream(2048);
marshaller.marshal(toBeMarshalled, new StreamResult(bos));
response.setContentType(getContentType());
response.setContentLength(bos.size());
ServletOutputStream out = response.getOutputStream();
bos.writeTo(out);
out.flush();
}
|
public void setMarshaller(Marshaller marshaller) {
Assert.notNull(marshaller, "'marshaller' must not be null");
this.marshaller = marshaller;
}
|
public void setModelKey(String modelKey) {
this.modelKey = 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. |