Search in sources :

Example 1 with BindStatus

use of org.springframework.web.reactive.result.view.BindStatus in project spring-framework by spring-projects.

the class FreeMarkerMacroTests method springMacroRequestContextIsAutomaticallyExposedAsModelAttribute.

@Test
public void springMacroRequestContextIsAutomaticallyExposedAsModelAttribute() throws Exception {
    storeTemplateInTempDir("<@spring.bind \"testBean.name\"/>\nHi ${spring.status.value}");
    FreeMarkerView view = new FreeMarkerView() {

        @Override
        protected Mono<Void> renderInternal(Map<String, Object> renderAttributes, MediaType contentType, ServerWebExchange exchange) {
            Object value = renderAttributes.get(SPRING_MACRO_REQUEST_CONTEXT_ATTRIBUTE);
            assertThat(value).isInstanceOf(RequestContext.class);
            BindStatus status = ((RequestContext) value).getBindStatus("testBean.name");
            assertThat(status.getExpression()).isEqualTo("name");
            assertThat(status.getValue()).isEqualTo("Dilbert");
            return super.renderInternal(renderAttributes, contentType, exchange);
        }
    };
    view.setApplicationContext(this.applicationContext);
    view.setBeanName("myView");
    view.setUrl("tmp.ftl");
    view.setConfiguration(this.freeMarkerConfig);
    view.render(singletonMap("testBean", new TestBean("Dilbert", 99)), null, this.exchange).subscribe();
    assertThat(getOutput()).containsExactly("Hi Dilbert");
}
Also used : MockServerWebExchange(org.springframework.web.testfixture.server.MockServerWebExchange) ServerWebExchange(org.springframework.web.server.ServerWebExchange) TestBean(org.springframework.beans.testfixture.beans.TestBean) MediaType(org.springframework.http.MediaType) RequestContext(org.springframework.web.reactive.result.view.RequestContext) DummyMacroRequestContext(org.springframework.web.reactive.result.view.DummyMacroRequestContext) BindStatus(org.springframework.web.reactive.result.view.BindStatus) HashMap(java.util.HashMap) ModelMap(org.springframework.ui.ModelMap) Map(java.util.Map) Collections.singletonMap(java.util.Collections.singletonMap) ExtendedModelMap(org.springframework.ui.ExtendedModelMap) Test(org.junit.jupiter.api.Test)

Aggregations

Collections.singletonMap (java.util.Collections.singletonMap)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Test (org.junit.jupiter.api.Test)1 TestBean (org.springframework.beans.testfixture.beans.TestBean)1 MediaType (org.springframework.http.MediaType)1 ExtendedModelMap (org.springframework.ui.ExtendedModelMap)1 ModelMap (org.springframework.ui.ModelMap)1 BindStatus (org.springframework.web.reactive.result.view.BindStatus)1 DummyMacroRequestContext (org.springframework.web.reactive.result.view.DummyMacroRequestContext)1 RequestContext (org.springframework.web.reactive.result.view.RequestContext)1 ServerWebExchange (org.springframework.web.server.ServerWebExchange)1 MockServerWebExchange (org.springframework.web.testfixture.server.MockServerWebExchange)1