Search in sources :

Example 21 with RedirectAttributesModelMap

use of org.springframework.web.servlet.mvc.support.RedirectAttributesModelMap in project spring-framework by bluecrow1986.

the class RedirectAttributesMethodArgumentResolver method resolveArgument.

@Override
public Object resolveArgument(MethodParameter parameter, @Nullable ModelAndViewContainer mavContainer, NativeWebRequest webRequest, @Nullable WebDataBinderFactory binderFactory) throws Exception {
    Assert.state(mavContainer != null, "RedirectAttributes argument only supported on regular handler methods");
    ModelMap redirectAttributes;
    if (binderFactory != null) {
        DataBinder dataBinder = binderFactory.createBinder(webRequest, null, DataBinder.DEFAULT_OBJECT_NAME);
        redirectAttributes = new RedirectAttributesModelMap(dataBinder);
    } else {
        redirectAttributes = new RedirectAttributesModelMap();
    }
    mavContainer.setRedirectModel(redirectAttributes);
    return redirectAttributes;
}
Also used : ModelMap(org.springframework.ui.ModelMap) RedirectAttributesModelMap(org.springframework.web.servlet.mvc.support.RedirectAttributesModelMap) DataBinder(org.springframework.validation.DataBinder) RedirectAttributesModelMap(org.springframework.web.servlet.mvc.support.RedirectAttributesModelMap)

Example 22 with RedirectAttributesModelMap

use of org.springframework.web.servlet.mvc.support.RedirectAttributesModelMap in project spring-framework by bluecrow1986.

the class ModelAndViewMethodReturnValueHandlerTests method handleRedirectAttributesWithViewName.

@Test
public void handleRedirectAttributesWithViewName() throws Exception {
    RedirectAttributesModelMap redirectAttributes = new RedirectAttributesModelMap();
    mavContainer.setRedirectModel(redirectAttributes);
    ModelAndView mav = new ModelAndView("redirect:viewName", "attrName", "attrValue");
    handler.handleReturnValue(mav, returnParamModelAndView, mavContainer, webRequest);
    ModelMap model = mavContainer.getModel();
    assertThat(mavContainer.getViewName()).isEqualTo("redirect:viewName");
    assertThat(model.get("attrName")).isEqualTo("attrValue");
    assertThat(model).isSameAs(redirectAttributes);
}
Also used : RedirectAttributesModelMap(org.springframework.web.servlet.mvc.support.RedirectAttributesModelMap) ModelMap(org.springframework.ui.ModelMap) ModelAndView(org.springframework.web.servlet.ModelAndView) RedirectAttributesModelMap(org.springframework.web.servlet.mvc.support.RedirectAttributesModelMap) Test(org.junit.jupiter.api.Test)

Example 23 with RedirectAttributesModelMap

use of org.springframework.web.servlet.mvc.support.RedirectAttributesModelMap in project spring-framework by bluecrow1986.

the class ModelAndViewMethodReturnValueHandlerTests method handleRedirectAttributesWithCustomPrefix.

@Test
public void handleRedirectAttributesWithCustomPrefix() throws Exception {
    RedirectAttributesModelMap redirectAttributes = new RedirectAttributesModelMap();
    mavContainer.setRedirectModel(redirectAttributes);
    ModelAndView mav = new ModelAndView("myRedirect:viewName", "attrName", "attrValue");
    handler.setRedirectPatterns("myRedirect:*");
    handler.handleReturnValue(mav, returnParamModelAndView, mavContainer, webRequest);
    ModelMap model = mavContainer.getModel();
    assertThat(mavContainer.getViewName()).isEqualTo("myRedirect:viewName");
    assertThat(model.get("attrName")).isEqualTo("attrValue");
    assertThat(model).isSameAs(redirectAttributes);
}
Also used : RedirectAttributesModelMap(org.springframework.web.servlet.mvc.support.RedirectAttributesModelMap) ModelMap(org.springframework.ui.ModelMap) ModelAndView(org.springframework.web.servlet.ModelAndView) RedirectAttributesModelMap(org.springframework.web.servlet.mvc.support.RedirectAttributesModelMap) Test(org.junit.jupiter.api.Test)

