Browse Source

SPR-6785 - Improve message error when the MarshallingView modelKey attribute is not valid

git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@2893 50f2f4bb-b051-0410-bef5-90022cba6387
pull/1/head
Arjen Poutsma 16 years ago
parent
commit
bf3c057208
  1. 3
      org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/view/xml/MarshallingView.java
  2. 23
      org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/view/xml/MarshallingViewTests.java

3
org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/view/xml/MarshallingView.java

@ -122,6 +122,9 @@ public class MarshallingView extends AbstractView { @@ -122,6 +122,9 @@ public class MarshallingView extends AbstractView {
protected Object locateToBeMarshalled(Map model) throws ServletException {
if (this.modelKey != null) {
Object o = model.get(this.modelKey);
if (o == null) {
throw new ServletException("Model contains no object with key [" + modelKey + "]");
}
if (!this.marshaller.supports(o.getClass())) {
throw new ServletException("Model object [" + o + "] retrieved via key [" + modelKey +
"] is not supported by the Marshaller");

23
org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/view/xml/MarshallingViewTests.java

@ -71,6 +71,29 @@ public class MarshallingViewTests { @@ -71,6 +71,29 @@ public class MarshallingViewTests {
verify(marshallerMock);
}
@Test
public void renderInvalidModelKey() throws Exception {
Object toBeMarshalled = new Object();
String modelKey = "key";
view.setModelKey("invalidKey");
Map model = new HashMap();
model.put(modelKey, toBeMarshalled);
MockHttpServletRequest request = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse();
replay(marshallerMock);
try {
view.render(model, request, response);
fail("ServletException expected");
}
catch (ServletException ex) {
// expected
}
assertEquals("Invalid content length", 0, response.getContentLength());
verify(marshallerMock);
}
@Test
public void renderModelKeyUnsupported() throws Exception {
Object toBeMarshalled = new Object();

Loading…
Cancel
Save