Example usage for org.springframework.web.servlet.support RequestContextUtils getOutputFlashMap

List of usage examples for org.springframework.web.servlet.support RequestContextUtils getOutputFlashMap

Introduction

In this page you can find the example usage for org.springframework.web.servlet.support RequestContextUtils getOutputFlashMap.

Prototype

public static FlashMap getOutputFlashMap(HttpServletRequest request) 

Source Link

Document

Return "output" FlashMap to save attributes for request after redirect.

Usage

From source file:org.sarons.spring4me.web.servlet.DispatcherServlet.java

private void prepareFlashMapForWidget(HttpServletRequest request, Object handler) {
    if (handler instanceof HandlerMethod) {
        HandlerMethod handlerMethod = (HandlerMethod) handler;
        ////from   w  w  w.java  2 s  . com
        Widget widget = AnnotationUtils.getAnnotation(handlerMethod.getBeanType(), Widget.class);
        if (widget != null) {
            //
            String widgetName = widget.value();
            if (!StringUtils.hasText(widgetName)) {
                String targetWidget = handlerMethod.getBeanType().getSimpleName();
                widgetName = StringUtils.uncapitalize(targetWidget);
            }
            //
            FlashMap flashMap = RequestContextUtils.getOutputFlashMap(request);
            flashMap.setTargetRequestPath(widgetName);
            FlashMapManager flashMapManager = RequestContextUtils.getFlashMapManager(request);
            flashMapManager.saveOutputFlashMap(flashMap, request, null);
        }
    }
}

From source file:org.wallride.web.controller.admin.category.CategoryRestController.java

@RequestMapping(value = "/{language}/categories/{id}", method = RequestMethod.DELETE)
public @ResponseBody DomainObjectDeletedModel<Long> delete(@PathVariable String language, @PathVariable long id,
        AuthorizedUser authorizedUser, HttpServletRequest request, HttpServletResponse response)
        throws BindException {
    Category category = categoryService.deleteCategory(id, language);
    FlashMap flashMap = RequestContextUtils.getOutputFlashMap(request);
    flashMap.put("deletedCategory", category);
    RequestContextUtils.getFlashMapManager(request).saveOutputFlashMap(flashMap, request, response);
    return new DomainObjectDeletedModel<>(category);
}

From source file:org.wallride.web.controller.admin.page.PageRestController.java

@RequestMapping(value = "/{language}/pages/{id}", method = RequestMethod.DELETE)
public @ResponseBody DomainObjectDeletedModel<Long> delete(@PathVariable String language, @PathVariable long id,
        AuthorizedUser authorizedUser, HttpServletRequest request, HttpServletResponse response)
        throws BindException {
    Page page = pageService.deletePage(id, language);
    FlashMap flashMap = RequestContextUtils.getOutputFlashMap(request);
    flashMap.put("deletedPage", page);
    RequestContextUtils.getFlashMapManager(request).saveOutputFlashMap(flashMap, request, response);
    return new DomainObjectDeletedModel<>(page);
}

From source file:org.wallride.web.controller.admin.tag.TagRestController.java

@RequestMapping(value = "/{language}/tags/merge", method = RequestMethod.POST)
public @ResponseBody DomainObjectSavedModel merge(@Valid TagMergeForm form, BindingResult errors,
        AuthorizedUser authorizedUser, HttpServletRequest request, HttpServletResponse response)
        throws BindException {
    if (errors.hasErrors()) {
        throw new BindException(errors);
    }//from ww  w .j  a v a  2s.c om

    Tag mergedTag;
    try {
        mergedTag = tagService.mergeTags(form.toTagMergeRequest(), authorizedUser);
    } catch (DuplicateNameException e) {
        errors.rejectValue("name", "NotDuplicate");
        throw new BindException(errors);
    }

    FlashMap flashMap = RequestContextUtils.getOutputFlashMap(request);
    flashMap.put("mergedTag", mergedTag);
    RequestContextUtils.getFlashMapManager(request).saveOutputFlashMap(flashMap, request, response);
    return new DomainObjectSavedModel<>(mergedTag);
}

From source file:com.epam.training.storefront.controllers.pages.AbstractRegisterPageController.java

