org.springframework.web.servlet.FlashMap Java Examples

The following examples show how to use org.springframework.web.servlet.FlashMap. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: FlashMapManagerTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void retrieveAndUpdateSortMultipleMatches() {
	FlashMap emptyFlashMap = new FlashMap();

	FlashMap flashMapOne = new FlashMap();
	flashMapOne.put("key1", "value1");
	flashMapOne.setTargetRequestPath("/one");

	FlashMap flashMapTwo = new FlashMap();
	flashMapTwo.put("key1", "value1");
	flashMapTwo.put("key2", "value2");
	flashMapTwo.setTargetRequestPath("/one/two");

	this.flashMapManager.setFlashMaps(Arrays.asList(emptyFlashMap, flashMapOne, flashMapTwo));

	this.request.setRequestURI("/one/two");
	FlashMap inputFlashMap = this.flashMapManager.retrieveAndUpdate(this.request, this.response);

	assertEquals(flashMapTwo, inputFlashMap);
	assertEquals("Input FlashMap should have been removed", 2, this.flashMapManager.getFlashMaps().size());
}
 
Example #2
Source File: RedirectView.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * Convert model to request parameters and redirect to the given URL.
 * @see #appendQueryProperties
 * @see #sendRedirect
 */
@Override
protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request,
		HttpServletResponse response) throws IOException {

	String targetUrl = createTargetUrl(model, request);
	targetUrl = updateTargetUrl(targetUrl, model, request, response);

	FlashMap flashMap = RequestContextUtils.getOutputFlashMap(request);
	if (!CollectionUtils.isEmpty(flashMap)) {
		UriComponents uriComponents = UriComponentsBuilder.fromUriString(targetUrl).build();
		flashMap.setTargetRequestPath(uriComponents.getPath());
		flashMap.addTargetRequestParams(uriComponents.getQueryParams());
		FlashMapManager flashMapManager = RequestContextUtils.getFlashMapManager(request);
		if (flashMapManager == null) {
			throw new IllegalStateException("FlashMapManager not found despite output FlashMap having been set");
		}
		flashMapManager.saveOutputFlashMap(flashMap, request, response);
	}

	sendRedirect(request, response, targetUrl, this.http10Compatible);
}
 
Example #3
Source File: FlashMapManagerTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test // SPR-15505
public void retrieveAndUpdateMatchByOriginatingPathAndQueryString() {
	FlashMap flashMap = new FlashMap();
	flashMap.put("key", "value");
	flashMap.setTargetRequestPath("/accounts");
	flashMap.addTargetRequestParam("a", "b");

	this.flashMapManager.setFlashMaps(Collections.singletonList(flashMap));

	this.request.setAttribute(WebUtils.FORWARD_REQUEST_URI_ATTRIBUTE, "/accounts");
	this.request.setAttribute(WebUtils.FORWARD_QUERY_STRING_ATTRIBUTE, "a=b");
	this.request.setRequestURI("/mvc/accounts");
	this.request.setQueryString("x=y");
	FlashMap inputFlashMap = this.flashMapManager.retrieveAndUpdate(this.request, this.response);

	assertEquals(flashMap, inputFlashMap);
	assertEquals("Input FlashMap should have been removed", 0, this.flashMapManager.getFlashMaps().size());
}
 
Example #4
Source File: FlashMapManagerTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void flashAttributesWithQueryParamsWithSpace() throws Exception {

	String encodedValue = URLEncoder.encode("1 2", "UTF-8");

	FlashMap flashMap = new FlashMap();
	flashMap.put("key", "value");
	flashMap.setTargetRequestPath("/path");
	flashMap.addTargetRequestParam("param", encodedValue);

	this.request.setCharacterEncoding("UTF-8");
	this.flashMapManager.saveOutputFlashMap(flashMap, this.request, this.response);

	MockHttpServletRequest requestAfterRedirect = new MockHttpServletRequest("GET", "/path");
	requestAfterRedirect.setQueryString("param=" + encodedValue);
	requestAfterRedirect.addParameter("param", "1 2");

	flashMap = this.flashMapManager.retrieveAndUpdate(requestAfterRedirect, new MockHttpServletResponse());
	assertNotNull(flashMap);
	assertEquals(1, flashMap.size());
	assertEquals("value", flashMap.get("key"));
}
 
