Example usage for org.springframework.web.servlet ModelAndView getModel

List of usage examples for org.springframework.web.servlet ModelAndView getModel

Introduction

In this page you can find the example usage for org.springframework.web.servlet ModelAndView getModel.

Prototype

public Map<String, Object> getModel() 

Source Link

Document

Return the model map.

Usage

From source file:org.jasig.cas.services.web.ManageRegisteredServicesMultiActionControllerTests.java

public void testManage() {
    final RegisteredServiceImpl r = new RegisteredServiceImpl();
    r.setId(1200);//from w w  w  .j a va  2s.  co  m
    r.setName("name");
    r.setServiceId("test");
    r.setEvaluationOrder(2);

    this.servicesManager.save(r);

    final ModelAndView modelAndView = this.controller.manage(new MockHttpServletRequest(),
            new MockHttpServletResponse());

    assertNotNull(modelAndView);
    assertEquals("manageServiceView", modelAndView.getViewName());

    final Collection c = (Collection) modelAndView.getModel().get("services");
    assertTrue(c.contains(r));
}

From source file:de.hybris.telcotrail.storefront.interceptors.beforeview.CmsPageBeforeViewHandler.java

@Override
public void beforeView(final HttpServletRequest request, final HttpServletResponse response,
        final ModelAndView modelAndView) {
    modelAndView.addObject("cmsSite", cmsSiteService.getCurrentSite());

    // Look for the page in the model
    AbstractPageModel page = (AbstractPageModel) modelAndView.getModel()
            .get(AbstractPageController.CMS_PAGE_MODEL);
    if (page != null) {
        final AbstractPageModel previewPage = lookupPreviewPage(request);
        if (previewPage != null && !previewPage.equals(page)) {
            // Have a preview page that overrides the current page

            // Check that the preview page is the same type as the expected page
            if (!page.getClass().isInstance(previewPage)) {
                LOG.error("Preview page is of type [" + previewPage.getClass().getName()
                        + "] expected page of type [" + page.getClass().getName() + "]");
            } else {
                // Push the preview page into the model
                LOG.info("Replaced page [" + page + "] with preview page [" + previewPage + "]");
                modelAndView.addObject(AbstractPageController.CMS_PAGE_MODEL, previewPage);

                // Check to see if we are using the default view for the page
                if (modelAndView.getViewName() != null
                        && modelAndView.getViewName().equals(getViewForPage(page))) {
                    final String viewForPreviewPage = getViewForPage(previewPage);
                    if (viewForPreviewPage != null && !viewForPreviewPage.equals(modelAndView.getViewName())) {
                        // Change the view name
                        LOG.info("Changing view from [" + modelAndView.getViewName() + "] to preview view ["
                                + viewForPreviewPage + "]");
                        modelAndView.setViewName(viewForPreviewPage);
                    }//from   ww w.jav  a  2s . c  o  m
                }

                page = previewPage;
            }
        }

        // Add the page to the request attribute "currentPage" which is then picked up by the CMSBodyTag, and then passed over to the CMS Cockpit
        request.setAttribute("currentPage", page);

        // Add the content slots to the page as a map of slots - keyed by position
        modelAndView.addObject(SLOTS_MODEL, getContentSlotsForPageAsMap(page));
    }
}

From source file:ch.silviowangler.dox.web.DocumentControllerTest.java

@Test
public void editWhenThrowsException() throws Exception {

    when(documentService.findDocumentReference(1L)).thenThrow(new DocumentNotFoundException(1L));

    final ModelAndView modelAndView = controller.editDocument(1L);

    assertThat(modelAndView.getViewName(), is("edit.doc"));
    assertThat(modelAndView.getModel().isEmpty(), is(true));
}

From source file:se.vgregion.urlservice.controllers.BookmarkControllerTest.java

@Test
public void redirectWithExistingHash() throws IOException {
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();

    ModelAndView mav = controller.redirect("foo", user.getUsername(), request, response);

    Assert.assertEquals(301, response.getStatus());
    Assert.assertEquals("http://example.com", response.getHeader("Location"));
    Assert.assertEquals("http://example.com", mav.getModel().get("longUrl"));
}

