4

I'm trying to show a Flux of Strings from my Controller to a view in Thymeleaf but i'm getting just a ReactiveDataDriverContextVariable on the view

I'm using Spring boot 2

This is my controller code

@GetMapping("/demo")
fun demo(model: Model): String{
    val data = Flux.just("ONE", "TWO", "THREE", "FOUR")
    model.addAttribute("data", ReactiveDataDriverContextVariable(data,1))
    return "demo"
}

And this is my Thymeleaf template code

    <table class="table table-striped table-responsive">
        <tr th:each="c: ${data}">
            <td th:text="${c}">...</td>
        </tr>
    </table>

As far as i know ReactiveDataDriverContextVariable should put thymeleaf in a data-driven mode and show a list of the four Strings but i'm just getting this output:

org.thymeleaf.spring5.context.webflux.ReactiveDataDriverContextVariable@30ece48

I'm sure there is something i'm missing Thank you in advance

EDIT 1

Seems to be a problem with Spring Security

I have this configuration enabled

@EnableWebSecurity
class SecurityConfig : WebSecurityConfigurerAdapter() {

    override fun configure(http: HttpSecurity) {
        http.authorizeRequests()
                .antMatchers("/demo")
                .permitAll()
    }

}

When i delete Spring Security configuration and dependency everything works just fine but i would like to secure the webapp

any ideas?

Cyberdev
  • 41
  • 6
  • Your usage seems correct but as far as i know `ReactiveDataDriverContextVariable` is available from Thymeleaf 3.0. Please post your pom file. – Ahmet Feb 12 '19 at 09:17
  • Hi @Ahmet i'm using Gradle so i post you a dependencyInsight: `org.thymeleaf:thymeleaf:3.0.11.RELEASE (selected by rule)` Thank you – Cyberdev Feb 12 '19 at 09:34
  • I am suspicous about the `ReactiveDataDriverContextVariable(data,1)`. Especially the second element of the constructor. Try increasing the buffer size like `ReactiveDataDriverContextVariable(data,5)` – Ahmet Feb 12 '19 at 11:18
  • Tried before, and had the same result just a context variable when it should be 4 strings. Maybe i'm missing some configuration? – Cyberdev Feb 12 '19 at 11:30
  • 1
    As far as thymeleaf part is concerned; everything is fine there. It might be of a config or dependency issue. Another thing might be the initialization and the assignment of `data` variable. I am not into kotlin so i can not say anything about that part. – Ahmet Feb 12 '19 at 12:14
  • 1
    I just copy-pasted your code, and run using Java, and all works just fine. It looks like, some issue with Kotlin – BSeitkazin Feb 13 '19 at 10:32
  • It seams to be a miss configuration with Spring security, i'll post the answer when i figure it out how to make it work – Cyberdev Feb 13 '19 at 10:36
  • 1
    I also have the same issue with Kotlin. I also removed the security dependecy and it's the same. – Peter Borbas Dec 02 '19 at 19:15

1 Answers1

1

I had the same issue. My problem was that for other reasons I also had org.springframework.boot:spring-boot-starter-web as dependency, so thymeleaf did not enable its reactive mode.

After I removed it, it started to work, but it only accepted a Flux and I couldn't render a single value. Looks like they only support iteration (th:each) on a stream, but I couldn't find a proper documentation only hello world examples.

My use case was different. I just did not want to block until I have my singe data object, so I came up with a workaround that also works with -web. I deferred the view name until I receive the data from the server. This way I don't need to block before I return the view name to start the rendering. (This method only works If you don't want to stream a long list) For -webflux you don't need this workaround, just simply put your Monos and Fluxes into the model. Webflux will automatically wait for them before calling thymeleaf.

WebMVC workaround:

@GetMapping("/article/{articleId}/{friendlyUrl}") fun article(@PathVariable articleId: String, @PathVariable friendlyUrl: String, model: Model): Mono<String> { return gwDispatcher.dispatch(GetArticleGwCommand(articleId)).map { it.article }.map { model.addAttribute("article", it) model.addAttribute("name", "Josh Smith"); "article" } }

Peter Borbas
  • 158
  • 6