I was trying to create simple Android application using ktor as server on the device.
I have managed to add dependecies for ktor 1.6.8 in my android project and I have tried to follow the docs I have managed to host and render very basic html page, but when trying to use model data property with thymeleaf I encounter some strange problem - the page is not rendered at all.
I.E:
<html xmlns:th="http://www.thymeleaf.org">
<body>
<h1 th:text="'Hello, ' ${user}"></h1>
</body>
</html>
This renders Hello, ThymeleafUser(id=1, name=Scott)
but changing to:
<html xmlns:th="http://www.thymeleaf.org">
<body>
<h1 th:text="'Hello, ' ${user.name}"></h1>
</body>
</html>
Doesn't render page at all (instead rendering expectedHello, Scott
)
The code for Server is more or less something taken from ktor generator and I have putted it into single kotlin class:
package com.example.testapp
import io.ktor.application.*
import io.ktor.response.*
import io.ktor.routing.*
import io.ktor.server.engine.*
import io.ktor.server.netty.*
import io.ktor.thymeleaf.*
import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver
import kotlin.concurrent.thread
class Server {
companion object {
fun startServer(){
thread(start=true) {
embeddedServer(Netty, host = "0.0.0.0", port = 12345) {
install(Thymeleaf){
setTemplateResolver(ClassLoaderTemplateResolver().apply {
prefix = "templates/"
suffix = ".html"
characterEncoding = "utf-8"
})
}
routing {
get("/html-thymeleaf") {
val sampleUser = ThymeleafUser(1, "Scott")
call.respond(ThymeleafContent("index", mapOf("user" to sampleUser)))
}
}
}.start(wait = true)
}
}
}
}
data class ThymeleafUser(val id: Int, val name: String)
The Server.startServer()
is called in Andorid MainActivity onCreate
method.
My question is: Why is this behavior occuring? Why I cannot access to property of the "user" as shown in examples or on github.
Perhaps this is just a noob question, but I don't see any logs, any stack trace, nothing to direct me how to solve this.
CodePudding user response:
Unfortunately, to evaluate expressions like ${user.name}
, Thymeleaf uses classes from the java.beans
package which aren't available on Android. In the Logcat I see the Unresolved exception class when finding catch block: java.beans.IntrospectionException
.