Home > database >  how to fix 500 status err in form submission template using thymeleaf?
how to fix 500 status err in form submission template using thymeleaf?

Time:01-09

I wrote some code using Spring docs: https://spring.io/guides/gs/handling-form-submission/

I think I made same thing like in docs but my code returns me (type=Internal Server Error, status=500).

Here my code:

FriendListsModel.java

    @Entity
public class FriendListsModel {

    @Id
    private Integer id;

    private String friendLists;
// constructor, getters, setters, equal, hashcode, toString (all autogenerated)

addController.java

 @Controller
public class addController {

    @GetMapping("/add2")
    public String addForm(Model model) {
        model.addAttribute("form", new FriendListsModel());
        return "form";
    }

    @PostMapping("/add2")
    public String addSubmit(@ModelAttribute FriendListsModel friendListsModel, Model model) {
        model.addAttribute("form", friendListsModel);
        return "result";
    }
}

form.html

    <!DOCTYPE HTML>
<html xmlns:th="https://www.thymeleaf.org">
<head> 
    <title>Getting Started: Handling Form Submission</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
    <h1>Form</h1>
    <form action="#" th:action="@{/add2}" th:object="${form}" method="post">
        <p>Id: <input type="text" th:field="*{id}" /></p>
        <p>Message: <input type="text" th:field="*{friendLists}" /></p>
        <p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p>
    </form>
</body>
</html>

result.html

<!DOCTYPE HTML>
<html xmlns:th="https://www.thymeleaf.org">
<head> 
    <title>Getting Started: Handling Form Submission</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
    <h1>Result</h1>
    <p th:text="'id: '   ${form.id}" />
    <p th:text="'content: '   ${form.friendLists}" />
    <a href="/add2">Submit another message</a>
</body>
</html>

Logs:

Caused by: org.springframework.beans.NotReadablePropertyException: Invalid property 'friendLists' of bean class [com.hoff.blog.FriendListsModel]: Bean property 'friendLists' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?
2022-01-05 20:23:17.347 DEBUG 16504 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Failed to complete request: org.thymeleaf.exceptions.TemplateProcessingException: Error during execution of processor 'org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor' (template: "form" - line 11, col 40)     
2022-01-05 20:23:17.358 ERROR 16504 --- [nio-8080-exec-1] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateProcessingException: Error during execution of processor 'org.thymeleaf.spring5.processor.SpringInputGeneralFieldTagProcessor' (template: "form" - line 11, col 40)] with root cause
2022-01-05 20:23:17.388 DEBUG 16504 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : "ERROR" dispatch for GET "/error", parameters={}
2022-01-05 20:23:17.391 DEBUG 16504 --- [nio-8080-exec-1] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped to org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController#errorHtml(HttpServletRequest, HttpServletResponse)
2022-01-05 20:23:17.412 DEBUG 16504 --- [nio-8080-exec-1] o.s.w.s.v.ContentNegotiatingViewResolver : Selected 'text/html' given [text/html, text/html;q=0.8]
2022-01-05 20:23:17.414 DEBUG 16504 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet        : Exiting from "ERROR" dispatch, status 500

CodePudding user response:

Problem was in my Model, I just changed field name before and forgot to change getter and setter names.

  •  Tags:  
  • Related