首页 > 解决方案 > 未将所有字段从 html 映射到控制器

问题描述

我需要更新我的类别对象

我的模型:

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.validation.constraints.NotNull;

@Entity
public class Category {
    @Id
    @GeneratedValue
    private int id;
    @NotNull
    private String name;
    private String description;
    @NotNull
    private Long created;
    private Long updated;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Long getCreated() {
        return created;
    }

    public void setCreated(Long created) {
        this.created = created;
    }

    public Long getUpdated() {
        return updated;
    }

    public void setUpdated(Long updated) {
        this.updated = updated;
    }

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

这是我的控制器:

@Controller
public class CategoryController {
    private CategoryRepository categoryRepository;

    private static Logger logger = LogManager.getLogger(CategoryController.class);

    // If class has only one constructore then @Autowired wiil execute automatically
    public CategoryController(CategoryRepository categoryRepository) {
        this.categoryRepository = categoryRepository;
        createStubCategoryList();
    }

    @PostMapping(value = "/category")
        public String submitCategory(Category category, Model model) {
            logger.info("updateCategory = " + category);
            model.addAttribute("submitted", true);
            model.addAttribute("category", category);
            categoryRepository.save(category);
            return "category";
        }

        @RequestMapping("category/edit/{id}")
        public String editCategory(@PathVariable("id") int id, Model model) {
            Optional<Category> category = categoryRepository.findById(id);
            logger.info("find_category = " + category);
            model.addAttribute("category", category);
            return "category";
        }

这是我编辑类别的模板:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title th:text="${appName}">Category template title</title>
    <link th:href="@{/public/style.css}" rel="stylesheet"/>
    <meta charset="UTF-8"/>
</head>
<body>
<div class="container">
    <form method="post" action="#" th:object="${category}" th:action="@{/category}">
        <h3>Category</h3>
        <input type="text" placeholder="name" id="name" th:field="*{name}"/>
        <textarea placeholder="Description of the category" rows="5" id="description"
                  th:field="*{description}"></textarea>
        <input type="submit" value="Submit"/>
    </form>

    <div class="result_message" th:if="${submitted}">
        <h3>Your category has been submitted.</h3>
        <p>Find all categories <a href="/categories">here</a></p>
    </div>
</div>
</body>
</html>

当日志中的调用方法有:

[INFO ] 2020-01-07 19:38:07.493 [http-nio-8090-exec-8] CategoryController - find_category = Optional[
Category{id=2, name='Electronics', created=1578418669105, updated=null, description='Electronics's description'}]

和这里的屏幕:

在此处输入图像描述

如您所见,字段created=1578418669105

好的。

现在我将名称“Electronics”编辑为“Electronics2”并单击提交。结果调用方法:submitCategory在我的控制器中。好的。

这里导致日志:

[INFO ] 2020-01-07 19:40:23.327 [http-nio-8090-exec-2] CategoryController - updateCategory = 
Category{id=0, name='Electronics2', created=null, updated=null, description='Electronics's description'}

但如您所见,创建的字段 为空。为什么?

我只需要更新可编辑字段:namedescription

其他字段(如created、updated、id)不得更改。此字段未映射。

我怎么能做到这一点?

标签: spring-bootthymeleaf

解决方案


因为 created, updated, id 你需要作为隐藏传递。它在 html 页面中不可用。


推荐阅读