11package de.tschuehly.spring.viewcomponent.thymeleaf
22
33import de.tschuehly.spring.viewcomponent.core.IViewContext
4+ import jakarta.servlet.http.HttpServletResponse
45import org.slf4j.LoggerFactory
56import org.springframework.context.ApplicationContext
67import org.springframework.web.context.request.RequestContextHolder
@@ -16,6 +17,7 @@ import org.thymeleaf.processor.element.IElementTagStructureHandler
1617import org.thymeleaf.spring6.view.ThymeleafViewResolver
1718import org.thymeleaf.templatemode.TemplateMode
1819import java.nio.charset.StandardCharsets
20+ import java.util.*
1921
2022
2123class ThymeleafViewComponentTagProcessor (dialectPrefix : String , private val applicationContext : ApplicationContext ) :
@@ -58,22 +60,31 @@ class ThymeleafViewComponentTagProcessor(dialectPrefix: String, private val appl
5860 throw ThymeleafViewComponentException (e.message, e.cause)
5961 }
6062 val response = (RequestContextHolder .getRequestAttributes() as ServletRequestAttributes ? )?.response!!
63+ val wrapper = renderViewComponent(response, viewContext, webContext.locale)
64+ val viewComponentBody = webContext.modelFactory.createText(
65+ wrapper
66+ )
67+ structureHandler.replaceWith(viewComponentBody, true )
68+
69+ }
70+
71+ private fun renderViewComponent (
72+ response : HttpServletResponse ,
73+ viewContext : IViewContext ,
74+ locale : Locale
75+ ): String {
6176 val wrapper = ContentCachingResponseWrapper (response)
6277 val thymeleafViewResolver = applicationContext.getBean(ThymeleafViewResolver ::class .java)
6378 val viewName = thymeleafViewResolver.resolveViewName(
6479 IViewContext .getViewComponentTemplateWithoutSuffix(viewContext),
65- webContext. locale
66- ) ? : throw ThymeleafViewComponentException (" No ViewName" , null )
80+ locale
81+ ) ? : throw ThymeleafViewComponentException (" No ViewName" , null )
6782 viewName.render(
6883 mapOf (viewContext.javaClass.simpleName.replaceFirstChar { it.lowercase() } to viewContext),
6984 (RequestContextHolder .getRequestAttributes() as ServletRequestAttributes ? )?.request!! ,
7085 wrapper
7186 )
72- val viewComponentBody = webContext.modelFactory.createText(
73- String (wrapper.contentAsByteArray, StandardCharsets .UTF_8 )
74- )
75- structureHandler.replaceWith(viewComponentBody, true )
76-
87+ return String (wrapper.contentAsByteArray, StandardCharsets .UTF_8 )
7788 }
7889
7990}
0 commit comments