From source file:com.ge.predix.acs.commons.web.RestErrorHandlerTest.java

@Test
public void testRestApiException() {
    RestErrorHandler errorHandler = new RestErrorHandler();

    HttpServletRequest request = new MockHttpServletRequest();
    HttpServletResponse response = new MockHttpServletResponse();
    Exception e = new RestApiException(HttpStatus.NOT_ACCEPTABLE, "NOT_ACCEPTABLE",
            "Not acceptable Error Message");

    ModelAndView errorResponse = errorHandler.createApiErrorResponse(e, request, response);

    // Custom error status code 406
    Assert.assertEquals(response.getStatus(), HttpStatus.NOT_ACCEPTABLE.value());

    Assert.assertNotNull(errorResponse);
    Assert.assertNotNull(errorResponse.getModel().get("ErrorDetails"));

    // Response payload with customer error code and message
    assertRestApiErrorResponse(errorResponse, "NOT_ACCEPTABLE", "Not acceptable Error Message");

}

From source file:fm.last.citrine.web.TaskFormControllerTest.java

@Test
public void testDelete() {
    Task task = new Task();
    task.setId(345);/*from   w  w w .ja v a 2  s . c  om*/
    TaskDTO dto = new TaskDTO(task);
    when(mockTaskManager.get(task.getId())).thenReturn(task);
    BindException bindException = new BindException(dto, "bla");
    mockRequest.addParameter(Constants.PARAM_DELETE, "true");
    ModelAndView modelAndView = taskFormController.onSubmit(mockRequest, mockResponse, dto, bindException);
    RedirectView view = (RedirectView) modelAndView.getView();
    assertEquals("tasks.do?selectedGroupName=" + Constants.GROUP_NAME_ALL, view.getUrl());
    verify(mockTaskManager).delete(task);
    assertTrue(modelAndView.getModel().isEmpty());
}

From source file:com.epam.cme.storefront.interceptors.beforeview.CmsPageBeforeViewHandler.java

@Override
public void beforeView(final HttpServletRequest request, final HttpServletResponse response,
        final ModelAndView modelAndView) {
    modelAndView.addObject("cmsSite", cmsSiteService.getCurrentSite());

    // Look for the page in the model
    AbstractPageModel page = (AbstractPageModel) modelAndView.getModel()
            .get(AbstractPageController.CMS_PAGE_MODEL);
    if (page != null) {
        final AbstractPageModel previewPage = lookupPreviewPage(request);
        if (previewPage != null && !previewPage.equals(page)) {
            // Have a preview page that overrides the current page

            // Check that the preview page is the same type as the expected page
            if (!page.getClass().isInstance(previewPage)) {
                LOG.error("Preview page is of type [" + previewPage.getClass().getName()
                        + "] expected page of type [" + page.getClass().getName() + "]");
            } else {
                // Push the preview page into the model
                LOG.info("Replaced page [" + page + "] with preview page [" + previewPage + "]");
                modelAndView.addObject(AbstractPageController.CMS_PAGE_MODEL, previewPage);

                // Check to see if we are using the default view for the page
                if (modelAndView.getViewName() != null
                        && modelAndView.getViewName().equals(getViewForPage(page))) {
                    final String viewForPreviewPage = getViewForPage(previewPage);
                    if (viewForPreviewPage != null && !viewForPreviewPage.equals(modelAndView.getViewName())) {
                        // Change the view name
                        LOG.info("Changing view from [" + modelAndView.getViewName() + "] to preview view ["
                                + viewForPreviewPage + "]");
                        modelAndView.setViewName(viewForPreviewPage);
                    }//from   ww w.  jav  a 2  s .c  om
                }

                page = previewPage;
            }
        }

        // Add the page to the request attribute "currentPage" which is then picked up by the
        // CMSBodyTag, and then passed over to the CMS Cockpit
        request.setAttribute("currentPage", page);

        // Add the content slots to the page as a map of slots - keyed by position
        modelAndView.addObject(SLOTS_MODEL, getContentSlotsForPageAsMap(page));
    }
}

