-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: v0.4.2 ViewContextContainer to compose pages from multiple c…
…omponents -> support htmx out of band response
- Loading branch information
Showing
6 changed files
with
103 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
src/main/kotlin/de/tschuehly/thymeleafviewcomponent/ViewContextContainer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package de.tschuehly.thymeleafviewcomponent | ||
|
||
class ViewContextContainer( | ||
vararg val viewContexts: ViewContext | ||
) |
42 changes: 42 additions & 0 deletions
42
...otlin/de/tschuehly/thymeleafviewcomponent/ViewContextContainerMethodReturnValueHandler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package de.tschuehly.thymeleafviewcomponent | ||
|
||
import jakarta.servlet.http.HttpServletRequest | ||
import jakarta.servlet.http.HttpServletResponse | ||
import org.springframework.core.MethodParameter | ||
import org.springframework.stereotype.Component | ||
import org.springframework.web.context.request.NativeWebRequest | ||
import org.springframework.web.context.support.WebApplicationObjectSupport | ||
import org.springframework.web.method.support.HandlerMethodReturnValueHandler | ||
import org.springframework.web.method.support.ModelAndViewContainer | ||
import org.thymeleaf.spring6.view.ThymeleafView | ||
import org.thymeleaf.spring6.view.ThymeleafViewResolver | ||
import java.util.* | ||
|
||
@Component | ||
class ViewContextContainerMethodReturnValueHandler( | ||
private val thymeleafViewResolver: ThymeleafViewResolver | ||
) : HandlerMethodReturnValueHandler, WebApplicationObjectSupport() { | ||
|
||
override fun supportsReturnType(returnType: MethodParameter): Boolean { | ||
return ViewContextContainer::class.java.isAssignableFrom(returnType.parameterType) | ||
} | ||
|
||
override fun handleReturnValue( | ||
returnValue: Any?, | ||
returnType: MethodParameter, | ||
mavContainer: ModelAndViewContainer, | ||
webRequest: NativeWebRequest | ||
) { | ||
val request = webRequest.getNativeRequest(HttpServletRequest::class.java)!! | ||
val response = webRequest.getNativeResponse(HttpServletResponse::class.java)!! | ||
val viewContextContainer = returnValue as ViewContextContainer | ||
viewContextContainer.viewContexts.forEach { viewContext -> | ||
val view: ThymeleafView = | ||
thymeleafViewResolver.resolveViewName(viewContext.componentTemplate!!, Locale.GERMAN) as ThymeleafView | ||
view.render(viewContext.contextAttributes.toMap(), request, response) | ||
|
||
} | ||
mavContainer.isRequestHandled = true | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters