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

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

Introduction

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

Prototype

public ModelMap getModelMap() 

Source Link

Document

Return the underlying ModelMap instance (never null ).

Usage

From source file:fragment.web.AbstractBaseControllerTest.java

@Test
public void testHandleGenericException() {
    Exception ex = new Exception("generic");
    MockHttpServletRequest request = new MockHttpServletRequest();
    ModelAndView mav = controller.handleGenericException(ex, request);
    Assert.assertNotNull(mav.getModelMap());
    ModelAndViewAssert.assertViewName(mav, "errors/error");
    ModelAndViewAssert.assertModelAttributeAvailable(mav, "errorMessage");
    ModelAndViewAssert.assertModelAttributeValue(mav, "errorMessage",
            messageSource.getMessage("ui.error.page.server.error.message", null, null));

    ex = new NullPointerException("NPE generic");
    mav = controller.handleGenericException(ex, request);
    Assert.assertNotNull(mav.getModelMap());
    ModelAndViewAssert.assertViewName(mav, "errors/error");
    ModelAndViewAssert.assertModelAttributeAvailable(mav, "errorMessage");
    ModelAndViewAssert.assertModelAttributeValue(mav, "errorMessage",
            messageSource.getMessage("ui.error.page.server.error.message", null, null));

    request.addHeader("X-Requested-With", "XMLHttpRequest");
    mav = controller.handleGenericException(ex, request);
    Assert.assertNotNull(mav.getModelMap());
    ModelAndViewAssert.assertViewName(mav, "errors.messagepage");
    ModelAndViewAssert.assertModelAttributeAvailable(mav, "errorMessage");
    ModelAndViewAssert.assertModelAttributeValue(mav, "errorMessage",
            messageSource.getMessage("ui.error.page.server.error.message", null, null));

    ex = new NullPointerException("NPE generic");
    mav = controller.handleGenericException(ex, request);
    Assert.assertNotNull(mav.getModelMap());
    ModelAndViewAssert.assertViewName(mav, "errors.messagepage");
    ModelAndViewAssert.assertModelAttributeAvailable(mav, "errorMessage");
    ModelAndViewAssert.assertModelAttributeValue(mav, "errorMessage",
            messageSource.getMessage("ui.error.page.server.error.message", null, null));

}

From source file:fragment.web.AbstractBaseControllerTest.java

@Test
public void testHandleServiceException() {
    IPtoCountryException ex = new IPtoCountryException("IPtoCountryException");
    ModelAndView mav = controller.handleServiceException(ex, new MockHttpServletRequest());
    Assert.assertNotNull(mav.getModelMap());
    ModelAndViewAssert.assertViewName(mav, "errors/serviceerror");
    ModelAndViewAssert.assertModelAttributeAvailable(mav, "serviceerror");
    ModelAndViewAssert.assertModelAttributeValue(mav, "serviceerror", ex.getMessage());

    CreditCardFraudCheckException cex = new CreditCardFraudCheckException("CreditCardFraudCheckException");
    mav = controller.handleServiceException(cex, new MockHttpServletRequest());
    Assert.assertNotNull(mav.getModelMap());
    ModelAndViewAssert.assertViewName(mav, "errors/serviceerror");
    ModelAndViewAssert.assertModelAttributeAvailable(mav, "serviceerror");
    ModelAndViewAssert.assertModelAttributeValue(mav, "serviceerror", cex.getMessage());

}

From source file:fragment.web.AbstractBaseControllerTest.java

@Test
public void testHandleInvalidAjaxRequestException() {
    InvalidAjaxRequestException ex = new InvalidAjaxRequestException("test");
    ModelAndView mav = controller.handleInvalidAjaxRequestException(ex, new MockHttpServletRequest());
    Assert.assertNotNull(mav.getModelMap());
    ModelAndViewAssert.assertViewName(mav, "errors.messagepage");
    ModelAndViewAssert.assertModelAttributeAvailable(mav, "errorMessage");
    ModelAndViewAssert.assertModelAttributeValue(mav, "errorMessage", ex.getMessage());

    ModelAndView mav1 = controller.handleInvalidAjaxRequestException(
            new InvalidAjaxRequestException(
                    new CreditCardFraudCheckException("exception", new Exception(), "10")),
            new MockHttpServletRequest());

    Assert.assertNotNull(mav1);/*w  w  w  .  j  a  v a  2s.  c  o  m*/
}

From source file:com.tms.controller.customer.CustomerController.java

@RequestMapping("/create")
public ModelAndView createCustomer(HttpServletResponse response, HttpServletRequest request) throws Exception {
    ModelAndView view = new ModelAndView();

    List<TimeZone> timeZones = timeZoneService.findAll();//
    view.getModelMap().put("timeZones", timeZones);

    List<OutFaceType> outFaceTypes = outFaceTypeService.findAll();//?
    List<OutFaceType> outFaceTypeArrayList = new ArrayList<OutFaceType>();
    List<OutFaceType> otherOutFaceTypeArrayList = new ArrayList<OutFaceType>();
    if (CollectionUtils.isNotEmpty(outFaceTypes)) {
        for (OutFaceType outfaceType : outFaceTypes) {
            if (outfaceType.isOther()) {
                otherOutFaceTypeArrayList.add(outfaceType);
            } else {
                outFaceTypeArrayList.add(outfaceType);
            }//  w w  w .  jav a 2 s . c  o m
        }
    }

    view.getModelMap().put("outFace", outFaceTypeArrayList);
    view.getModelMap().put("otherOutFace", otherOutFaceTypeArrayList);

    List<ReportManagement> reportManagements = reportManagementService.findAll();//?
    view.getModelMap().put("reportManagements", reportManagements);

    List<Features> features = featuresService.findAll();//?
    view.getModelMap().put("features", features);

    view.getModelMap().put("customerStatus", CompanyStatus.values());
    view.setViewName("admin/customer/savecustomer");
    return view;
}

From source file:fragment.web.AbstractBaseControllerTest.java

@Test
public void testHandleApiException() {
    MockHttpServletRequest request = new MockHttpServletRequest();
    ModelAndView mav = controller.handleApiException(new ApiException(), request);
    Assert.assertNotNull(mav.getModelMap());

    mav = controller.handleApiException(new CloudServiceExceptionWrapper(), request,
            new MockHttpServletResponse());
    Assert.assertNotNull(mav.getModelMap());

    mav = controller.handleApiException(new UpdateUserApiException(), request);
    Assert.assertNotNull(mav.getModelMap());
    Assert.assertNotNull(mav.getModelMap().get("EditUserResponse"));

    mav = controller.handleApiException(new UpdateTenantApiException(), request);
    Assert.assertNotNull(mav.getModelMap());
    Assert.assertNotNull(mav.getModelMap().get("EditTenantResponse"));

    mav = controller.handleApiException(new ChangeTenantStateApiException(), request);
    Assert.assertNotNull(mav.getModelMap());
    Assert.assertNotNull(mav.getModelMap().get("EditTenantStateResponse"));

    mav = controller.handleApiException(new VerifyUserExistenceApiException(), request);
    Assert.assertNotNull(mav.getModelMap());
    Assert.assertNotNull(mav.getModelMap().get("VerifyUserExistenceResponse"));

    mav = controller.handleApiException(new ListProductBundlesApiException(), request);
    Assert.assertNotNull(mav.getModelMap());
    Assert.assertNotNull(mav.getModelMap().get("ListProductBundlesResponse"));

    mav = controller.handleApiException(new SubscribeToProductBundleApiException(), request);
    Assert.assertNotNull(mav.getModelMap());
    Assert.assertNotNull(mav.getModelMap().get("SubscribeToProductBundleResponse"));

    mav = controller.handleApiException(new ListTenantsApiException(), request);
    Assert.assertNotNull(mav.getModelMap());
    Assert.assertNotNull(mav.getModelMap().get("ListTenantsResponse"));

    mav = controller.handleApiException(new FetchUserApiException(), request);
    Assert.assertNotNull(mav.getModelMap());
    Assert.assertNotNull(mav.getModelMap().get("FetchUserResponse"));

    mav = controller.handleApiException(new RegisterTenantApiException(), request);
    Assert.assertNotNull(mav.getModelMap());
    Assert.assertNotNull(mav.getModelMap().get("RegisterAccountResponse"));

    mav = controller.handleApiException(new ListSubscriptionsApiException(), request);
    Assert.assertNotNull(mav.getModelMap());
    Assert.assertNotNull(mav.getModelMap().get("ListSubscriptionsResponse"));
}

From source file:org.slc.sli.dashboard.util.DashboardExceptionResolver.java

@Override
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler,
        Exception ex) {//from w  w w. ja va 2  s  . c  om

    ModelAndView mv = super.resolveException(request, response, handler, ex);
    if (mv == null) {
        mv = new ModelAndView("error");
    }

    // Leverage ErrorController to display custom error page with Dashboard application exception details
    if ((ex instanceof DashboardException) && (ex.getMessage() != null)) {
        ErrorDescriptor error = ErrorDescriptor.DEFAULT;
        mv.getModelMap().addAttribute(Constants.ATTR_ERROR_HEADING, error.getHeading());
        mv.getModelMap().addAttribute(Constants.ATTR_ERROR_CONTENT, ex.getMessage());
    } else {

        // If not Dashboard application exception, then provide developer exception details
        String stackTrace = getStackTrace(ex);
        logger.error(stackTrace);
        if (logger.isDebugEnabled()) {
            ErrorDescriptor error = ErrorDescriptor.EXCEPTION;
            mv.getModelMap().addAttribute(Constants.ATTR_ERROR_HEADING, error.getHeading());
            mv.getModelMap().addAttribute(Constants.ATTR_ERROR_CONTENT, ex.getMessage());

            // If debug is enabled, then provide developer exception stack trace
            mv.getModelMap().addAttribute(Constants.ATTR_ERROR_DETAILS_ENABLED, true);
            mv.getModelMap().addAttribute(Constants.ATTR_ERROR_DETAILS, stackTrace);
        }
    }

    response.setStatus(500);

    setContextPath(mv.getModelMap(), request);
    addHeaderFooter(mv.getModelMap());

    return mv;
}

