Search in sources :

Example 31 with ModelMap

use of org.springframework.ui.ModelMap in project spring-boot by spring-projects.

the class MyController method get.

@GetMapping("/")
public ModelAndView get(HttpSession session) {
    Object sessionVar = session.getAttribute("var");
    if (sessionVar == null) {
        sessionVar = new Date();
        session.setAttribute("var", sessionVar);
    }
    ModelMap model = new ModelMap("message", Message.MESSAGE).addAttribute("sessionVar", sessionVar);
    return new ModelAndView("hello", model);
}
Also used : ModelMap(org.springframework.ui.ModelMap) ModelAndView(org.springframework.web.servlet.ModelAndView) Date(java.util.Date) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 32 with ModelMap

use of org.springframework.ui.ModelMap in project spring-framework by spring-projects.

the class FreeMarkerViewTests method render.

@Test
public void render() throws Exception {
    FreeMarkerView view = new FreeMarkerView();
    view.setConfiguration(this.freeMarkerConfig);
    view.setUrl("test.ftl");
    ModelMap model = new ExtendedModelMap();
    model.addAttribute("hello", "hi FreeMarker");
    view.render(model, null, this.exchange).block(Duration.ofMillis(5000));
    StepVerifier.create(this.exchange.getResponse().getBody()).consumeNextWith(buf -> assertEquals("<html><body>hi FreeMarker</body></html>", asString(buf))).expectComplete().verify();
}
Also used : ExtendedModelMap(org.springframework.ui.ExtendedModelMap) ModelMap(org.springframework.ui.ModelMap) ExtendedModelMap(org.springframework.ui.ExtendedModelMap) Test(org.junit.Test)

Example 33 with ModelMap

use of org.springframework.ui.ModelMap in project spring-framework by spring-projects.

the class RequestMappingHandlerAdapterIntegrationTests method handle.

@Test
public void handle() throws Exception {
    Class<?>[] parameterTypes = new Class<?>[] { int.class, String.class, String.class, String.class, Map.class, Date.class, Map.class, String.class, String.class, TestBean.class, Errors.class, TestBean.class, Color.class, HttpServletRequest.class, HttpServletResponse.class, TestBean.class, TestBean.class, User.class, OtherUser.class, Model.class, UriComponentsBuilder.class };
    String datePattern = "yyyy.MM.dd";
    String formattedDate = "2011.03.16";
    Date date = new GregorianCalendar(2011, Calendar.MARCH, 16).getTime();
    TestBean sessionAttribute = new TestBean();
    TestBean requestAttribute = new TestBean();
    request.addHeader("Content-Type", "text/plain; charset=utf-8");
    request.addHeader("header", "headerValue");
    request.addHeader("anotherHeader", "anotherHeaderValue");
    request.addParameter("datePattern", datePattern);
    request.addParameter("dateParam", formattedDate);
    request.addParameter("paramByConvention", "paramByConventionValue");
    request.addParameter("age", "25");
    request.setCookies(new Cookie("cookie", "99"));
    request.setContent("Hello World".getBytes("UTF-8"));
    request.setUserPrincipal(new User());
    request.setContextPath("/contextPath");
    request.setServletPath("/main");
    System.setProperty("systemHeader", "systemHeaderValue");
    Map<String, String> uriTemplateVars = new HashMap<>();
    uriTemplateVars.put("pathvar", "pathvarValue");
    request.setAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE, uriTemplateVars);
    request.getSession().setAttribute("sessionAttribute", sessionAttribute);
    request.setAttribute("requestAttribute", requestAttribute);
    HandlerMethod handlerMethod = handlerMethod("handle", parameterTypes);
    ModelAndView mav = handlerAdapter.handle(request, response, handlerMethod);
    ModelMap model = mav.getModelMap();
    assertEquals("viewName", mav.getViewName());
    assertEquals(99, model.get("cookie"));
    assertEquals("pathvarValue", model.get("pathvar"));
    assertEquals("headerValue", model.get("header"));
    assertEquals(date, model.get("dateParam"));
    Map<?, ?> map = (Map<?, ?>) model.get("headerMap");
    assertEquals("headerValue", map.get("header"));
    assertEquals("anotherHeaderValue", map.get("anotherHeader"));
    assertEquals("systemHeaderValue", model.get("systemHeader"));
    map = (Map<?, ?>) model.get("paramMap");
    assertEquals(formattedDate, map.get("dateParam"));
    assertEquals("paramByConventionValue", map.get("paramByConvention"));
    assertEquals("/contextPath", model.get("value"));
    TestBean modelAttr = (TestBean) model.get("modelAttr");
    assertEquals(25, modelAttr.getAge());
    assertEquals("Set by model method [modelAttr]", modelAttr.getName());
    assertSame(modelAttr, request.getSession().getAttribute("modelAttr"));
    BindingResult bindingResult = (BindingResult) model.get(BindingResult.MODEL_KEY_PREFIX + "modelAttr");
    assertSame(modelAttr, bindingResult.getTarget());
    assertEquals(1, bindingResult.getErrorCount());
    String conventionAttrName = "testBean";
    TestBean modelAttrByConvention = (TestBean) model.get(conventionAttrName);
    assertEquals(25, modelAttrByConvention.getAge());
    assertEquals("Set by model method [modelAttrByConvention]", modelAttrByConvention.getName());
    assertSame(modelAttrByConvention, request.getSession().getAttribute(conventionAttrName));
    bindingResult = (BindingResult) model.get(BindingResult.MODEL_KEY_PREFIX + conventionAttrName);
    assertSame(modelAttrByConvention, bindingResult.getTarget());
    assertTrue(model.get("customArg") instanceof Color);
    assertEquals(User.class, model.get("user").getClass());
    assertEquals(OtherUser.class, model.get("otherUser").getClass());
    assertSame(sessionAttribute, model.get("sessionAttribute"));
    assertSame(requestAttribute, model.get("requestAttribute"));
    assertEquals(new URI("http://localhost/contextPath/main/path"), model.get("url"));
}
Also used : Cookie(javax.servlet.http.Cookie) BindingResult(org.springframework.validation.BindingResult) HashMap(java.util.HashMap) Color(java.awt.Color) ModelMap(org.springframework.ui.ModelMap) GregorianCalendar(java.util.GregorianCalendar) ModelAndView(org.springframework.web.servlet.ModelAndView) MockHttpServletResponse(org.springframework.mock.web.test.MockHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) URI(java.net.URI) Date(java.util.Date) InvocableHandlerMethod(org.springframework.web.method.support.InvocableHandlerMethod) HandlerMethod(org.springframework.web.method.HandlerMethod) MockMultipartHttpServletRequest(org.springframework.mock.web.test.MockMultipartHttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) Errors(org.springframework.validation.Errors) TestBean(org.springframework.tests.sample.beans.TestBean) UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) Model(org.springframework.ui.Model) ModelMap(org.springframework.ui.ModelMap) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 34 with ModelMap

