Search in sources :

Example 21 with FlashMap

use of org.springframework.web.servlet.FlashMap in project spring-framework by spring-projects.

the class PrintingResultHandlerTests method flashMap.

@Test
public void flashMap() throws Exception {
    FlashMap flashMap = new FlashMap();
    flashMap.put("attrName", "attrValue");
    this.request.setAttribute(DispatcherServlet.class.getName() + ".OUTPUT_FLASH_MAP", flashMap);
    this.handler.handle(this.mvcResult);
    assertValue("FlashMap", "Attribute", "attrName");
    assertValue("FlashMap", "value", "attrValue");
}
Also used : FlashMap(org.springframework.web.servlet.FlashMap) Test(org.junit.jupiter.api.Test)

Example 22 with FlashMap

use of org.springframework.web.servlet.FlashMap in project BroadleafCommerce by BroadleafCommerce.

the class AdminBasicEntityController method removeEntity.

/**
 * Attempts to remove the given entity.
 *
 * @param request
 * @param response
 * @param model
 * @param pathVars
 * @param id
 * @return the return view path
 * @throws Exception
 */
@RequestMapping(value = "/{id}/delete", method = RequestMethod.POST)
public String removeEntity(HttpServletRequest request, HttpServletResponse response, Model model, @PathVariable Map<String, String> pathVars, @PathVariable(value = "id") String id, @ModelAttribute(value = "entityForm") EntityForm entityForm, BindingResult result, RedirectAttributes ra) throws Exception {
    String sectionKey = getSectionKey(pathVars);
    String sectionClassName = getClassNameForSection(sectionKey);
    List<SectionCrumb> sectionCrumbs = getSectionCrumbs(request, sectionKey, id);
    String[] sectionCriteria = customCriteriaService.mergeSectionCustomCriteria(sectionClassName, getSectionCustomCriteria());
    Entity entity = service.removeEntity(entityForm, sectionCriteria, sectionCrumbs).getEntity();
    // Removal does not normally return an Entity unless there is some validation error
    if (entity != null) {
        entityFormValidator.validate(entityForm, entity, result);
        if (result.hasErrors()) {
            // Create a flash attribute for the unsuccessful delete
            FlashMap fm = new FlashMap();
            fm.put("headerFlash", "delete.unsuccessful");
            fm.put("headerFlashAlert", true);
            request.setAttribute(DispatcherServlet.OUTPUT_FLASH_MAP_ATTRIBUTE, fm);
            // Re-look back up the entity so that we can return something populated
            PersistencePackageRequest ppr = getSectionPersistencePackageRequest(sectionClassName, sectionCrumbs, pathVars);
            ClassMetadata cmd = service.getClassMetadata(ppr).getDynamicResultSet().getClassMetaData();
            entity = service.getRecord(ppr, id, cmd, false).getDynamicResultSet().getRecords()[0];
            Map<String, DynamicResultSet> subRecordsMap = service.getRecordsForAllSubCollections(ppr, entity, sectionCrumbs);
            entityForm.clearFieldsMap();
            formService.populateEntityForm(cmd, entity, subRecordsMap, entityForm, sectionCrumbs);
            modifyEntityForm(entityForm, pathVars);
            return populateJsonValidationErrors(entityForm, result, new JsonResponse(response)).done();
        }
    }
    ra.addFlashAttribute("headerFlash", "delete.successful");
    ra.addFlashAttribute("headerFlashAlert", true);
    if (isAjaxRequest(request)) {
        // redirect attributes won't work here since ajaxredirect actually makes a new request
        return "ajaxredirect:" + getContextPath(request) + sectionKey + "?headerFlash=delete.successful";
    } else {
        return "redirect:/" + sectionKey;
    }
}
Also used : SectionCrumb(org.broadleafcommerce.openadmin.dto.SectionCrumb) ClassMetadata(org.broadleafcommerce.openadmin.dto.ClassMetadata) Entity(org.broadleafcommerce.openadmin.dto.Entity) FlashMap(org.springframework.web.servlet.FlashMap) PersistencePackageRequest(org.broadleafcommerce.openadmin.server.domain.PersistencePackageRequest) DynamicResultSet(org.broadleafcommerce.openadmin.dto.DynamicResultSet) JsonResponse(org.broadleafcommerce.common.web.JsonResponse) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 23 with FlashMap

use of org.springframework.web.servlet.FlashMap in project spring-framework by spring-projects.

the class ExceptionHandlerExceptionResolverTests method setup.

@BeforeEach
public void setup() throws Exception {
    this.resolver = new ExceptionHandlerExceptionResolver();
    this.resolver.setWarnLogCategory(this.resolver.getClass().getName());
    this.request = new MockHttpServletRequest("GET", "/");
    this.request.setAttribute(DispatcherServlet.OUTPUT_FLASH_MAP_ATTRIBUTE, new FlashMap());
    this.response = new MockHttpServletResponse();
}
Also used : FlashMap(org.springframework.web.servlet.FlashMap) MockHttpServletRequest(org.springframework.web.testfixture.servlet.MockHttpServletRequest) MockHttpServletResponse(org.springframework.web.testfixture.servlet.MockHttpServletResponse) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 24 with FlashMap

use of org.springframework.web.servlet.FlashMap in project spring-framework by spring-projects.

the class RedirectViewTests method flashMap.

@Test
public void flashMap() throws Exception {
    RedirectView rv = new RedirectView();
    rv.setUrl("https://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);
    assertThat(response.getStatus()).isEqualTo(303);
    assertThat(response.getHeader("Location")).isEqualTo("https://url.somewhere.com/path?id=1");
    assertThat(flashMap.getTargetRequestPath()).isEqualTo("/path");
    assertThat(flashMap.getTargetRequestParams().toSingleValueMap()).isEqualTo(model);
}
Also used : FlashMap(org.springframework.web.servlet.FlashMap) ModelMap(org.springframework.ui.ModelMap) Test(org.junit.jupiter.api.Test)

Example 25 with FlashMap

use of org.springframework.web.servlet.FlashMap in project spring-framework by spring-projects.

the class FlashMapManagerTests method retrieveAndUpdateMatchWithMultiValueParam.

// SPR-8798
@Test
public void retrieveAndUpdateMatchWithMultiValueParam() {
    FlashMap flashMap = new FlashMap();
    flashMap.put("name", "value");
    flashMap.addTargetRequestParam("id", "1");
    flashMap.addTargetRequestParam("id", "2");
    this.flashMapManager.setFlashMaps(Arrays.asList(flashMap));
    this.request.setQueryString("id=1");
    FlashMap inputFlashMap = this.flashMapManager.retrieveAndUpdate(this.request, this.response);
    assertThatFlashMap(inputFlashMap).isNull();
    assertThat(this.flashMapManager.getFlashMaps().size()).as("FlashMap should not have been removed").isEqualTo(1);
    this.request.setQueryString("id=1&id=2");
    inputFlashMap = this.flashMapManager.retrieveAndUpdate(this.request, this.response);
    assertThatFlashMap(inputFlashMap).isEqualTo(flashMap);
    assertThat(this.flashMapManager.getFlashMaps().size()).as("Input FlashMap should have been removed").isEqualTo(0);
}
Also used : FlashMap(org.springframework.web.servlet.FlashMap) Test(org.junit.jupiter.api.Test)

Aggregations

FlashMap (org.springframework.web.servlet.FlashMap)30 Test (org.junit.jupiter.api.Test)20 SessionFlashMapManager (org.springframework.web.servlet.support.SessionFlashMapManager)5 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)5 MockHttpServletResponse (org.springframework.web.testfixture.servlet.MockHttpServletResponse)5 BeforeEach (org.junit.jupiter.api.BeforeEach)3 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)2 HandlerMethod (org.springframework.web.method.HandlerMethod)2 FlashMapManager (org.springframework.web.servlet.FlashMapManager)2 ModelAndView (org.springframework.web.servlet.ModelAndView)2 ArrayList (java.util.ArrayList)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 Cookie (javax.servlet.http.Cookie)1 JsonResponse (org.broadleafcommerce.common.web.JsonResponse)1 ClassMetadata (org.broadleafcommerce.openadmin.dto.ClassMetadata)1 DynamicResultSet (org.broadleafcommerce.openadmin.dto.DynamicResultSet)1 Entity (org.broadleafcommerce.openadmin.dto.Entity)1 SectionCrumb (org.broadleafcommerce.openadmin.dto.SectionCrumb)1 PersistencePackageRequest (org.broadleafcommerce.openadmin.server.domain.PersistencePackageRequest)1 Test (org.junit.Test)1