Search in sources :

Example 6 with RedirectModel

use of cn.taketoday.web.view.RedirectModel in project today-framework by TAKETODAY.

the class AbstractUrlViewController method handleRequestInternal.

/**
 * Retrieves the URL path to use for lookup and delegates to
 * {@link #getViewNameForRequest}. Also adds the content of
 * {@link RequestContextUtils#getInputRedirectModel(RequestContext)} to the model.
 */
@Override
protected ModelAndView handleRequestInternal(RequestContext request) {
    String viewName = getViewNameForRequest(request);
    if (log.isTraceEnabled()) {
        log.trace("Returning view name '{}'", viewName);
    }
    RedirectModel model = RequestContextUtils.getInputRedirectModel(request);
    if (model != null) {
        return new ModelAndView(viewName, model.asMap());
    }
    return new ModelAndView(viewName);
}
Also used : RedirectModel(cn.taketoday.web.view.RedirectModel) ModelAndView(cn.taketoday.web.view.ModelAndView)

Example 7 with RedirectModel

use of cn.taketoday.web.view.RedirectModel in project today-framework by TAKETODAY.

the class BindingContext method toString.

/**
 * Return diagnostic information.
 */
@Override
public String toString() {
    StringBuilder sb = new StringBuilder("BindingContext: ");
    if (isViewReference()) {
        sb.append("reference to view with name '").append(view).append('\'');
    } else {
        sb.append("View is [").append(view).append(']');
    }
    sb.append("; default model ");
    sb.append(getModel());
    RedirectModel redirectModel = getRedirectModel();
    if (redirectModel != null) {
        sb.append("; redirect model ");
        sb.append(redirectModel);
    }
    return sb.toString();
}
Also used : RedirectModel(cn.taketoday.web.view.RedirectModel)

Example 8 with RedirectModel

use of cn.taketoday.web.view.RedirectModel in project today-framework by TAKETODAY.

the class ModelMethodProcessorTests method handleModelReturnValue.

@Test
public void handleModelReturnValue() throws Exception {
    BindingContext bindingContext = webRequest.getBindingContext();
    bindingContext.addAttribute("attr1", "value1");
    ModelMap returnValue = new ModelMap();
    returnValue.addAttribute("attr2", "value2");
    Method method = getClass().getDeclaredMethod("model", Model.class);
    HandlerMethod handlerMethod = new HandlerMethod(this, method);
    processor.handleReturnValue(webRequest, handlerMethod, returnValue);
    assertThat(bindingContext.getModel().get("attr1")).isEqualTo("value1");
    assertThat(bindingContext.getModel().get("attr2")).isEqualTo("value2");
    // RedirectModel
    assertThat((Object) bindingContext.getRedirectModel()).isNull();
    assertThat(webRequest.hasAttribute(RedirectModel.OUTPUT_ATTRIBUTE)).isFalse();
    assertThat((Object) RedirectModel.findOutputModel(webRequest)).isNull();
    assertThat((Object) RedirectModel.findOutputModel(webRequest)).isSameAs(bindingContext.getRedirectModel());
    processor.handleReturnValue(webRequest, redirectModelHandler, new RedirectModel("attr3", "value3"));
    assertThat(webRequest.hasAttribute(RedirectModel.OUTPUT_ATTRIBUTE)).isTrue();
    assertThat((Object) RedirectModel.findOutputModel(webRequest)).isSameAs(bindingContext.getRedirectModel());
    assertThat((Object) bindingContext.getRedirectModel()).isNotNull();
    assertThat(bindingContext.getRedirectModel().getAttribute("attr3")).isEqualTo("value3");
    processor.handleReturnValue(webRequest, redirectModelHandler, new RedirectModel("attr4", "value4"));
    assertThat(bindingContext.getRedirectModel().getAttribute("attr3")).isEqualTo("value3");
    assertThat(bindingContext.getRedirectModel().getAttribute("attr4")).isEqualTo("value4");
}
Also used : RedirectModel(cn.taketoday.web.view.RedirectModel) ModelMap(cn.taketoday.web.view.ModelMap) HandlerMethod(cn.taketoday.web.handler.method.HandlerMethod) Method(java.lang.reflect.Method) BindingContext(cn.taketoday.web.BindingContext) HandlerMethod(cn.taketoday.web.handler.method.HandlerMethod) Test(org.junit.jupiter.api.Test)

Example 9 with RedirectModel

use of cn.taketoday.web.view.RedirectModel in project today-framework by TAKETODAY.

the class UrlFilenameViewControllerTests method withFlashAttributes.

@PathPatternsParameterizedTest
void withFlashAttributes(Function<String, RequestContext> requestFactory) throws Exception {
    UrlFilenameViewController controller = new UrlFilenameViewController();
    RequestContext request = requestFactory.apply("/index");
    request.setAttribute(RedirectModel.INPUT_ATTRIBUTE, new RedirectModel("name", "value"));
    ModelAndView mv = controller.handleRequest(request);
    assertThat(mv.getViewName()).isEqualTo("index");
    assertThat(mv.getModel().size()).isEqualTo(1);
    assertThat(mv.getModel().get("name")).isEqualTo("value");
}
Also used : RedirectModel(cn.taketoday.web.view.RedirectModel) ModelAndView(cn.taketoday.web.view.ModelAndView) RequestContext(cn.taketoday.web.RequestContext) ServletRequestContext(cn.taketoday.web.servlet.ServletRequestContext) MockServletRequestContext(cn.taketoday.web.servlet.MockServletRequestContext) PathPatternsParameterizedTest(cn.taketoday.web.view.PathPatternsParameterizedTest)

Example 10 with RedirectModel

use of cn.taketoday.web.view.RedirectModel in project today-infrastructure by TAKETODAY.

the class BindingContext method toString.

/**
 * Return diagnostic information.
 */
@Override
public String toString() {
    StringBuilder sb = new StringBuilder("BindingContext: ");
    if (isViewReference()) {
        sb.append("reference to view with name '").append(view).append('\'');
    } else {
        sb.append("View is [").append(view).append(']');
    }
    sb.append("; default model ");
    sb.append(getModel());
    RedirectModel redirectModel = getRedirectModel();
    if (redirectModel != null) {
        sb.append("; redirect model ");
        sb.append(redirectModel);
    }
    return sb.toString();
}
Also used : RedirectModel(cn.taketoday.web.view.RedirectModel)

Aggregations

RedirectModel (cn.taketoday.web.view.RedirectModel)13 ModelAndView (cn.taketoday.web.view.ModelAndView)6 BindingContext (cn.taketoday.web.BindingContext)4 Test (org.junit.jupiter.api.Test)4 Nullable (cn.taketoday.lang.Nullable)2 RequestContext (cn.taketoday.web.RequestContext)2 AsyncWebRequest (cn.taketoday.web.context.async.AsyncWebRequest)2 WebAsyncManager (cn.taketoday.web.context.async.WebAsyncManager)2 MockServletRequestContext (cn.taketoday.web.servlet.MockServletRequestContext)2 ServletRequestContext (cn.taketoday.web.servlet.ServletRequestContext)2 PathPatternsParameterizedTest (cn.taketoday.web.view.PathPatternsParameterizedTest)2 RequestContextDataBinder (cn.taketoday.web.bind.RequestContextDataBinder)1 HandlerMethod (cn.taketoday.web.handler.method.HandlerMethod)1 ModelMap (cn.taketoday.web.view.ModelMap)1 RedirectModelManager (cn.taketoday.web.view.RedirectModelManager)1 Method (java.lang.reflect.Method)1