/**
 * This method takes data from the registration form and create a new customer account and attempts to log in using
 * the credentials of this new user./* ww  w.  j  a va 2s  .  co m*/
 * 
 * @param referer
 * @param form
 * @param bindingResult
 * @param model
 * @param request
 * @param response
 * @return true if there are no binding errors or the account does not already exists.
 * @throws CMSItemNotFoundException
 */
protected String processRegisterUserRequest(final String referer, final RegisterForm form,
        final BindingResult bindingResult, final Model model, final HttpServletRequest request,
        final HttpServletResponse response) throws CMSItemNotFoundException {
    if (bindingResult.hasErrors()) {
        model.addAttribute(new LoginForm());
        GlobalMessages.addErrorMessage(model, "form.global.error");
        return handleRegistrationError(model);
    }

    final RegisterData data = new RegisterData();
    data.setFirstName(form.getFirstName());
    data.setLastName(form.getLastName());
    data.setLogin(form.getEmail());
    data.setPassword(form.getPwd());
    data.setTitleCode(form.getTitleCode());
    try {
        getCustomerFacade().register(data);
        getAutoLoginStrategy().login(form.getEmail(), form.getPwd(), request, response);

        getSubscriptionFacade().updateProfile(new HashMap<String, String>());

        RequestContextUtils.getOutputFlashMap(request).put(GlobalMessages.CONF_MESSAGES_HOLDER,
                Collections.singletonList("registration.confirmation.message.title"));
    } catch (final DuplicateUidException e) {
        LOG.warn("registration failed: " + e);
        model.addAttribute(new LoginForm());
        bindingResult.rejectValue("email", "registration.error.account.exists.title");
        GlobalMessages.addErrorMessage(model, "form.global.error");
        return handleRegistrationError(model);
    } catch (final SubscriptionFacadeException e) {
        LOG.warn(String.format("Creating new subscription billing profile for user %s failed", form.getEmail()),
                e);
        model.addAttribute(new LoginForm());
        GlobalMessages.addErrorMessage(model, "registration.error.subscription.billing.profil");
        return handleRegistrationError(model);
    }

    return REDIRECT_PREFIX + getSuccessRedirect(request, response);
}

From source file:com.epam.cme.storefront.controllers.pages.AbstractRegisterPageController.java

/**
 * This method takes data from the registration form and create a new customer account and
 * attempts to log in using the credentials of this new user.
 * /*  www .  ja v a  2 s. co m*/
 * @param referer
 * @param form
 * @param bindingResult
 * @param model
 * @param request
 * @param response
 * @return true if there are no binding errors or the account does not already exists.
 * @throws CMSItemNotFoundException
 */
protected String processRegisterUserRequest(final String referer, final RegisterForm form,
        final BindingResult bindingResult, final Model model, final HttpServletRequest request,
        final HttpServletResponse response) throws CMSItemNotFoundException {
    if (bindingResult.hasErrors()) {
        model.addAttribute(new LoginForm());
        GlobalMessages.addErrorMessage(model, "form.global.error");
        return handleRegistrationError(model);
    }
    final RegisterData data = new RegisterData();
    data.setFirstName(form.getFirstName());
    data.setLastName(form.getLastName());
    data.setLogin(form.getEmail());
    data.setPassword(form.getPwd());
    data.setTitleCode(form.getTitleCode());
    try {
        getCustomerFacade().register(data);
        getAutoLoginStrategy().login(form.getEmail(), form.getPwd(), request, response);
        getSubscriptionFacade().updateProfile(new HashMap<String, String>());
        RequestContextUtils.getOutputFlashMap(request).put(GlobalMessages.CONF_MESSAGES_HOLDER,
                Collections.singletonList("registration.confirmation.message.title"));
    } catch (final DuplicateUidException e) {
        LOG.warn("registration failed: " + e);
        model.addAttribute(new LoginForm());
        bindingResult.rejectValue("email", "registration.error.account.exists.title");
        GlobalMessages.addErrorMessage(model, "form.global.error");
        return handleRegistrationError(model);
    } catch (final SubscriptionFacadeException e) {
        LOG.warn(String.format("Creating new subscription billing profile for user %s failed", form.getEmail()),
                e);
        model.addAttribute(new LoginForm());
        GlobalMessages.addErrorMessage(model, "registration.error.subscription.billing.profil");
        return handleRegistrationError(model);
    }
    return REDIRECT_PREFIX + getSuccessRedirect(request, response);
}

From source file:de.iteratec.iteraplan.presentation.dialog.GuiController.java

/**
 * Add a new error message in any request of the redirect-chain.
 * Call populateErrorMessages() after the last possible error message
 * to make the messages available in "ErrorMessagesMVC.jsp"
 * //from ww  w  . j a  va2 s.c  o  m
 * Concerning the usage of FlashMap/-Attributes, please refer to comment in
 * "springmvc-servlet.xml", bean "DefaultAnnotationHandlerMapping".
 * 
 * @param request  The {@link HttpServletRequest}
 * @param message  A new error message
 */
@SuppressWarnings("unchecked")
protected void addErrorMessage(HttpServletRequest request, String message) {
    FlashMap outputFlashMap = RequestContextUtils.getOutputFlashMap(request);
    List<String> errorMessages;
    if (outputFlashMap.containsKey(SessionConstants.MVC_ERROR_MESSAGES_KEY)) {
        errorMessages = (List<String>) outputFlashMap.get(SessionConstants.MVC_ERROR_MESSAGES_KEY);
    } else {
        errorMessages = Lists.newArrayList();
        outputFlashMap.put(SessionConstants.MVC_ERROR_MESSAGES_KEY, errorMessages);
    }
    errorMessages.add(message);
}

From source file:org.kmnet.com.fw.web.exception.SystemExceptionResolver.java

/**
 * Sets exception code in {@code HttpServletRequest} and {@code HttpServletResponse} header.
 * <p>/* ww w  .ja v  a2s .  c om*/
 * Sets exception code in {@code HttpServletRequest} and {@code HttpServletResponse} header. If exceptionCodeAttribute is
 * {@code null} or blank or space is set, then exception code is not set.
 * </p>
 * @param ex Exception
 * @param request {@link HttpServletRequest}
 * @param response {@link HttpServletResponse}
 */
protected void setExceptionCode(Exception ex, HttpServletRequest request, HttpServletResponse response) {

    String exceptionCode = null;
    if (exceptionCodeResolver != null
            && (StringUtils.hasText(exceptionCodeAttribute) || StringUtils.hasText(exceptionCodeHeader))) {
        exceptionCode = exceptionCodeResolver.resolveExceptionCode(ex);
    }

    if (exceptionCode == null) {
        return;
    }

    if (StringUtils.hasText(exceptionCodeAttribute)) {
        request.setAttribute(exceptionCodeAttribute, exceptionCode);
        FlashMap flashMap = RequestContextUtils.getOutputFlashMap(request);
        if (flashMap != null) {
            flashMap.put(exceptionCodeAttribute, exceptionCode);
        }
    }

    if (StringUtils.hasText(exceptionCodeHeader)) {
        response.setHeader(exceptionCodeHeader, exceptionCode);
    }

}

From source file:org.terasoluna.gfw.web.exception.SystemExceptionResolverTest.java

@Test
public void testSetResultMessages_BusinessException_flashMap_is_null() {

    ResultMessages resultMessages = ResultMessages.error().add("code01");
    BusinessException occurException = new BusinessException(resultMessages);

    testTarget.setResultMessages(occurException, mockRequest);

    FlashMap flashMap = RequestContextUtils.getOutputFlashMap(mockRequest);

    assertThat(flashMap, is(nullValue()));

}

From source file:com.acc.storefront.controllers.misc.StoreSessionController.java

@ExceptionHandler(UnknownIdentifierException.class)
public String handleUnknownIdentifierException(final UnknownIdentifierException exception,
        final HttpServletRequest request) {
    final Map<String, Object> currentFlashScope = RequestContextUtils.getOutputFlashMap(request);
    currentFlashScope.put(GlobalMessages.ERROR_MESSAGES_HOLDER, exception.getMessage());
    return REDIRECT_PREFIX + "/404";
}