ThymeleafConfig.java 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * Copyright 2002-2017 the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package sample;
  17. import org.springframework.context.ApplicationContext;
  18. import org.springframework.context.annotation.Bean;
  19. import org.springframework.context.annotation.Configuration;
  20. import org.springframework.web.reactive.config.ViewResolverRegistry;
  21. import org.springframework.web.reactive.config.WebFluxConfigurer;
  22. import org.thymeleaf.spring5.ISpringWebFluxTemplateEngine;
  23. import org.thymeleaf.spring5.SpringWebFluxTemplateEngine;
  24. import org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver;
  25. import org.thymeleaf.spring5.view.reactive.ThymeleafReactiveViewResolver;
  26. import org.thymeleaf.templatemode.TemplateMode;
  27. import thymeleaf.PatchThymeleafReactiveView;
  28. /**
  29. * @author Rob Winch
  30. * @since 5.0
  31. */
  32. @Configuration
  33. public class ThymeleafConfig implements WebFluxConfigurer {
  34. private ApplicationContext applicationContext;
  35. public ThymeleafConfig(final ApplicationContext applicationContext) {
  36. super();
  37. this.applicationContext = applicationContext;
  38. }
  39. @Bean
  40. public SpringResourceTemplateResolver thymeleafTemplateResolver() {
  41. SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
  42. resolver.setApplicationContext(this.applicationContext);
  43. resolver.setPrefix("classpath:/templates/");
  44. resolver.setSuffix(".html");
  45. resolver.setTemplateMode(TemplateMode.HTML);
  46. resolver.setCacheable(false);
  47. resolver.setCheckExistence(true);
  48. return resolver;
  49. }
  50. @Bean
  51. public ISpringWebFluxTemplateEngine thymeleafTemplateEngine() {
  52. SpringWebFluxTemplateEngine templateEngine = new SpringWebFluxTemplateEngine();
  53. templateEngine.setTemplateResolver(thymeleafTemplateResolver());
  54. return templateEngine;
  55. }
  56. @Bean
  57. public ThymeleafReactiveViewResolver thymeleafChunkedAndDataDrivenViewResolver() {
  58. ThymeleafReactiveViewResolver viewResolver = new ThymeleafReactiveViewResolver();
  59. viewResolver.setTemplateEngine(thymeleafTemplateEngine());
  60. viewResolver.setOrder(1);
  61. viewResolver.setResponseMaxChunkSizeBytes(8192); // OUTPUT BUFFER size limit
  62. viewResolver.setViewClass(PatchThymeleafReactiveView.class);
  63. return viewResolver;
  64. }
  65. @Override
  66. public void configureViewResolvers(ViewResolverRegistry registry) {
  67. registry.viewResolver(thymeleafChunkedAndDataDrivenViewResolver());
  68. }
  69. }