From source file:fragment.web.AbstractBaseControllerTest.java

@Test
public void testHandlePageNotFound() {
    String viewName = "errors/notfound";

    ModelAndView mav = controller.handlePageNotFound(new IllegalArgumentException(),
            new MockHttpServletRequest());
    Assert.assertNotNull(mav.getModelMap());
    ModelAndViewAssert.assertViewName(mav, viewName);

    mav = controller.handlePageNotFound(new NoSuchUserException(), new MockHttpServletRequest());
    Assert.assertNotNull(mav.getModelMap());
    ModelAndViewAssert.assertViewName(mav, viewName);

    mav = controller.handlePageNotFound(new NoSuchTenantException(), new MockHttpServletRequest());
    Assert.assertNotNull(mav.getModelMap());
    ModelAndViewAssert.assertViewName(mav, viewName);

    mav = controller.handlePageNotFound(new NoSuchDefinitionException(), new MockHttpServletRequest());
    Assert.assertNotNull(mav.getModelMap());
    ModelAndViewAssert.assertViewName(mav, viewName);

    mav = controller.handlePageNotFound(new UserAuthorizationInvalidException(), new MockHttpServletRequest());
    Assert.assertNotNull(mav.getModelMap());
    ModelAndViewAssert.assertViewName(mav, viewName);
}

From source file:fragment.web.AbstractBaseControllerTest.java

@Test
public void testHandleAjaxFormValidationException() {
    FieldError error = new FieldError("tenant", "teannt", "Invalid Tenant");
    List<ObjectError> lstError = new ArrayList<ObjectError>();
    lstError.add(error);//from w ww.  j  av a2  s.c o m
    BindingResult result = EasyMock.createMock(BindingResult.class);
    EasyMock.expect(result.getAllErrors()).andReturn(lstError).anyTimes();
    EasyMock.replay(result);
    Errors errors = new BindException(result);

    MockHttpServletResponse response = new MockHttpServletResponse();
    ModelAndView mav = controller.handleAjaxFormValidationException(new AjaxFormValidationException(errors),
            new MockHttpServletRequest(), response);
    Assert.assertNotNull(mav.getModelMap());
    Assert.assertEquals(420, response.getStatus());
    // ModelAndViewAssert.assertViewName(mav, expectedName);
}

From source file:fr.hoteia.qalingo.web.mvc.controller.catalog.ProductController.java

/**
 * /* w w w. ja  va 2s  .  com*/
 */
protected void initSpecificSeo(final HttpServletRequest request, final ModelAndView modelAndView,
        final String titleKeyPrefixSufix, String productName) throws Exception {
    final Localization currentLocalization = requestUtil.getCurrentLocalization(request);
    final Locale locale = currentLocalization.getLocale();

    String pageTitleKey = "header.title." + titleKeyPrefixSufix;
    String appName = (String) modelAndView.getModelMap().get(Constants.APP_NAME);
    Object[] params = { productName };
    String headerTitle = coreMessageSource.getMessage(pageTitleKey, params, locale);
    modelAndView.addObject("seoPageTitle", appName + " - " + headerTitle);
    modelAndView.addObject("mainContentTitle", headerTitle);
}

From source file:org.mitre.openid.connect.web.ClientAPI.java

/**
 * Delete a client//from ww w  . java2  s  .co m
 * @param id
 * @param modelAndView
 * @return
 */
@PreAuthorize("hasRole('ROLE_ADMIN')")
@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
public String apiDeleteClient(@PathVariable("id") Long id, ModelAndView modelAndView) {

    ClientDetailsEntity client = clientService.getClientById(id);

    if (client == null) {
        logger.error("apiDeleteClient failed; client with id " + id + " could not be found.");
        modelAndView.getModelMap().put(HttpCodeView.CODE, HttpStatus.NOT_FOUND);
        modelAndView.getModelMap().put(JsonErrorView.ERROR_MESSAGE,
                "Could not delete client. The requested client with id " + id + "could not be found.");
        return JsonErrorView.VIEWNAME;
    } else {
        modelAndView.getModelMap().put(HttpCodeView.CODE, HttpStatus.OK);
        clientService.deleteClient(client);
    }

    return HttpCodeView.VIEWNAME;
}