Skip to content

Commit

Permalink
release: v0.8.2 Thymeleaf Tag Processor now uses the Thymeleaf ViewRe…
Browse files Browse the repository at this point in the history
…solver to render nested ViewComponents allowing to use th:field
  • Loading branch information
tschuehly committed Jun 27, 2024
1 parent 61b6c99 commit 4d19d1d
Show file tree
Hide file tree
Showing 20 changed files with 85 additions and 58 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ spring.view-component.local-development=true
<summary>Gradle</summary>

```kotlin
implementation("de.tschuehly:spring-view-component-thymeleaf:0.8.1")
implementation("de.tschuehly:spring-view-component-thymeleaf:0.8.2")
sourceSets {
main {
resources {
Expand All @@ -249,7 +249,7 @@ sourceSets {
<dependency>
<groupId>de.tschuehly</groupId>
<artifactId>spring-view-component-thymeleaf</artifactId>
<version>0.8.1</version>
<version>0.8.2</version>
</dependency>
</dependencies>
<build>
Expand Down Expand Up @@ -286,7 +286,7 @@ plugins {
id("gg.jte.gradle") version("3.1.12")
}

implementation("de.tschuehly:spring-view-component-jte:0.8.1")
implementation("de.tschuehly:spring-view-component-jte:0.8.2")

jte{
generate()
Expand All @@ -305,7 +305,7 @@ jte{
<dependency>
<groupId>de.tschuehly</groupId>
<artifactId>spring-view-component-jte</artifactId>
<version>0.8.1</version>
<version>0.8.2</version>
</dependency>
</dependencies>
<build>
Expand Down Expand Up @@ -343,7 +343,7 @@ jte{
<summary>Gradle</summary>

```kotlin
implementation("de.tschuehly:spring-view-component-kte:0.8.1")
implementation("de.tschuehly:spring-view-component-kte:0.8.2")
jte{
generate()
sourceDirectory = Path("src/main/kotlin")
Expand Down
2 changes: 1 addition & 1 deletion core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ plugins {
}

group = "de.tschuehly"
version = "0.8.1"
version = "0.8.2"
java.sourceCompatibility = JavaVersion.VERSION_17

repositories {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ import org.springframework.boot.test.web.client.TestRestTemplate
import org.springframework.http.HttpMethod
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity

abstract class IntegrationTestBase {
@Autowired
lateinit var testRestTemplate: TestRestTemplate

@Test
fun testIndexComponent() {
val expectedHtml =
Expand All @@ -37,6 +39,9 @@ abstract class IntegrationTestBase {
<div>
<h2>This is the SimpleViewComponent</h2>
<div>Hello World</div>
<div>
<input id="helloWorld" name="helloWorld" value="Hello World">
</div>
</div>
""".trimIndent()
assertEndpointReturns("/simple", expectedHtml)
Expand All @@ -45,34 +50,35 @@ abstract class IntegrationTestBase {
@Test
fun testLayoutComponent() {
//language=html
val expectedHtml =
"""
val expectedHtml = """
<html>
<nav>This is a Navbar</nav>
<body>
<div><h2>This is the SimpleViewComponent</h2>
<div>Hello World</div></div></body>
<footer>This is a footer</footer></html>
<div>Hello World</div>
<div>
<input id="helloWorld" name="helloWorld" value="Hello World">
</div>
</div>
</body>
<footer>This is a footer</footer>
</html>
""".trimIndent()
assertEndpointReturns("/layout", expectedHtml)
}

fun assertEndpointReturns(url: String, expectedHtml: String) {
val response: ResponseEntity<String> = this.testRestTemplate
.exchange(url, HttpMethod.GET, null, String::class.java)
assertThat(response.statusCode)
.isEqualTo(HttpStatus.OK)
val response: ResponseEntity<String> =
this.testRestTemplate.exchange(url, HttpMethod.GET, null, String::class.java)
assertThat(response.statusCode).isEqualTo(HttpStatus.OK)
Assertions.assertEquals(
expectedHtml.rmWhitespaceBetweenHtmlTags(), response.body?.rmWhitespaceBetweenHtmlTags()
)
}

fun String.rmWhitespaceBetweenHtmlTags(): String {
// Replace whitespace between > and word
return this.replace("(?<=>)(\\s*)(?=\\w)".toRegex(), "")
.replace("(?<=\\w)(\\s*)(?=<)".toRegex(), "")
.replace("(?<=>)(\\s*)(?=<)".toRegex(), "")
.replace("\r\n","\n")
.trim()
return this.replace("(?<=>)(\\s*)(?=\\w)".toRegex(), "").replace("(?<=\\w)(\\s*)(?=<)".toRegex(), "")
.replace("(?<=>)(\\s*)(?=<)".toRegex(), "").replace("\r\n", "\n").trim()
}
}
2 changes: 1 addition & 1 deletion examples/.run/Run all example tests.run.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<option name="executionName" />
<option name="externalProjectPath" value="$PROJECT_DIR$/examples" />
<option name="externalSystemIdString" value="GRADLE" />
<option name="scriptParameters" value="" />
<option name="scriptParameters" value="--stacktrace --stacktrace --stacktrace --stacktrace" />
<option name="taskDescriptions">
<list />
</option>
Expand Down
4 changes: 2 additions & 2 deletions examples/jte-example/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ repositories {
dependencies {
implementation("org.springframework.boot:spring-boot-starter-web")

implementation("de.tschuehly:spring-view-component-jte:0.8.1")
implementation("de.tschuehly:spring-view-component-jte:0.8.2")

implementation("org.webjars.npm:htmx.org:1.9.11")
implementation("org.webjars:webjars-locator-core:0.58")

testImplementation("org.springframework.boot:spring-boot-starter-test")
testRuntimeOnly("org.springframework.boot:spring-boot-devtools")
testImplementation(testFixtures("de.tschuehly:spring-view-component-core:0.8.1"))
testImplementation(testFixtures("de.tschuehly:spring-view-component-core:0.8.2"))
}

tasks.withType<Test> {
Expand Down
2 changes: 1 addition & 1 deletion examples/jte-example/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<description>JTE Example</description>
<properties>
<java.version>17</java.version>
<view.component.version>0.8.1</view.component.version>
<view.component.version>0.8.2</view.component.version>
</properties>
<dependencies>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@
<div>
<h2>This is the SimpleViewComponent</h2>
<div>${simpleView.helloWorld()}</div>
<div>
<input id="helloWorld" name="helloWorld" value="${simpleView.helloWorld()}">
</div>
</div>
6 changes: 3 additions & 3 deletions examples/kte-example/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ repositories {
}

dependencies {
implementation("de.tschuehly:spring-view-component-kte:0.8.1")
implementation("de.tschuehly:spring-view-component-core:0.8.1")
implementation("de.tschuehly:spring-view-component-kte:0.8.2")
implementation("de.tschuehly:spring-view-component-core:0.8.2")
implementation("io.github.wimdeblauwe:htmx-spring-boot:3.1.1")

implementation("org.webjars.npm:htmx.org:1.9.11")
Expand All @@ -47,7 +47,7 @@ dependencies {

testImplementation("org.springframework.boot:spring-boot-starter-test")
testImplementation("org.springframework.boot:spring-boot-devtools")
testImplementation(testFixtures("de.tschuehly:spring-view-component-core:0.8.1"))
testImplementation(testFixtures("de.tschuehly:spring-view-component-core:0.8.2"))
}

tasks.withType<KotlinCompile> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@
<div>
<h2>This is the SimpleViewComponent</h2>
<div>${simpleView.helloWorld}</div>
<div>
<input id="helloWorld" name="helloWorld" value="${simpleView.helloWorld}">
</div>
</div>
4 changes: 2 additions & 2 deletions examples/thymeleaf-java-example/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@ repositories {

dependencies {
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("de.tschuehly:spring-view-component-thymeleaf:0.8.1")
implementation("de.tschuehly:spring-view-component-thymeleaf:0.8.2")

implementation("org.webjars.npm:htmx.org:1.9.11")
implementation("org.webjars:webjars-locator-core:0.58")

testImplementation("org.springframework.boot:spring-boot-starter-test")
testImplementation("org.springframework.boot:spring-boot-devtools")
testImplementation(testFixtures("de.tschuehly:spring-view-component-core:0.8.1"))
testImplementation(testFixtures("de.tschuehly:spring-view-component-core:0.8.2"))
}

tasks.withType<Test> {
Expand Down
2 changes: 1 addition & 1 deletion examples/thymeleaf-java-example/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<description>Thymeleaf Java Example</description>
<properties>
<java.version>17</java.version>
<view.component.version>0.8.1</view.component.version>
<view.component.version>0.8.2</view.component.version>
</properties>
<dependencies>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@
<h2>This is the SimpleViewComponent</h2>
<!--/*@thymesVar id="simpleView" type="de.tschuehly.example.thymeleafjava.web.simple.SimpleViewComponent.SimpleView"*/-->
<div th:text="${simpleView.helloWorld()}"></div>

<div th:object="${simpleView}">
<input th:field="*{helloWorld}">
</div>
</div>
6 changes: 3 additions & 3 deletions examples/thymeleaf-kotlin-example/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ repositories {
}

dependencies {
implementation("de.tschuehly:spring-view-component-thymeleaf:0.8.1")
implementation("de.tschuehly:spring-view-component-core:0.8.1")
implementation("de.tschuehly:spring-view-component-thymeleaf:0.8.2")
implementation("de.tschuehly:spring-view-component-core:0.8.2")

implementation("org.webjars.npm:htmx.org:1.9.2")
implementation("org.webjars:webjars-locator:0.47")
Expand All @@ -35,7 +35,7 @@ dependencies {
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")

testImplementation("org.springframework.boot:spring-boot-devtools")
testImplementation(testFixtures("de.tschuehly:spring-view-component-core:0.8.1"))
testImplementation(testFixtures("de.tschuehly:spring-view-component-core:0.8.2"))
}

tasks.withType<KotlinCompile> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@
<h2>This is the SimpleViewComponent</h2>
<!--/*@thymesVar id="simpleView" type="de.tschuehly.example.thymeleafkotlin.web.simple.SimpleViewComponent.SimpleView"*/-->
<div th:text="${simpleView.helloWorld}"></div>
<div th:object="${simpleView}">
<input th:field="*{helloWorld}">
</div>
</div>
4 changes: 2 additions & 2 deletions jte/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ plugins {
}

group = "de.tschuehly"
version = "0.8.1"
version = "0.8.2"
java.sourceCompatibility = JavaVersion.VERSION_17

repositories {
Expand All @@ -22,7 +22,7 @@ repositories {
}
dependencies {
api("gg.jte:jte:3.1.12")
api("de.tschuehly:spring-view-component-core:0.8.1")
api("de.tschuehly:spring-view-component-core:0.8.2")

implementation("gg.jte:jte-spring-boot-starter-3:3.1.12")
implementation("org.springframework.boot:spring-boot-starter-web")
Expand Down
4 changes: 2 additions & 2 deletions kte/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ plugins {
}

group = "de.tschuehly"
version = "0.8.1"
version = "0.8.2"
java.sourceCompatibility = JavaVersion.VERSION_17

repositories {
mavenCentral()
}
dependencies {
api("gg.jte:jte-kotlin:3.1.12")
api("de.tschuehly:spring-view-component-core:0.8.1")
api("de.tschuehly:spring-view-component-core:0.8.2")

implementation("gg.jte:jte-spring-boot-starter-3:3.1.12")
implementation("org.springframework.boot:spring-boot-starter-web")
Expand Down
4 changes: 2 additions & 2 deletions thymeleaf/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ plugins {
}

group = "de.tschuehly"
version = "0.8.1"
version = "0.8.2"
java.sourceCompatibility = JavaVersion.VERSION_17

repositories {
mavenCentral()
}

dependencies {
api("de.tschuehly:spring-view-component-core:0.8.1")
api("de.tschuehly:spring-view-component-core:0.8.2")
api("org.springframework.boot:spring-boot-devtools")
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("org.springframework.boot:spring-boot-starter-thymeleaf")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package de.tschuehly.spring.viewcomponent.thymeleaf
import de.tschuehly.spring.viewcomponent.core.ViewComponentAutoConfiguration
import de.tschuehly.spring.viewcomponent.core.component.ViewComponentProperties
import org.springframework.beans.factory.ObjectProvider
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
import org.springframework.boot.autoconfigure.thymeleaf.ThymeleafProperties
import org.springframework.context.ApplicationContext
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.context.annotation.Import
Expand All @@ -26,7 +26,8 @@ class ThymeleafViewComponentAutoConfiguration {
fun templateEngine(
properties: ThymeleafProperties,
templateResolvers: ObjectProvider<ITemplateResolver>,
dialects: ObjectProvider<IDialect>
dialects: ObjectProvider<IDialect>,
applicationContext: ApplicationContext
): SpringTemplateEngine {
val engine = SpringTemplateEngine()
engine.enableSpringELCompiler = properties.isEnableSpringElCompiler
Expand All @@ -41,7 +42,7 @@ class ThymeleafViewComponentAutoConfiguration {
dialect
)
}
engine.addDialect(ThymeleafViewComponentDialect())
engine.addDialect(ThymeleafViewComponentDialect(applicationContext))
return engine
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package de.tschuehly.spring.viewcomponent.thymeleaf

import org.springframework.context.ApplicationContext
import org.thymeleaf.dialect.AbstractProcessorDialect
import org.thymeleaf.processor.IProcessor
import org.thymeleaf.standard.StandardDialect

class ThymeleafViewComponentDialect : AbstractProcessorDialect(
class ThymeleafViewComponentDialect(private val applicationContext: ApplicationContext) : AbstractProcessorDialect(
"ViewComponent Dialect", "view", StandardDialect.PROCESSOR_PRECEDENCE
) {
override fun getProcessors(dialectPrefix: String): MutableSet<IProcessor> {
return mutableSetOf(
ThymeleafViewComponentTagProcessor(dialectPrefix)
ThymeleafViewComponentTagProcessor(dialectPrefix,applicationContext)
)

}
Expand Down
Loading

0 comments on commit 4d19d1d

Please sign in to comment.