use of org.springframework.ui.ModelMap in project spring-framework by spring-projects.

the class RedirectViewTests method flashMap.

@SuppressWarnings("AssertEqualsBetweenInconvertibleTypes")
@Test
public void flashMap() throws Exception {
    RedirectView rv = new RedirectView();
    rv.setUrl("http://url.somewhere.com/path");
    rv.setHttp10Compatible(false);
    FlashMap flashMap = new FlashMap();
    flashMap.put("successMessage", "yay!");
    request.setAttribute(DispatcherServlet.OUTPUT_FLASH_MAP_ATTRIBUTE, flashMap);
    ModelMap model = new ModelMap("id", "1");
    rv.render(model, request, response);
    assertEquals(303, response.getStatus());
    assertEquals("http://url.somewhere.com/path?id=1", response.getHeader("Location"));
    assertEquals("/path", flashMap.getTargetRequestPath());
    assertEquals(model, flashMap.getTargetRequestParams().toSingleValueMap());
}
Also used : FlashMap(org.springframework.web.servlet.FlashMap) ModelMap(org.springframework.ui.ModelMap) Test(org.junit.Test)

Example 35 with ModelMap

use of org.springframework.ui.ModelMap in project spring-framework by spring-projects.

the class RedirectAttributesMethodArgumentResolver method resolveArgument.

@Override
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
    ModelMap redirectAttributes;
    if (binderFactory != null) {
        DataBinder dataBinder = binderFactory.createBinder(webRequest, null, null);
        redirectAttributes = new RedirectAttributesModelMap(dataBinder);
    } else {
        redirectAttributes = new RedirectAttributesModelMap();
    }
    mavContainer.setRedirectModel(redirectAttributes);
    return redirectAttributes;
}
Also used : RedirectAttributesModelMap(org.springframework.web.servlet.mvc.support.RedirectAttributesModelMap) ModelMap(org.springframework.ui.ModelMap) DataBinder(org.springframework.validation.DataBinder) RedirectAttributesModelMap(org.springframework.web.servlet.mvc.support.RedirectAttributesModelMap)

Aggregations

ModelMap (org.springframework.ui.ModelMap)53 Test (org.junit.Test)23 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)23 ModelAndView (org.springframework.web.servlet.ModelAndView)11 RedirectAttributesModelMap (org.springframework.web.servlet.mvc.support.RedirectAttributesModelMap)9 ArrayList (java.util.ArrayList)8 HashMap (java.util.HashMap)6 ModelAndView (org.springframework.web.portlet.ModelAndView)5 Map (java.util.Map)4 HttpServletRequest (javax.servlet.http.HttpServletRequest)4 IOException (java.io.IOException)3 Date (java.util.Date)3 StudyBean (org.akaza.openclinica.bean.managestudy.StudyBean)3 HttpSessionRequiredException (org.springframework.web.HttpSessionRequiredException)3 NimbusClient (backtype.storm.utils.NimbusClient)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 ParseException (java.text.ParseException)2 SimpleDateFormat (java.text.SimpleDateFormat)2 PortletPreferences (javax.portlet.PortletPreferences)2 HttpServletResponse (javax.servlet.http.HttpServletResponse)2