Skip to content

Commit

Permalink
Converted the ModelAndView to return a Map<String, Object>.
Browse files Browse the repository at this point in the history
Updated GenericIntegrationTest to pass with the change.
  • Loading branch information
N0odlez committed Aug 6, 2016
1 parent 12774f1 commit 77c854b
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 9 deletions.
9 changes: 5 additions & 4 deletions src/main/java/spark/ModelAndView.java
Expand Up @@ -16,6 +16,8 @@
*/ */
package spark; package spark;


import java.util.Map;

/** /**
* Model And View class is used to set the name of the view and the model object * Model And View class is used to set the name of the view and the model object
* to be rendered. * to be rendered.
Expand All @@ -27,7 +29,7 @@ public class ModelAndView {
/** /**
* Model object. * Model object.
*/ */
private Object model; private Map<String, Object> model;
/** /**
* View name used to render output. * View name used to render output.
*/ */
Expand All @@ -39,16 +41,15 @@ public class ModelAndView {
* @param model the model * @param model the model
* @param viewName the view name * @param viewName the view name
*/ */
public ModelAndView(Object model, String viewName) { public ModelAndView(Map<String, Object> model, String viewName) {
super();
this.model = model; this.model = model;
this.viewName = viewName; this.viewName = viewName;
} }


/** /**
* @return the model object * @return the model object
*/ */
public Object getModel() { public Map<String, Object> getModel() {
return model; return model;
} }


Expand Down
4 changes: 3 additions & 1 deletion src/main/java/spark/Spark.java
Expand Up @@ -16,6 +16,8 @@
*/ */
package spark; package spark;


import java.util.Map;

import static spark.Service.ignite; import static spark.Service.ignite;


/** /**
Expand Down Expand Up @@ -1077,7 +1079,7 @@ public static void init() {
* @param viewName the view name * @param viewName the view name
* @return the model and view * @return the model and view
*/ */
public static ModelAndView modelAndView(Object model, String viewName) { public static ModelAndView modelAndView(Map<String, Object> model, String viewName) {
return new ModelAndView(model, viewName); return new ModelAndView(model, viewName);
} }


Expand Down
4 changes: 3 additions & 1 deletion src/main/java/spark/TemplateEngine.java
@@ -1,6 +1,8 @@
package spark; package spark;




import java.util.Map;

/** /**
* A Template holds the implementation of the 'render' method. * A Template holds the implementation of the 'render' method.
* TemplateViewRoute instead of returning the result of calling toString() as body, it returns the result of calling render method. * TemplateViewRoute instead of returning the result of calling toString() as body, it returns the result of calling render method.
Expand Down Expand Up @@ -28,7 +30,7 @@ public String render(Object object) {
* @param viewName to be rendered. * @param viewName to be rendered.
* @return object with model and view set. * @return object with model and view set.
*/ */
public ModelAndView modelAndView(Object model, String viewName) { public ModelAndView modelAndView(Map<String, Object> model, String viewName) {
return new ModelAndView(model, viewName); return new ModelAndView(model, viewName);
} }


Expand Down
4 changes: 3 additions & 1 deletion src/main/java/spark/TemplateViewRouteImpl.java
Expand Up @@ -17,6 +17,8 @@
package spark; package spark;




import java.util.Map;

/** /**
* A TemplateViewRoute is built up by a path (for url-matching) and the implementation of the 'render' method. * A TemplateViewRoute is built up by a path (for url-matching) and the implementation of the 'render' method.
* TemplateViewRoute instead of returning the result of calling toString() as body, it returns the result of calling render method. * TemplateViewRoute instead of returning the result of calling toString() as body, it returns the result of calling render method.
Expand Down Expand Up @@ -92,7 +94,7 @@ public Object render(Object object) {
* @param viewName t be rendered. * @param viewName t be rendered.
* @return object with model and view set. * @return object with model and view set.
*/ */
public ModelAndView modelAndView(Object model, String viewName) { public ModelAndView modelAndView(Map<String, Object> model, String viewName) {
return new ModelAndView(model, viewName); return new ModelAndView(model, viewName);
} }


Expand Down
7 changes: 5 additions & 2 deletions src/test/java/spark/GenericIntegrationTest.java
Expand Up @@ -117,7 +117,10 @@ public static void setup() throws IOException {
}); });


get("/templateView", (q, a) -> { get("/templateView", (q, a) -> {
return new ModelAndView("Hello", "my view"); return new ModelAndView(new HashMap<String, Object>() {
{
put("hello", "Hello");
}}, "my view");
}, new TemplateEngine() { }, new TemplateEngine() {
public String render(ModelAndView modelAndView) { public String render(ModelAndView modelAndView) {
return modelAndView.getModel() + " from " + modelAndView.getViewName(); return modelAndView.getModel() + " from " + modelAndView.getViewName();
Expand Down Expand Up @@ -214,7 +217,7 @@ public void routes_should_be_accept_type_aware() throws Exception {
public void template_view_should_be_rendered_with_given_model_view_object() throws Exception { public void template_view_should_be_rendered_with_given_model_view_object() throws Exception {
UrlResponse response = testUtil.doMethod("GET", "/templateView", null); UrlResponse response = testUtil.doMethod("GET", "/templateView", null);
Assert.assertEquals(200, response.status); Assert.assertEquals(200, response.status);
Assert.assertEquals("Hello from my view", response.body); Assert.assertEquals("{hello=Hello} from my view", response.body);
} }


@Test @Test
Expand Down

0 comments on commit 77c854b

Please sign in to comment.