带有 Thymeleaf 的 Ckeditor,Spring 启动,Mysql

Ckeditor with Thymeleaf, Spring boot, Mysql

单击 "Save" 时,除 "Content" 外,所有字段都插入到 MySQL 中。

当调试字段 "Content" 为空时

帮帮我,谢谢

这是我的代码:

形式-post.html

<form action="#" th:action="@{/savepost}" th:object="${post}"
            method="POST" novalidate="novalidate">
            <!-- <input type="hidden" th:field="*{id}" /> -->
            <div class="form-group">
                <label>Title</label> <input type="text" class="form-control"
                    th:field="*{title}" th:errorclass="field-error" /> <em
                    th:if="${#fields.hasErrors('title')}" th:errors="*{title}"></em>
            </div>
            <div class="form-group">
                <label>Description</label> <input type="text"
                    class="form-control" th:field="*{description}"
                    th:errorclass="field-error" /> <em
                    th:if="${#fields.hasErrors('description')}" th:errors="*{description}"></em>
            </div>
            <div class="form-group">
                <label>Link Image</label> <input type="url"
                    class="form-control" th:field="*{image_title}" />
            </div>
            <div class="form-group">
                <label>Category</label> <input type="number"
                    class="form-control" th:field="*{id_category}" />
            </div>
            <div class="form-group">
                <label>Id Images</label> <input type="number"
                    class="form-control" th:field="*{id_images}" />
            </div>
            <!-- <input type="hidden" th:field="*{create_date}" /> -->
            <div class="form-group">
                <label>Create By</label> <input type="text"
                    class="form-control" th:field="*{create_by}" />
            </div>
            <!-- <input type="hidden" th:field="*{view}" /> -->
            <div class="form-group">
                <label>Source</label> <input type="text"
                    class="form-control" th:field="*{source_post}" />
            </div>
            <div class="form-group">
                <label>Content</label> <input type="text" id="con" name="con"
                    class="form-control" th:field="*{content}" th:text="${content}"/>
            </div>
            <button type="submit" class="btn btn-primary">Save</button>
            <button type="reset" class="btn btn-primary">Reset</button>
        <script>
            CKEDITOR.replace('con');
        </script>
        </form>

Controller.java @控制器

@GetMapping(value="/createpost")
public String create(Model model) {

    model.addAttribute("post", new Post());
    return "form-post.html";
}

@PostMapping(value="/savepost")
public String save(@Valid Post post, BindingResult result, RedirectAttributes redirect) {


    if(post.getCreate_date() == null) {
        Date date = new Date();
        post.setCreate_date(date);
    }
    if(post.getView() == null) {
        post.setView(1);
    }
    if(post.getContent() == null || post.getContent().isEmpty()) {
        post.setContent("Error insert content!!!. Because input content null or empty.");
    }
    postService.savePost(post);
    //redirect.addFlashAttribute("success", "Saved contact successfully!");
    return "redirect:/index";
}

您在 th:field 中使用了两次内容,在 th:text 中使用了一次。移除 th:text

应该是

<input type="text" id="con" name="con" class="form-control" th:field="*{content}"/>

我找到了问题的答案。

<input type="text" id="con" name="con" class="form-control" th:field="*{content}"/>

替换为:

<textarea name="cont" id="cont" rows="10" cols="80" th:field="*{content}"></textarea>