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?
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"
}
}