Search code examples
javaspringthymeleaf

Calling Java Method/Variable in HTML - Thymeleaf Spring Boot


I want to call an Java Object method in my HTML File, but I don't know how to do that. I think my th:text in my HTML could be the problem, but couldn't find a better solution.

My HTML:

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Create User</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<h1>Form</h1>
    <p>First Name: <input type="text" th:object="${ov}" th:text="${T(com.example.servingwebcontent.Overview).getGear()}" /></p>


</body>
</html>

My Java Object:

package com.example.servingwebcontent;

public class Overview {

    String gear;

    public void setGear(String gear) {
        this.gear = gear;
    }

    public String getGear() {
        return gear;
    }


}

And my Java Controller:

  public String handle=null;
    @RequestMapping("/overview")
    public String overview(@RequestParam(name="name", required=false) String name,@RequestParam String testOrder, Model model){
        handle=testOrder;
        model.addAttribute("ov", testOrder);
        return "overview";
    }


Solution

  • Replace <p> with the following line and it should work:

    <p><span th:text="${ov}">value</span></p>