Kristjano Xhava
Kristjano Xhava

Reputation: 13

Not able to use th:field of thymeleaf

I'm trying to develop a project with spring boot which will be used by employees to applicate for day off or vacations, i am also using crud operations as Rest web services, but i'm having a problem with the th:field of thymeleaf which generates the error: org.thymeleaf.exceptions.TemplateProcessingException: Error during execution of processor 'org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor' (template: "admin/home" - line 81, col 54) Below you find the code of the controller and the html

@GetMapping("/application")
public String applicationForm(Model model) {
    model.addAttribute("application", new Application());
    return "home";
}

@RequestMapping(value="/application", method=RequestMethod.POST)
public String applicationSubmit(@ModelAttribute Application application, Model model, BindingResult bindingResult, MultipartHttpServletRequest request) throws IOException {
    if (bindingResult.hasErrors()) 
        return "home";

    model.addAttribute("application", application);

    return "result";
}

<form action="#" th:action="@{'createApplication'}" th:object="${application}" method="post">

            Description: <input type="text" id="description" th:field="*{description}"/>    
            From Date: <input type="text" id="from" th:field="*{fromDate}" />
            To Date: <input type="text" id="to" th:field="*{toDate}"/>

            <button type="submit" class="btn btn-primary">Submit</button>

            </form>

Upvotes: 0

Views: 1502

Answers (1)

Supun Dharmarathne
Supun Dharmarathne

Reputation: 1148

Use this application class. This exception occurs when the fields are not accessible from thymeleaf, either they are not public or do not have public access (getters / setters).

public class Application {
private String description;
private String fromDate;
private String toDate;

public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}

public String getFromDate() {
    return fromDate;
}

public void setFromDate(String fromDate) {
    this.fromDate = fromDate;
}

public String getToDate() {
    return toDate;
}

public void setToDate(String toDate) {
    this.toDate = toDate;
}

}

Upvotes: 1

Related Questions