Example 24 with RedirectAttributesModelMap

use of org.springframework.web.servlet.mvc.support.RedirectAttributesModelMap in project spring-framework by bluecrow1986.

the class ModelAndViewMethodReturnValueHandlerTests method handleRedirectAttributesWithViewReference.

@Test
public void handleRedirectAttributesWithViewReference() throws Exception {
    RedirectAttributesModelMap redirectAttributes = new RedirectAttributesModelMap();
    mavContainer.setRedirectModel(redirectAttributes);
    ModelAndView mav = new ModelAndView(new RedirectView(), "attrName", "attrValue");
    handler.handleReturnValue(mav, returnParamModelAndView, mavContainer, webRequest);
    assertThat(mavContainer.getView().getClass()).isEqualTo(RedirectView.class);
    assertThat(mavContainer.getModel().get("attrName")).isEqualTo("attrValue");
    assertThat(mavContainer.getModel()).as("RedirectAttributes should be used if controller redirects").isSameAs(redirectAttributes);
}
Also used : ModelAndView(org.springframework.web.servlet.ModelAndView) RedirectView(org.springframework.web.servlet.view.RedirectView) RedirectAttributesModelMap(org.springframework.web.servlet.mvc.support.RedirectAttributesModelMap) Test(org.junit.jupiter.api.Test)

Example 25 with RedirectAttributesModelMap

use of org.springframework.web.servlet.mvc.support.RedirectAttributesModelMap in project spring-framework by bluecrow1986.

the class ModelAndViewMethodReturnValueHandlerTests method handleRedirectAttributesWithoutRedirect.

@Test
public void handleRedirectAttributesWithoutRedirect() throws Exception {
    RedirectAttributesModelMap redirectAttributes = new RedirectAttributesModelMap();
    mavContainer.setRedirectModel(redirectAttributes);
    ModelAndView mav = new ModelAndView();
    handler.handleReturnValue(mav, returnParamModelAndView, mavContainer, webRequest);
    ModelMap model = mavContainer.getModel();
    assertThat(mavContainer.getView()).isEqualTo(null);
    assertThat(mavContainer.getModel().isEmpty()).isTrue();
    assertThat(model).as("RedirectAttributes should not be used if controller doesn't redirect").isNotSameAs(redirectAttributes);
}
Also used : RedirectAttributesModelMap(org.springframework.web.servlet.mvc.support.RedirectAttributesModelMap) ModelMap(org.springframework.ui.ModelMap) ModelAndView(org.springframework.web.servlet.ModelAndView) RedirectAttributesModelMap(org.springframework.web.servlet.mvc.support.RedirectAttributesModelMap) Test(org.junit.jupiter.api.Test)

Aggregations

RedirectAttributesModelMap (org.springframework.web.servlet.mvc.support.RedirectAttributesModelMap)64 ModelMap (org.springframework.ui.ModelMap)44 Test (org.junit.jupiter.api.Test)33 ModelAndView (org.springframework.web.servlet.ModelAndView)25 Test (org.junit.Test)24 RedirectView (org.springframework.web.servlet.view.RedirectView)10 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)9 BindingResult (org.springframework.validation.BindingResult)6 DBUnitTest (org.orcid.test.DBUnitTest)5 MethodParameter (org.springframework.core.MethodParameter)5 DataBinder (org.springframework.validation.DataBinder)5 TodoDTO (net.petrikainulainen.spring.testmvc.todo.dto.TodoDTO)4 AbstractNGrinderTransactionalTest (org.ngrinder.AbstractNGrinderTransactionalTest)4 FileEntry (org.ngrinder.script.model.FileEntry)3 Sample (ca.corefacility.bioinformatics.irida.model.sample.Sample)2 List (java.util.List)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 TodoNotFoundException (net.petrikainulainen.spring.testmvc.todo.exception.TodoNotFoundException)2 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)2 Model (org.springframework.ui.Model)2