Skip to content

Commit

Permalink
save
Browse files Browse the repository at this point in the history
  • Loading branch information
tschuehly committed Jun 27, 2024
1 parent 61b6c99 commit 29221a9
Showing 1 changed file with 15 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@ package de.tschuehly.spring.viewcomponent.thymeleaf

import de.tschuehly.spring.viewcomponent.core.IViewContext
import org.slf4j.LoggerFactory
import org.springframework.web.context.request.RequestContextHolder
import org.springframework.web.context.request.ServletRequestAttributes
import org.springframework.web.util.ContentCachingResponseWrapper
import org.thymeleaf.context.ITemplateContext
import org.thymeleaf.context.WebEngineContext
import org.thymeleaf.engine.AttributeName
import org.thymeleaf.engine.EngineEventUtils
import org.thymeleaf.model.IProcessableElementTag
import org.thymeleaf.processor.element.AbstractAttributeTagProcessor
import org.thymeleaf.processor.element.IElementTagStructureHandler
import org.thymeleaf.spring6.SpringTemplateEngine
import org.thymeleaf.spring6.context.SpringContextUtils
import org.thymeleaf.spring6.view.ThymeleafView
import org.thymeleaf.templatemode.TemplateMode
import java.nio.charset.StandardCharsets


class ThymeleafViewComponentTagProcessor(dialectPrefix: String) :
Expand Down Expand Up @@ -55,16 +58,21 @@ class ThymeleafViewComponentTagProcessor(dialectPrefix: String) :
logger.error("Could not execute expression: \"${expression.stringRepresentation}\"")
throw ThymeleafViewComponentException(e.message, e.cause)
}
val appCtx = SpringContextUtils.getApplicationContext(webContext)
val engine = appCtx.getBean(SpringTemplateEngine::class.java)

webContext.setVariable(viewContext.javaClass.simpleName.replaceFirstChar { it.lowercase() }, viewContext)
val response = (RequestContextHolder.getRequestAttributes() as ServletRequestAttributes?)?.response!!
val wrapper = ContentCachingResponseWrapper(response)
var thymeleafView = ThymeleafView(IViewContext.getViewComponentTemplateWithoutSuffix(viewContext))

thymeleafView.render(
mapOf(viewContext.javaClass.simpleName.replaceFirstChar { it.lowercase() } to viewContext),
(RequestContextHolder.getRequestAttributes() as ServletRequestAttributes?)?.request!!,
wrapper
)

// TODO: Cannot process attribute '{th:field,data-th-field}': no associated BindStatus could be found for the intended form binding operations. This can be due to the lack of a proper management of the Spring RequestContext, which is usually done through the ThymeleafView or ThymeleafReactiveView (template:
val modelFactory = webContext.modelFactory
engine.enableSpringELCompiler = true
val viewComponentBody = modelFactory.createText(
engine.process(IViewContext.getViewComponentTemplateWithoutSuffix(viewContext), webContext)
String(wrapper.contentAsByteArray, StandardCharsets.UTF_8)
)
structureHandler.replaceWith(viewComponentBody, true)

Expand Down

0 comments on commit 29221a9

Please sign in to comment.