Not able to use th:field of thymeleaf

本小妞迷上赌 提交于 2021-01-29 07:43:13

问题


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>

回答1:


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

}



来源:https://stackoverflow.com/questions/54425204/not-able-to-use-thfield-of-thymeleaf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!