1

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";
    }

3
  • Do you get any error message or what is the result of your code? Commented Jan 2, 2020 at 15:46
  • 3
    Maybe you should start by reading the documentation: thymeleaf.org/doc/tutorials/2.1/thymeleafspring.html Commented Jan 2, 2020 at 16:06
  • Yes, an status: 500 error and the consol output says that my method getGear() cannot be found on type com.example.servingwebcontent.Overview Commented Jan 2, 2020 at 16:54

2 Answers 2

3

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

<p><span th:text="${ov}">value</span></p>
Sign up to request clarification or add additional context in comments.

Comments

0

Your th:text attribute is attempting to call a static method. You should be able to just use ${ov.gear}.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.