Search in sources :

Example 1 with ModelMap

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

the class ModelMethodProcessorTests method supportsReturnType.

@Test
public void supportsReturnType() throws NoSuchMethodException {
    Method method = getClass().getDeclaredMethod("model", Model.class);
    HandlerMethod handlerMethod = new HandlerMethod(this, method);
    assertThat(processor.supportsHandler(null)).isFalse();
    assertThat(processor.supportsHandler(handlerMethod)).isTrue();
    assertThat(processor.supportsHandler(redirectModelHandler)).isTrue();
    assertThat(processor.supportsReturnValue(handlerMethod)).isFalse();
    assertThat(processor.supportsReturnValue(new ModelMap())).isTrue();
}
Also used : ModelMap(cn.taketoday.web.view.ModelMap) HandlerMethod(cn.taketoday.web.handler.method.HandlerMethod) Method(java.lang.reflect.Method) HandlerMethod(cn.taketoday.web.handler.method.HandlerMethod) Test(org.junit.jupiter.api.Test)

Example 2 with ModelMap

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

the class ModelMethodProcessorTests method resolveArgumentValue.

@Test
public void resolveArgumentValue() throws Throwable {
    BindingContext bindingContext = webRequest.getBindingContext();
    ModelMap model = bindingContext.getModel();
    assertThat(processor.resolveArgument(webRequest, new ResolvableMethodParameter(paramModel))).isSameAs(model);
    assertThat(processor.resolveArgument(webRequest, new ResolvableMethodParameter(redirectModelParam))).isSameAs(bindingContext.getRedirectModel());
    assertThat(webRequest.hasAttribute(RedirectModel.OUTPUT_ATTRIBUTE)).isTrue();
    assertThat((Object) RedirectModel.findOutputModel(webRequest)).isSameAs(bindingContext.getRedirectModel());
}
Also used : ModelMap(cn.taketoday.web.view.ModelMap) ResolvableMethodParameter(cn.taketoday.web.handler.method.ResolvableMethodParameter) BindingContext(cn.taketoday.web.BindingContext) Test(org.junit.jupiter.api.Test)

Example 3 with ModelMap

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

the class ErrorsMethodArgumentResolver method resolveArgument.

@Nullable
@Override
public Object resolveArgument(RequestContext context, ResolvableMethodParameter resolvable) throws Throwable {
    BindingContext bindingContext = context.getBindingContext();
    ModelMap model = bindingContext.getModel();
    String lastKey = CollectionUtils.lastElement(model.keySet());
    if (lastKey != null && lastKey.startsWith(BindingResult.MODEL_KEY_PREFIX)) {
        return model.getAttribute(lastKey);
    }
    throw new IllegalStateException("An Errors/BindingResult argument is expected to be declared immediately after " + "the model attribute, the @RequestBody or the @RequestPart arguments " + "to which they apply: " + resolvable.getMethod());
}
Also used : ModelMap(cn.taketoday.web.view.ModelMap) BindingContext(cn.taketoday.web.BindingContext) Nullable(cn.taketoday.lang.Nullable)

Example 4 with ModelMap

use of cn.taketoday.web.view.ModelMap 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 5 with ModelMap

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

the class SessionAttributesHandlerTests method storeAttributes.

@Test
public void storeAttributes() throws Exception {
    ModelMap model = new ModelMap();
    model.put("attr1", "value1");
    model.put("attr2", "value2");
    model.put("attr3", new TestBean());
    sessionAttributesHandler.storeAttributes(request, model);
    assertThat(sessionAttributeStore.retrieveAttribute(request, "attr1")).isEqualTo("value1");
    assertThat(sessionAttributeStore.retrieveAttribute(request, "attr2")).isEqualTo("value2");
    boolean condition = sessionAttributeStore.retrieveAttribute(request, "attr3") instanceof TestBean;
    assertThat(condition).isTrue();
}
Also used : TestBean(cn.taketoday.beans.testfixture.beans.TestBean) ModelMap(cn.taketoday.web.view.ModelMap) Test(org.junit.jupiter.api.Test)

Aggregations

ModelMap (cn.taketoday.web.view.ModelMap)6 BindingContext (cn.taketoday.web.BindingContext)4 Test (org.junit.jupiter.api.Test)4 Nullable (cn.taketoday.lang.Nullable)2 HandlerMethod (cn.taketoday.web.handler.method.HandlerMethod)2 Method (java.lang.reflect.Method)2 TestBean (cn.taketoday.beans.testfixture.beans.TestBean)1 ResolvableMethodParameter (cn.taketoday.web.handler.method.ResolvableMethodParameter)1 RedirectModel (cn.taketoday.web.view.RedirectModel)1