Home > Enterprise >  Why isn't the thymeleaf object recognized?
Why isn't the thymeleaf object recognized?

Time:01-21

The first code snipped is me adding(or rather trying) the attribute 'NewUser' to use in html.

The error pops up for 'th:field="*{name}"'.

Error: Error during execution of processor 'org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor'

@RequestMapping("home")
public String newUser(Model model) {
    User newUser = new User();
    newUser.setId(100l);
    newUser.setName("Test");
    newUser.setEmail("[email protected]");
    newUser.setPermission(0);
    model.addAttribute(newUser);
    return "index";
}

<form action="#" th:action="@{/home/add}" th:object="${newUser}" method="get">
    <p>Id: <input type="text" th:field="*{name}" /></p>
    <p>Message: <input type="text" th:field="*{email}" /></p>
    <p>Message: <input type="text" th:field="*{permission}" /></p>
    <p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p>
</form>

CodePudding user response:

I think the cause is you didn't create the getter method for the fileds: name, email, and permission. So, when the thymeleaf try to render the page, it cannot read the value of the name field, or email field, or permission field.

CodePudding user response:

I was adding another model and it seems like it was causing issues, so I ended up doing this and everything is working fine.

The 'userList' model is used elsewhere in the code.

@GetMapping("home")
public String index(Model model) {
    User user = new User();
    model.addAttribute("user", user);
    model.addAttribute("userList", userService.getUsers());
    return "index";
}

<form th:object="${user}" th:action="@{/home/add}" method="get">
    <p>Id: <input type="text" th:field="*{name}" /></p>
    <p>Message: <input type="text" th:field="*{email}" /></p>
    <p>Message: <input type="text" th:field="*{permission}" /></p>
    <p><input type="submit" value="Submit" /> <input/></p>
</form>
  •  Tags:  
  • Related