I'm trying to display a json data into an HTML table. but I found that every table's contents contain a "..." in each variable, like this:
I want the "..." sign of each variable to disappear. here is my code:
The controller method:
@GetMapping()
fun home(model: Model): String {
val users = userService.getAllUsers()
model.addAttribute("tableAttributes", jsonHelper.getMembersKeyValues(User()))
model.addAttribute("tableData", users.map { jsonHelper.getSeparatedMembersByKeys(it) })
return "home"
}
Here is the HTML:
<div class="container-sm">
<table class="table">
<thead>
<tr>
<th scope="col" th:each="attribute: ${tableAttributes}" th:text="${attribute}">#</th>
</tr>
</thead>
<tbody>
<tr th:each="data: ${tableData}">
<td th:each="variable: ${data}" th:text="${variable}"></td>
</tr>
</tbody>
</table>
</div>
The JsonHelper class:
@Component
class JsonHelper() {
private val gson = Gson().newBuilder().serializeNulls().create()
fun <T> toJsonObject(model: T): JsonObject {
val jsonString = gson.toJson(model)
return gson.fromJson(jsonString, JsonObject::class.java)
}
fun <T> getMembersKeyValues(model: T): List<String> {
val jsonObject = toJsonObject(model)
return jsonObject.keySet().toList()
}
fun <T> getSeparatedMembersByKeys(model: T): JsonArray {
val jsonObject = toJsonObject(model)
val keys = jsonObject.keySet().toList()
val data = JsonArray()
for (key in keys) {
data.add(jsonObject.get(key))
}
return data
}
}
I use dummy data like this:
data class User(
var username: String? = null,
var password: String? = null
): Serializable