Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用Thymeleaf和Spring MVC编辑对象的最佳方法_Java_Spring Mvc_Thymeleaf - Fatal编程技术网

Java 使用Thymeleaf和Spring MVC编辑对象的最佳方法

Java 使用Thymeleaf和Spring MVC编辑对象的最佳方法,java,spring-mvc,thymeleaf,Java,Spring Mvc,Thymeleaf,我在一个网站上工作,在这个网站上,用户可以添加和编辑东西(这与描述这些东西的具体内容无关)。 我使用Thymeleaf实现前端,SpringMVC实现后端,JPA实现数据库逻辑。现在我正在尝试实现编辑逻辑,但我不知道哪种方法是最好的。 我的想法是:向用户显示允许他编辑的所有输入字段(在HTML页面上),这些字段已经用当前值填充。然后他可以编辑他想要的任何字段,最后按下编辑按钮来保存更改。 在后端获得新对象后,我从数据库中检索旧版本,以检查用户更改了哪个字段。对于每个被更改的字段,我都会更新旧版本

我在一个网站上工作,在这个网站上,用户可以添加和编辑东西(这与描述这些东西的具体内容无关)。 我使用Thymeleaf实现前端,SpringMVC实现后端,JPA实现数据库逻辑。现在我正在尝试实现编辑逻辑,但我不知道哪种方法是最好的。 我的想法是:向用户显示允许他编辑的所有输入字段(在HTML页面上),这些字段已经用当前值填充。然后他可以编辑他想要的任何字段,最后按下编辑按钮来保存更改。 在后端获得新对象后,我从数据库中检索旧版本,以检查用户更改了哪个字段。对于每个被更改的字段,我都会更新旧版本,只有当我完成后,我才会调用JPA方法save并保存该对象的新版本。 有更好的方法吗? 如果我将对象放在模型中,以便在HTML页面中向用户显示它的所有字段,可以保存旧对象的所有信息,而不仅仅是用户可以更改的信息,那将是完美的。让我用一个例子更好地解释我想说的:

假设我们试图编辑其被调用的Person的对象具有以下属性:

  • 身份证
  • 名字
  • 绰号
但用户只能编辑以下属性:

  • 绰号
因此,处理页面get请求的控制器如下所示:

@GetMapping("person/{personId}/edit")
public String getEditPersonPage(@PathVariable Integer personId, Model model) {
    Person person = personService.getById(personId); //person has all the attributes filled in
    model.addAttribute("person", person);
@PutMapping("person/{personId}/edit")
    public String editPerson(@ModelAttribute Person person, @PathVariable Integer personId){
        personService.editPerson(person); //person has only the three fields filled in and all the other attributes as NULL
        return "redirect:/person/" + personId;
    }
处理put请求的控制器如下所示:

@GetMapping("person/{personId}/edit")
public String getEditPersonPage(@PathVariable Integer personId, Model model) {
    Person person = personService.getById(personId); //person has all the attributes filled in
    model.addAttribute("person", person);
@PutMapping("person/{personId}/edit")
    public String editPerson(@ModelAttribute Person person, @PathVariable Integer personId){
        personService.editPerson(person); //person has only the three fields filled in and all the other attributes as NULL
        return "redirect:/person/" + personId;
    }
HTML页面:

<form th:object="${person}" th:method="PUT">
    <fieldset>
        <p th:text="*{id}"></p>
        <p th:text="*{name}"></p>
        <p th:text="*{surname}"></p>

        <input type="number" th:field="*{money}" th:value="*{money}" />
        <input type="text" th:field="*{nickname}" th:value="*{nickname}" />
        <input type="text" th:field="*{sex}" th:value="*{sex}" />

        <input type="submit" value="Edit" />
    </fieldset>
</form>

当我在模型中插入object person以呈现HTML页面时,该对象具有所有属性。事实上,我可以决定它的哪些属性显示在HTML页面中(在本例中只有三个:金钱、昵称和性别)。但是当用户按下提交按钮(编辑)时,我只收到HTML页面中显示的字段(金钱、昵称、性别)。所以我要做的是:我必须获取这三个字段,检查它们是否已更改,如果已更改,则更新旧版本。 如果用户按下submit按钮时,控制器可以检索person的所有字段(而不仅仅是允许他更新的三个字段),这将是完美的。在这种情况下,我可以跳过检查阶段,直接将新版本保存在数据库中(旧值保持不变,不设置为NULL)


有什么想法吗?

您可以对不需要编辑的属性使用
只读属性

我认为这是保持表单中所有值可见的最好方法,以便将所有信息提供给正在编辑的用户。所有值都将随对象一起提交和序列化,但表单只允许编辑您需要的三个值


readonly
元素不可编辑,但在相应表单提交时发送。禁用的
元素是不可编辑的,提交时不会发送。

是的,但如果我使用浏览器的inspect工具删除输入字段的readonly属性会怎么样?我不完全确定,但我认为这涵盖了您的问题,因为我正在研究相同的挑战。