单击“保存”时,除“内容”外,所有字段都将插入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 @Controller
@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:文本
应该
<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>