Example #5
Source File: TagRestController.java    From wallride with Apache License 2.0 6 votes vote down vote up
@RequestMapping(value="/{language}/tags", method=RequestMethod.POST)
public @ResponseBody DomainObjectSavedModel save(
		@Valid TagCreateForm form,
		BindingResult errors,
		AuthorizedUser authorizedUser,
		HttpServletRequest request,
		HttpServletResponse response) throws BindException {
	if (errors.hasErrors()) {
		throw new BindException(errors);
	}

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

	FlashMap flashMap = RequestContextUtils.getOutputFlashMap(request);
	flashMap.put("savedTag", savedTag);
	RequestContextUtils.getFlashMapManager(request).saveOutputFlashMap(flashMap, request, response);
	return new DomainObjectSavedModel<>(savedTag);
}
 
Example #6
Source File: CategoryRestController.java    From wallride with Apache License 2.0 6 votes vote down vote up
@RequestMapping(value="/{language}/categories", method=RequestMethod.POST)
public @ResponseBody DomainObjectSavedModel save(
		@Valid CategoryCreateForm form,
		BindingResult result,
		AuthorizedUser authorizedUser,
		HttpServletRequest request,
		HttpServletResponse response) throws BindException {
	if (result.hasErrors()) {
		throw new BindException(result);
	}
	Category category = categoryService.createCategory(form.buildCategoryCreateRequest(), authorizedUser);
	FlashMap flashMap = RequestContextUtils.getOutputFlashMap(request);
	flashMap.put("savedCategory", category);
	RequestContextUtils.getFlashMapManager(request).saveOutputFlashMap(flashMap, request, response);
	return new DomainObjectSavedModel<>(category);
}
 
Example #7
Source File: FlashMapManagerTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void retrieveAndUpdateSortMultipleMatches() {
	FlashMap emptyFlashMap = new FlashMap();

	FlashMap flashMapOne = new FlashMap();
	flashMapOne.put("key1", "value1");
	flashMapOne.setTargetRequestPath("/one");

	FlashMap flashMapTwo = new FlashMap();
	flashMapTwo.put("key1", "value1");
	flashMapTwo.put("key2", "value2");
	flashMapTwo.setTargetRequestPath("/one/two");

	this.flashMapManager.setFlashMaps(Arrays.asList(emptyFlashMap, flashMapOne, flashMapTwo));

	this.request.setRequestURI("/one/two");
	FlashMap inputFlashMap = this.flashMapManager.retrieveAndUpdate(this.request, this.response);

	assertEquals(flashMapTwo, inputFlashMap);
	assertEquals("Input FlashMap should have been removed", 2, this.flashMapManager.getFlashMaps().size());
}
 
Example #8
Source File: RequestMappingHandlerAdapterTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void setAlwaysUseRedirectAttributes() throws Exception {
	HandlerMethodArgumentResolver redirectAttributesResolver = new RedirectAttributesMethodArgumentResolver();
	HandlerMethodArgumentResolver modelResolver = new ModelMethodProcessor();
	HandlerMethodReturnValueHandler viewHandler = new ViewNameMethodReturnValueHandler();

	this.handlerAdapter.setArgumentResolvers(Arrays.asList(redirectAttributesResolver, modelResolver));
	this.handlerAdapter.setReturnValueHandlers(Collections.singletonList(viewHandler));
	this.handlerAdapter.setIgnoreDefaultModelOnRedirect(true);
	this.handlerAdapter.afterPropertiesSet();

	this.request.setAttribute(DispatcherServlet.OUTPUT_FLASH_MAP_ATTRIBUTE, new FlashMap());

	HandlerMethod handlerMethod = handlerMethod(new RedirectAttributeController(), "handle", Model.class);
	ModelAndView mav = this.handlerAdapter.handle(request, response, handlerMethod);

	assertTrue("Without RedirectAttributes arg, model should be empty", mav.getModel().isEmpty());
}
 
Example #9
Source File: RequestMappingHandlerAdapterTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void setAlwaysUseRedirectAttributes() throws Exception {
	HandlerMethodArgumentResolver redirectAttributesResolver = new RedirectAttributesMethodArgumentResolver();
	HandlerMethodArgumentResolver modelResolver = new ModelMethodProcessor();
	HandlerMethodReturnValueHandler viewHandler = new ViewNameMethodReturnValueHandler();

	this.handlerAdapter.setArgumentResolvers(Arrays.asList(redirectAttributesResolver, modelResolver));
	this.handlerAdapter.setReturnValueHandlers(Collections.singletonList(viewHandler));
	this.handlerAdapter.setIgnoreDefaultModelOnRedirect(true);
	this.handlerAdapter.afterPropertiesSet();

	this.request.setAttribute(DispatcherServlet.OUTPUT_FLASH_MAP_ATTRIBUTE, new FlashMap());

	HandlerMethod handlerMethod = handlerMethod(new RedirectAttributeController(), "handle", Model.class);
	ModelAndView mav = this.handlerAdapter.handle(request, response, handlerMethod);

	assertTrue("Without RedirectAttributes arg, model should be empty", mav.getModel().isEmpty());
}
 
Example #10
Source File: JseErrorsTagTest.java    From sinavi-jfw with Apache License 2.0 6 votes vote down vote up
@Test
public void エラーメッセージがリクエストとフラッシュスコープにセットされ出力される() throws Exception {

    MessageContext context = new MessageContext(request);
    context.saveErrorMessageToRequest("E-JX_ERRORS_TAG_TEST#0001");
    context.saveErrorMessageToFlash("E-JX_ERRORS_TAG_TEST#0002");
    FlashMap flashMap = RequestContextUtils.getOutputFlashMap(request);
    request.setAttribute(MessageContext.ERROR_MESSAGE_KEY_TO_FLASH, flashMap.get(MessageContext.ERROR_MESSAGE_KEY_TO_FLASH));
    JseErrorsTag tag = new JseErrorsTag();
    tag.setJspContext(page);
    tag.doTag();

    assertThat(response.getContentAsString(), is("<div class=\"jfw_messages\">"
            + "<p class=\"jfw_err_msg_style\">E-JX_ERRORS_TAG_TEST#0001</p>"
            + "<p class=\"jfw_err_msg_style\">E-JX_ERRORS_TAG_TEST#0002</p></div>"));
}
 
Example #11
Source File: FlashMapManagerTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@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);

	assertNull(inputFlashMap);
	assertEquals("FlashMap should not have been removed", 1, this.flashMapManager.getFlashMaps().size());

	this.request.setQueryString("id=1&id=2");
	inputFlashMap = this.flashMapManager.retrieveAndUpdate(this.request, this.response);

	assertEquals(flashMap, inputFlashMap);
	assertEquals("Input FlashMap should have been removed", 0, this.flashMapManager.getFlashMaps().size());
}
 
Example #12
Source File: FlashMapManagerTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void retrieveAndUpdateSortMultipleMatches() {
	FlashMap emptyFlashMap = new FlashMap();

	FlashMap flashMapOne = new FlashMap();
	flashMapOne.put("key1", "value1");
	flashMapOne.setTargetRequestPath("/one");

	FlashMap flashMapTwo = new FlashMap();
	flashMapTwo.put("key1", "value1");
	flashMapTwo.put("key2", "value2");
	flashMapTwo.setTargetRequestPath("/one/two");

	this.flashMapManager.setFlashMaps(Arrays.asList(emptyFlashMap, flashMapOne, flashMapTwo));

	this.request.setRequestURI("/one/two");
	FlashMap inputFlashMap = this.flashMapManager.retrieveAndUpdate(this.request, this.response);

	assertEquals(flashMapTwo, inputFlashMap);
	assertEquals("Input FlashMap should have been removed", 2, this.flashMapManager.getFlashMaps().size());
}
 
Example #13
Source File: RedirectView.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Convert model to request parameters and redirect to the given URL.
 * @see #appendQueryProperties
 * @see #sendRedirect
 */
@Override
protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request,
		HttpServletResponse response) throws IOException {

	String targetUrl = createTargetUrl(model, request);
	targetUrl = updateTargetUrl(targetUrl, model, request, response);

	FlashMap flashMap = RequestContextUtils.getOutputFlashMap(request);
	if (!CollectionUtils.isEmpty(flashMap)) {
		UriComponents uriComponents = UriComponentsBuilder.fromUriString(targetUrl).build();
		flashMap.setTargetRequestPath(uriComponents.getPath());
		flashMap.addTargetRequestParams(uriComponents.getQueryParams());
		FlashMapManager flashMapManager = RequestContextUtils.getFlashMapManager(request);
		if (flashMapManager == null) {
			throw new IllegalStateException("FlashMapManager not found despite output FlashMap having been set");
		}
		flashMapManager.saveOutputFlashMap(flashMap, request, response);
	}

	sendRedirect(request, response, targetUrl, this.http10Compatible);
}
 
Example #14
Source File: RequestMappingHandlerAdapterTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void setAlwaysUseRedirectAttributes() throws Exception {
	HandlerMethodArgumentResolver redirectAttributesResolver = new RedirectAttributesMethodArgumentResolver();
	HandlerMethodArgumentResolver modelResolver = new ModelMethodProcessor();
	HandlerMethodReturnValueHandler viewHandler = new ViewNameMethodReturnValueHandler();

	this.handlerAdapter.setArgumentResolvers(Arrays.asList(redirectAttributesResolver, modelResolver));
	this.handlerAdapter.setReturnValueHandlers(Collections.singletonList(viewHandler));
	this.handlerAdapter.setIgnoreDefaultModelOnRedirect(true);
	this.handlerAdapter.afterPropertiesSet();

	this.request.setAttribute(DispatcherServlet.OUTPUT_FLASH_MAP_ATTRIBUTE, new FlashMap());

	HandlerMethod handlerMethod = handlerMethod(new RedirectAttributeController(), "handle", Model.class);
	ModelAndView mav = this.handlerAdapter.handle(request, response, handlerMethod);

	assertTrue("Without RedirectAttributes arg, model should be empty", mav.getModel().isEmpty());
}
 
Example #15
Source File: FlashMapManagerTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void flashAttributesWithQueryParamsWithSpace() throws Exception {

	String encodedValue = URLEncoder.encode("1 2", "UTF-8");

	FlashMap flashMap = new FlashMap();
	flashMap.put("key", "value");
	flashMap.setTargetRequestPath("/path");
	flashMap.addTargetRequestParam("param", encodedValue);

	this.request.setCharacterEncoding("UTF-8");
	this.flashMapManager.saveOutputFlashMap(flashMap, this.request, this.response);

	MockHttpServletRequest requestAfterRedirect = new MockHttpServletRequest("GET", "/path");
	requestAfterRedirect.setQueryString("param=" + encodedValue);
	requestAfterRedirect.addParameter("param", "1 2");

	flashMap = this.flashMapManager.retrieveAndUpdate(requestAfterRedirect, new MockHttpServletResponse());
	assertNotNull(flashMap);
	assertEquals(1, flashMap.size());
	assertEquals("value", flashMap.get("key"));
}
 
Example #16
Source File: FlashMapManagerTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test // SPR-15505
public void retrieveAndUpdateMatchByOriginatingPathAndQueryString() {
	FlashMap flashMap = new FlashMap();
	flashMap.put("key", "value");
	flashMap.setTargetRequestPath("/accounts");
	flashMap.addTargetRequestParam("a", "b");

	this.flashMapManager.setFlashMaps(Collections.singletonList(flashMap));

	this.request.setAttribute(WebUtils.FORWARD_REQUEST_URI_ATTRIBUTE, "/accounts");
	this.request.setAttribute(WebUtils.FORWARD_QUERY_STRING_ATTRIBUTE, "a=b");
	this.request.setRequestURI("/mvc/accounts");
	this.request.setQueryString("x=y");
	FlashMap inputFlashMap = this.flashMapManager.retrieveAndUpdate(this.request, this.response);

	assertEquals(flashMap, inputFlashMap);
	assertEquals("Input FlashMap should have been removed", 0, this.flashMapManager.getFlashMaps().size());
}
 
Example #17
Source File: PageRestController.java    From wallride with Apache License 2.0 6 votes vote down vote up
@RequestMapping(value="/{language}/pages", method=RequestMethod.POST)
public @ResponseBody DomainObjectSavedModel save(
		@Valid PageCreateForm form,
		BindingResult result,
		AuthorizedUser authorizedUser,
		HttpServletRequest request,
		HttpServletResponse response) throws BindException {
	if (result.hasErrors()) {
		throw new BindException(result);
	}
	Page page = pageService.createPage(form.buildPageCreateRequest(), Post.Status.DRAFT, authorizedUser);
	FlashMap flashMap = RequestContextUtils.getOutputFlashMap(request);
	flashMap.put("savedPage", page);
	RequestContextUtils.getFlashMapManager(request).saveOutputFlashMap(flashMap, request, response);
	return new DomainObjectSavedModel<>(page);
}
 
Example #18
Source File: FlashMapManagerTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void saveOutputFlashMapDecodeParameters() throws Exception {

	FlashMap flashMap = new FlashMap();
	flashMap.put("key", "value");
	flashMap.setTargetRequestPath("/path");
	flashMap.addTargetRequestParam("param", "%D0%90%D0%90");
	flashMap.addTargetRequestParam("param", "%D0%91%D0%91");
	flashMap.addTargetRequestParam("param", "%D0%92%D0%92");
	flashMap.addTargetRequestParam("%3A%2F%3F%23%5B%5D%40", "value");

	this.request.setCharacterEncoding("UTF-8");
	this.flashMapManager.saveOutputFlashMap(flashMap, this.request, this.response);

	MockHttpServletRequest requestAfterRedirect = new MockHttpServletRequest("GET", "/path");
	requestAfterRedirect.setQueryString("param=%D0%90%D0%90&param=%D0%91%D0%91&param=%D0%92%D0%92&%3A%2F%3F%23%5B%5D%40=value");
	requestAfterRedirect.addParameter("param", "\u0410\u0410");
	requestAfterRedirect.addParameter("param", "\u0411\u0411");
	requestAfterRedirect.addParameter("param", "\u0412\u0412");
	requestAfterRedirect.addParameter(":/?#[]@", "value");

	flashMap = this.flashMapManager.retrieveAndUpdate(requestAfterRedirect, new MockHttpServletResponse());
	assertNotNull(flashMap);
	assertEquals(1, flashMap.size());
	assertEquals("value", flashMap.get("key"));
}
 
Example #19
Source File: TagRestController.java    From wallride with Apache License 2.0 6 votes vote down vote up
@RequestMapping(value="/{language}/tags/{id}", method=RequestMethod.POST)
public @ResponseBody DomainObjectUpdatedModel update(
		@Valid TagEditForm form,
		BindingResult errors,
		@PathVariable long id,
		AuthorizedUser authorizedUser,
		HttpServletRequest request,
		HttpServletResponse response) throws BindException {
	form.setId(id);
	if (errors.hasErrors()) {
		throw new BindException(errors);
	}

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

	FlashMap flashMap = RequestContextUtils.getOutputFlashMap(request);
	flashMap.put("savedTag", savedTag);
	RequestContextUtils.getFlashMapManager(request).saveOutputFlashMap(flashMap, request, response);
	return new DomainObjectUpdatedModel<>(savedTag);
}
 
Example #20
Source File: FlashMapManagerTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void saveOutputFlashMapNormalizeTargetPath() throws InterruptedException {
	FlashMap flashMap = new FlashMap();
	flashMap.put("key", "value");

	flashMap.setTargetRequestPath(".");
	this.request.setRequestURI("/once/upon/a/time");
	this.flashMapManager.saveOutputFlashMap(flashMap, this.request, this.response);

	assertEquals("/once/upon/a", flashMap.getTargetRequestPath());

	flashMap.setTargetRequestPath("./");
	this.request.setRequestURI("/once/upon/a/time");
	this.flashMapManager.saveOutputFlashMap(flashMap, this.request, this.response);

	assertEquals("/once/upon/a/", flashMap.getTargetRequestPath());

	flashMap.setTargetRequestPath("..");
	this.request.setRequestURI("/once/upon/a/time");
	this.flashMapManager.saveOutputFlashMap(flashMap, this.request, this.response);

	assertEquals("/once/upon", flashMap.getTargetRequestPath());

	flashMap.setTargetRequestPath("../");
	this.request.setRequestURI("/once/upon/a/time");
	this.flashMapManager.saveOutputFlashMap(flashMap, this.request, this.response);

	assertEquals("/once/upon/", flashMap.getTargetRequestPath());

	flashMap.setTargetRequestPath("../../only");
	this.request.setRequestURI("/once/upon/a/time");
	this.flashMapManager.saveOutputFlashMap(flashMap, this.request, this.response);

	assertEquals("/once/only", flashMap.getTargetRequestPath());
}
 
Example #21
Source File: RedirectViewTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Before
public void setUp() throws Exception {
	this.request = new MockHttpServletRequest();
	this.request.setContextPath("/context");
	this.request.setCharacterEncoding(WebUtils.DEFAULT_CHARACTER_ENCODING);
	this.request.setAttribute(DispatcherServlet.OUTPUT_FLASH_MAP_ATTRIBUTE, new FlashMap());
	this.request.setAttribute(DispatcherServlet.FLASH_MAP_MANAGER_ATTRIBUTE, new SessionFlashMapManager());
	this.response = new MockHttpServletResponse();

}
 
Example #22
Source File: PrintingResultHandler.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Print "output" flash attributes.
 */
protected void printFlashMap(FlashMap flashMap) throws Exception {
	if (ObjectUtils.isEmpty(flashMap)) {
		this.printer.printValue("Attributes", null);
	}
	else {
		flashMap.forEach((name, value) -> {
			this.printer.printValue("Attribute", name);
			this.printer.printValue("value", value);
		});
	}
}
 
Example #23
Source File: SessionFlashMapManager.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Retrieves saved FlashMap instances from the HTTP session, if any.
 */
@Override
@SuppressWarnings("unchecked")
@Nullable
protected List<FlashMap> retrieveFlashMaps(HttpServletRequest request) {
	HttpSession session = request.getSession(false);
	return (session != null ? (List<FlashMap>) session.getAttribute(FLASH_MAPS_SESSION_ATTRIBUTE) : null);
}
 
