Home > Enterprise >  How do I pass an object with thymeleaf to java spring controller?
How do I pass an object with thymeleaf to java spring controller?

Time:12-03

I can't pass a object Project without the object is null. How do I pass the object to the spring controller from the html?

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
    <head>
        <meta charset="UTF-8">
        <title>Home</title>
    </head>
    <body>
        <form action="/addproject" method="POST">
            <input type="hidden" name="project" th:value="${project}"/>
            <input type="submit" value="Save Project" class="submit">
        </form>
    </body>
</html>
@Controller
public class ProjectController {

    @GetMapping("/")
    public String indexPage(Model model) {
        model.addAttribute("project", new Project("Test", "Test"));
        return "index";
    }

    @PostMapping("/addproject")
    public String add(WebRequest webRequest) {
        Project p = (Project) webRequest.getAttribute("project", WebRequest.SCOPE_REQUEST);
        return "index";
    }
}

CodePudding user response:

Please check this article. In short you need to pass required object to ModelAndView, not Model:

@GetMapping("/")
public ModelAndView indexPage(Model model) {
    ModelAndView mav = new ModelAndView("index");
    mav.addObject("project", new Project("Test", "Test"));
    return mav;
}

and access it by name from template:

<b th:text="${project.attribute}">Text ...</b>

CodePudding user response:

I would suggest you to proceed with the document here first.

You don't have a requirement or problem with ModelAndView.

The first time you call the /project page, the project class information will be loaded by @GetMapping, if you want, it will be captured by @PostMapping when you change and submit it.

project.html file:

<!DOCTYPE HTML>
<html xmlns:th="https://www.thymeleaf.org">
<head>
    <title>Project Form</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<h1>Project Form</h1>
<form method="post" th:action="@{/project}" th:object="${project}">
    <p>Name: <input type="text" th:field="*{name}" /></p>
    <p>Description: <input type="text" th:field="*{description}" /></p>
    <p><input type="submit" value="Submit" /> </p>
</form>
</body>
</html>

Project.java file:

public class Project {

    private String name;
    private String description;

    public Project(String name, String description) {
        this.name = name;
        this.description = description;
    }

    // getter/setter ...
}

ProjectController.java file:

@Controller
public class ProjectController {

    @GetMapping("/project")
    public String greetingForm(Model model) {
        model.addAttribute("project", new Project("PN1", "PD1"));
        return "project";
    }

    @PostMapping("/project")
    public String greetingSubmit(@ModelAttribute Project project, Model model) {
        model.addAttribute("project", project);
        return "project";
    }
}
  • Related