【发布时间】:2020-08-17 11:17:54
【问题描述】:
我有简单的实体...
@Entity
public class WatchedDirectory {
@Column(nullable=false)
@NotBlank(message="Filesystem path CANNOT be empty")
String filesystemPath;
}
... 和 GET 端点用于创建一个 ...
@GetMapping("/add")
public String add(@ModelAttribute WatchedDirectory watchedDirectory) {
return "mng-dir-add";
}
... 显示在 Thymeleaf 中制作的表单,带有错误验证等。一旦你点击 sumbmit 按钮,数据就会进入 POST 端点......
@PostMapping("/add")
public String addExecute(@Valid @ModelAttribute WatchedDirectory watchedDirectory, BindingResult result, RedirectAttributes redirect, Model model) {
if(result.hasErrors()) {
// here I want to iterate through
// errors and clean erroneous fields
return "mng-dir-add";
}
watchedDirectory = fs.persistDirectory(watchedDirectory);
redirect.addFlashAttribute("added", watchedDirectory);
return "redirect:/list";
}
...一切都很好,花花公子。当数据有效时,它会被持久化并重定向到列表(POST/Redirect/GET)。当数据无效时,thymeleaf 的错误字段会被填充,我会在相应字段下方列出错误消息。
我唯一想改变但不知道怎么做的就是从模型中清除一些数据。
到目前为止我尝试过的事情:修改@ModelAttribute参数,在Model中设置属性,在RedirectAttributes中设置属性。每次我得到相同的数据用户提供的输出形式没有任何变化时,出于某种原因,我无法改变任何事情。我也尝试重定向到 GET 方法,但似乎它清除了我不想要的 slate clean。
如果有人感兴趣,这就是 thymeleaf 中的形式:
<form id="content" action="#" th:action="@{/add}" th:object="${watchedDirectory}" method="post" class="was-validated">
<div class="form-group has-feedback has-error">
<label for="filesystemPath">Filesystem path:</label>
<input th:field="*{filesystemPath}" type="text" id="filesystemPath" name="filesystemPath" class="form-control" placeholder="~/" required />
<label th:if="${#fields.hasErrors('filesystemPath')}" th:errors="*{filesystemPath}"></label>
</div>
<button type="submit" class="btn btn-outline-success">Save</button>
</form>
required 输入字段上的属性在提供空格时将关闭,但 Spring 验证会出现错误消息。清除此字段并将其返回给用户将使事情比显示混合信号更加一致,例如:
任何帮助将不胜感激。
【问题讨论】:
标签: spring spring-boot spring-mvc spring-data-jpa spring-data