From source file:no.dusken.common.plugin.control.admin.PluginStoreControllerTest.java

@Test
public void testRequest() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest("GET", "/pluginstore");
    MockHttpServletResponse response = new MockHttpServletResponse();

    ModelAndView mav = pluginStoreController.handleRequestInternal(request, response);
    Map<DuskenPlugin, Map<String, String>> plugins = (Map<DuskenPlugin, Map<String, String>>) mav.getModel()
            .get("plugins");
    assertNotNull("Pluginsmap was null", plugins);
    assertEquals("Pluginsmap contained wrong number of plugins", 2, plugins.size());
    for (DuskenPlugin p : plugins.keySet()) {
        Map<String, String> settings = plugins.get(p);
        assertEquals("Wrong number of settings", 2, settings.size());
    }//from ww w. j a  va2 s .com
    assertNotNull("Map did not contain locale", mav.getModel().get("locale"));
}

From source file:com.lc.storefront.interceptors.beforeview.CmsPageBeforeViewHandler.java

protected AbstractPageModel updateCmsPageInModelAndView(final HttpServletRequest request,
        final ModelAndView modelAndView) {
    // Look for the page in the model
    final AbstractPageModel requestedPage = (AbstractPageModel) modelAndView.getModel()
            .get(AbstractPageController.CMS_PAGE_MODEL);
    if (requestedPage != null) {
        final AbstractPageModel previewPage = lookupPreviewPage(request);
        if (previewPage != null && !previewPage.equals(requestedPage)) {
            // Have a preview page that overrides the current page

            // Check that the preview page is the same type as the expected
            // page
            if (!requestedPage.getClass().isInstance(previewPage)) {
                LOG.error("Preview page is of type [" + previewPage.getClass().getName()
                        + "] expected page of type [" + requestedPage.getClass().getName() + "]");
            } else {
                // Push the preview page into the model
                LOG.info("Replaced page [" + requestedPage + "] with preview page [" + previewPage + "]");
                modelAndView.addObject(AbstractPageController.CMS_PAGE_MODEL, previewPage);

                // Check to see if we are using the default view for the
                // page
                if (modelAndView.getViewName() != null
                        && modelAndView.getViewName().equals(getViewForPage(requestedPage))) {
                    final String viewForPreviewPage = getViewForPage(previewPage);
                    if (viewForPreviewPage != null && !viewForPreviewPage.equals(modelAndView.getViewName())) {
                        // Change the view name
                        LOG.info("Changing view from [" + modelAndView.getViewName() + "] to preview view ["
                                + viewForPreviewPage + "]");
                        modelAndView.setViewName(viewForPreviewPage);
                    }/* w  w  w  .ja va2  s . c  o  m*/
                }

                return previewPage;
            }
        }
    }
    return requestedPage;
}

From source file:com.ctc.storefront.interceptors.beforeview.CmsPageBeforeViewHandler.java

protected AbstractPageModel updateCmsPageInModelAndView(final HttpServletRequest request,
        final ModelAndView modelAndView) {
    // Look for the page in the model
    final AbstractPageModel requestedPage = (AbstractPageModel) modelAndView.getModel()
            .get(AbstractPageController.CMS_PAGE_MODEL);
    if (requestedPage != null) {
        final AbstractPageModel previewPage = lookupPreviewPage(request);
        if (previewPage != null && !previewPage.equals(requestedPage)) {
            // Have a preview page that overrides the current page

            // Check that the preview page is the same type as the expected page
            if (!requestedPage.getClass().isInstance(previewPage)) {
                LOG.error("Preview page is of type [" + previewPage.getClass().getName()
                        + "] expected page of type [" + requestedPage.getClass().getName() + "]");
            } else {
                // Push the preview page into the model
                LOG.info("Replaced page [" + requestedPage + "] with preview page [" + previewPage + "]");
                modelAndView.addObject(AbstractPageController.CMS_PAGE_MODEL, previewPage);

                assignViewForPreviewPage(modelAndView, requestedPage, previewPage);

                return previewPage;
            }//from   ww  w . j a va2s .c om
        }
    }
    return requestedPage;
}