Skip to content

Commit ce1e9f0

Browse files
committed
renderViewComponent seperate method
1 parent b4e27b9 commit ce1e9f0

1 file changed

Lines changed: 18 additions & 7 deletions

File tree

thymeleaf/src/main/kotlin/de/tschuehly/spring/viewcomponent/thymeleaf/ThymeleafViewComponentTagProcessor.kt

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package de.tschuehly.spring.viewcomponent.thymeleaf
22

33
import de.tschuehly.spring.viewcomponent.core.IViewContext
4+
import jakarta.servlet.http.HttpServletResponse
45
import org.slf4j.LoggerFactory
56
import org.springframework.context.ApplicationContext
67
import org.springframework.web.context.request.RequestContextHolder
@@ -16,6 +17,7 @@ import org.thymeleaf.processor.element.IElementTagStructureHandler
1617
import org.thymeleaf.spring6.view.ThymeleafViewResolver
1718
import org.thymeleaf.templatemode.TemplateMode
1819
import java.nio.charset.StandardCharsets
20+
import java.util.*
1921

2022

2123
class 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

Comments
 (0)