MarshallingView explicitly skips BindingResult when searching for a model object
Just implementing common custom subclass behavior out-of-the-box... Issue: SPR-11417
This commit is contained in:
parent
3716a8ef7d
commit
6f58491b9c
|
@ -25,6 +25,7 @@ import javax.xml.transform.stream.StreamResult;
|
||||||
import org.springframework.oxm.Marshaller;
|
import org.springframework.oxm.Marshaller;
|
||||||
import org.springframework.util.Assert;
|
import org.springframework.util.Assert;
|
||||||
import org.springframework.util.StreamUtils;
|
import org.springframework.util.StreamUtils;
|
||||||
|
import org.springframework.validation.BindingResult;
|
||||||
import org.springframework.web.servlet.View;
|
import org.springframework.web.servlet.View;
|
||||||
import org.springframework.web.servlet.view.AbstractView;
|
import org.springframework.web.servlet.view.AbstractView;
|
||||||
|
|
||||||
|
@ -38,6 +39,7 @@ import org.springframework.web.servlet.view.AbstractView;
|
||||||
* property or have Spring locate the Source object.
|
* property or have Spring locate the Source object.
|
||||||
*
|
*
|
||||||
* @author Arjen Poutsma
|
* @author Arjen Poutsma
|
||||||
|
* @author Juergen Hoeller
|
||||||
* @since 3.0
|
* @since 3.0
|
||||||
*/
|
*/
|
||||||
public class MarshallingView extends AbstractView {
|
public class MarshallingView extends AbstractView {
|
||||||
|
@ -67,7 +69,8 @@ public class MarshallingView extends AbstractView {
|
||||||
*/
|
*/
|
||||||
public MarshallingView(Marshaller marshaller) {
|
public MarshallingView(Marshaller marshaller) {
|
||||||
this();
|
this();
|
||||||
setMarshaller(marshaller);
|
Assert.notNull(marshaller, "Marshaller must not be null");
|
||||||
|
this.marshaller = marshaller;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -75,7 +78,6 @@ public class MarshallingView extends AbstractView {
|
||||||
* Sets the {@link Marshaller} to be used by this view.
|
* Sets the {@link Marshaller} to be used by this view.
|
||||||
*/
|
*/
|
||||||
public void setMarshaller(Marshaller marshaller) {
|
public void setMarshaller(Marshaller marshaller) {
|
||||||
Assert.notNull(marshaller, "Marshaller must not be null");
|
|
||||||
this.marshaller = marshaller;
|
this.marshaller = marshaller;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -112,9 +114,10 @@ public class MarshallingView extends AbstractView {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Locates the object to be marshalled. The default implementation first attempts to look
|
* Locate the object to be marshalled.
|
||||||
* under the configured {@linkplain #setModelKey(String) model key}, if any, before attempting
|
* <p>The default implementation first attempts to look under the configured
|
||||||
* to locate an object of {@linkplain Marshaller#supports(Class) supported type}.
|
* {@linkplain #setModelKey(String) model key}, if any, before attempting to
|
||||||
|
* locate an object of {@linkplain Marshaller#supports(Class) supported type}.
|
||||||
* @param model the model Map
|
* @param model the model Map
|
||||||
* @return the Object to be marshalled (or {@code null} if none found)
|
* @return the Object to be marshalled (or {@code null} if none found)
|
||||||
* @throws IllegalStateException if the model object specified by the
|
* @throws IllegalStateException if the model object specified by the
|
||||||
|
@ -134,7 +137,8 @@ public class MarshallingView extends AbstractView {
|
||||||
return obj;
|
return obj;
|
||||||
}
|
}
|
||||||
for (Object obj : model.values()) {
|
for (Object obj : model.values()) {
|
||||||
if (obj != null && this.marshaller.supports(obj.getClass())) {
|
if (obj != null && (model.size() == 1 || !(obj instanceof BindingResult)) &&
|
||||||
|
this.marshaller.supports(obj.getClass())) {
|
||||||
return obj;
|
return obj;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,6 +17,7 @@
|
||||||
package org.springframework.web.servlet.view.xml;
|
package org.springframework.web.servlet.view.xml;
|
||||||
|
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.LinkedHashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import javax.xml.transform.stream.StreamResult;
|
import javax.xml.transform.stream.StreamResult;
|
||||||
|
|
||||||
|
@ -26,6 +27,8 @@ import org.junit.Test;
|
||||||
import org.springframework.mock.web.test.MockHttpServletRequest;
|
import org.springframework.mock.web.test.MockHttpServletRequest;
|
||||||
import org.springframework.mock.web.test.MockHttpServletResponse;
|
import org.springframework.mock.web.test.MockHttpServletResponse;
|
||||||
import org.springframework.oxm.Marshaller;
|
import org.springframework.oxm.Marshaller;
|
||||||
|
import org.springframework.validation.BeanPropertyBindingResult;
|
||||||
|
import org.springframework.validation.BindingResult;
|
||||||
|
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.*;
|
||||||
import static org.mockito.BDDMockito.*;
|
import static org.mockito.BDDMockito.*;
|
||||||
|
@ -35,9 +38,10 @@ import static org.mockito.BDDMockito.*;
|
||||||
*/
|
*/
|
||||||
public class MarshallingViewTests {
|
public class MarshallingViewTests {
|
||||||
|
|
||||||
|
private Marshaller marshallerMock;
|
||||||
|
|
||||||
private MarshallingView view;
|
private MarshallingView view;
|
||||||
|
|
||||||
private Marshaller marshallerMock;
|
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void createView() throws Exception {
|
public void createView() throws Exception {
|
||||||
|
@ -45,6 +49,7 @@ public class MarshallingViewTests {
|
||||||
view = new MarshallingView(marshallerMock);
|
view = new MarshallingView(marshallerMock);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void getContentType() {
|
public void getContentType() {
|
||||||
assertEquals("Invalid content type", "application/xml", view.getContentType());
|
assertEquals("Invalid content type", "application/xml", view.getContentType());
|
||||||
|
@ -159,6 +164,26 @@ public class MarshallingViewTests {
|
||||||
verify(marshallerMock).marshal(eq(toBeMarshalled), isA(StreamResult.class));
|
verify(marshallerMock).marshal(eq(toBeMarshalled), isA(StreamResult.class));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void renderNoModelKeyAndBindingResultFirst() throws Exception {
|
||||||
|
Object toBeMarshalled = new Object();
|
||||||
|
String modelKey = "key";
|
||||||
|
Map<String, Object> model = new LinkedHashMap<String, Object>();
|
||||||
|
model.put(BindingResult.MODEL_KEY_PREFIX + modelKey, new BeanPropertyBindingResult(toBeMarshalled, modelKey));
|
||||||
|
model.put(modelKey, toBeMarshalled);
|
||||||
|
|
||||||
|
MockHttpServletRequest request = new MockHttpServletRequest();
|
||||||
|
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||||
|
|
||||||
|
given(marshallerMock.supports(BeanPropertyBindingResult.class)).willReturn(true);
|
||||||
|
given(marshallerMock.supports(Object.class)).willReturn(true);
|
||||||
|
|
||||||
|
view.render(model, request, response);
|
||||||
|
assertEquals("Invalid content type", "application/xml", response.getContentType());
|
||||||
|
assertEquals("Invalid content length", 0, response.getContentLength());
|
||||||
|
verify(marshallerMock).marshal(eq(toBeMarshalled), isA(StreamResult.class));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testRenderUnsupportedModel() throws Exception {
|
public void testRenderUnsupportedModel() throws Exception {
|
||||||
Object toBeMarshalled = new Object();
|
Object toBeMarshalled = new Object();
|
||||||
|
|
Loading…
Reference in New Issue