Example #24
Source File: RequestContextUtils.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Convenience method that retrieves the {@link #getOutputFlashMap "output"
 * FlashMap}, updates it with the path and query params of the target URL,
 * and then saves it using the {@link #getFlashMapManager FlashMapManager}.
 * @param location the target URL for the redirect
 * @param request the current request
 * @param response the current response
 * @since 5.0
 */
public static void saveOutputFlashMap(String location, HttpServletRequest request, HttpServletResponse response) {
	FlashMap flashMap = getOutputFlashMap(request);
	if (CollectionUtils.isEmpty(flashMap)) {
		return;
	}

	UriComponents uriComponents = UriComponentsBuilder.fromUriString(location).build();
	flashMap.setTargetRequestPath(uriComponents.getPath());
	flashMap.addTargetRequestParams(uriComponents.getQueryParams());

	FlashMapManager manager = getFlashMapManager(request);
	Assert.state(manager != null, "No FlashMapManager. Is this a DispatcherServlet handled request?");
	manager.saveOutputFlashMap(flashMap, request, response);
}
 
Example #25
Source File: FlashMapManagerTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void saveOutputFlashMapDecodeTargetPath() throws InterruptedException {
	FlashMap flashMap = new FlashMap();
	flashMap.put("key", "value");

	flashMap.setTargetRequestPath("/once%20upon%20a%20time");
	this.flashMapManager.saveOutputFlashMap(flashMap, this.request, this.response);

	assertEquals("/once upon a time", flashMap.getTargetRequestPath());
}
 
Example #26
Source File: JseErrorsTagTest.java    From sinavi-jfw with Apache License 2.0 5 votes vote down vote up
@Test
public void エラーメッセージがフラッシュスコープにセットされ出力される() throws Exception {

    MessageContext context = new MessageContext(request);
    context.saveErrorMessageToFlash("E-JX_ERRORS_TAG_TEST#0001");
    FlashMap flashMap = RequestContextUtils.getOutputFlashMap(request);
    request.setAttribute(MessageContext.ERROR_MESSAGE_KEY_TO_FLASH, flashMap.get(MessageContext.ERROR_MESSAGE_KEY_TO_FLASH));
    JseErrorsTag tag = new JseErrorsTag();
    tag.setJspContext(page);
    tag.doTag();

    assertThat(response.getContentAsString(), is("<div class=\"jfw_messages\">"
        + "<p class=\"jfw_err_msg_style\">E-JX_ERRORS_TAG_TEST#0001</p></div>"));
}
 
Example #27
Source File: JseInformationsTagTest.java    From sinavi-jfw with Apache License 2.0 5 votes vote down vote up
@Test
public void インフォメーションメッセージがフラッシュスコープにセットされ出力される() throws Exception {

    MessageContext context = new MessageContext(request);
    context.saveInformationMessageToFlash("I-JX_INFORMATIONS_TAG_TEST#0001");
    FlashMap flashMap = RequestContextUtils.getOutputFlashMap(request);
    request.setAttribute(MessageContext.INFORMATION_MESSAGE_KEY_TO_FLASH, flashMap.get(MessageContext.INFORMATION_MESSAGE_KEY_TO_FLASH));
    JseInformationsTag tag = new JseInformationsTag();
    tag.setJspContext(page);
    tag.doTag();

    assertThat(response.getContentAsString(), is("<div class=\"jfw_messages\">"
        + "<p class=\"jfw_msg_style\">I-JX_INFORMATIONS_TAG_TEST#0001</p></div>"));
}
 
Example #28
Source File: StubMvcResult.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
public StubMvcResult(MockHttpServletRequest request,
					 Object handler,
					 HandlerInterceptor[] interceptors,
					 Exception resolvedException,
					 ModelAndView mav,
					 FlashMap flashMap,
					 MockHttpServletResponse response) {
	this.request = request;
	this.handler = handler;
	this.interceptors = interceptors;
	this.resolvedException = resolvedException;
	this.mav = mav;
	this.flashMap = flashMap;
	this.response = response;
}
 
Example #29
Source File: FlashMapManagerTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void saveOutputFlashMap() throws InterruptedException {
	FlashMap flashMap = new FlashMap();
	flashMap.put("name", "value");

	this.flashMapManager.setFlashMapTimeout(-1); // expire immediately so we can check expiration started
	this.flashMapManager.saveOutputFlashMap(flashMap, this.request, this.response);
	List<FlashMap> allMaps = this.flashMapManager.getFlashMaps();

	assertNotNull(allMaps);
	assertSame(flashMap, allMaps.get(0));
	assertTrue(flashMap.isExpired());
}
 
Example #30
Source File: JseInformationsTagTest.java    From sinavi-jfw with Apache License 2.0 5 votes vote down vote up
@Test
public void エラーメッセージがフラッシュスコープにセットされ出力されない() throws Exception {

    MessageContext context = new MessageContext(request);
    context.saveErrorMessageToFlash("E-JX_ERRORS_TAG_TEST#0001");
    FlashMap flashMap = RequestContextUtils.getOutputFlashMap(request);
    request.setAttribute(MessageContext.INFORMATION_MESSAGE_KEY_TO_FLASH, flashMap.get(MessageContext.INFORMATION_MESSAGE_KEY_TO_FLASH));
    JseInformationsTag tag = new JseInformationsTag();
    tag.setJspContext(page);
    tag.doTag();

    assertThat(response.getContentAsString(), is(""));
}