简体   繁体   English

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

[英]How do I pass an object with thymeleaf to java spring controller?

I can't pass a object Project without the object is null.如果没有 object 是 null,我无法通过 object项目 How do I pass the object to the spring controller from the html?如何将 object 传递给 spring controller?

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

Please checkthis article .请检查这篇文章 In short you need to pass required object to ModelAndView , not Model :简而言之,您需要将所需的 object 传递给ModelAndView ,而不是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>

I would suggest you to proceed with the document here first.我建议您先在此处处理文档

As in the other answer, you don't have a requirement or problem with ModelAndView .与其他答案一样,您对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页面时,@GetMapping 会加载项目@GetMapping信息,如果需要,在更改并提交时会被@PostMapping捕获。

add-project.html file: add-project.html文件:

<!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: Project.java文件:

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: ProjectController.java文件:

@Controller
public class ProjectController {

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM