带有thymeleaf、spring boot、mysql的ckeditor

envsm3lx  于 2021-06-20  发布在  Mysql
关注(0)|答案(2)|浏览(387)

单击“保存”时,除“内容”外,所有字段都插入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";
}
xmq68pz9

xmq68pz91#

我找到了问题的答案。

<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>
yvgpqqbh

yvgpqqbh2#

您在th:字段和th:文本中使用了两次内容。删除:文本
应该是

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

相关问题