Spring 检查数据库中是否存在团队或输入字段是否为空
有必要检查Team的输入字段是否为空,以及存储库中是否有这样的团队 如果字段不是空的,并且没有这样的团队,那么您可以创建一个新团队。如果字段为空或团队已存在,则给出一个错误 管理员控制器Spring 检查数据库中是否存在团队或输入字段是否为空,spring,rest,thymeleaf,Spring,Rest,Thymeleaf,有必要检查Team的输入字段是否为空,以及存储库中是否有这样的团队 如果字段不是空的,并且没有这样的团队,那么您可以创建一个新团队。如果字段为空或团队已存在,则给出一个错误 管理员控制器 @Controller public class AdminController { @RequestMapping(value = "/admin/team", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALU
@Controller
public class AdminController {
@RequestMapping(value = "/admin/team", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
public String addTeam(Model model, @ModelAttribute("teamForm") @Validated TeamForm teamForm,
BindingResult result, final RedirectAttributes redirectAttributes) {
System.out.println("addTeam invoked");
if (result.hasErrors()) {
return "/admin";
}
Team newTeam = new Team();
newTeam.setName(teamForm.getName());
newTeam.setUrl(teamForm.getUrl());
teamRepository.save(newTeam);
return "teamList";
}
@RequestMapping(value = "/admin", method = RequestMethod.GET)
public String adminPage(Model model) {
model.addAttribute("teamForm",new TeamForm());
model.addAttribute("eventForm",new EventForm());
model.addAttribute("usersForm",new UsersForm());
return "admin";
}
admin.html
<form th:action="@{/admin/team}"
th:object="${teamForm}" method="POST">
Team name:
<input type="text" th:field="*{name}" />
<p th:if="${#fields.hasErrors('name')}" th:errors="*{name}">Incorrect Name</p>
<br/>
Url :
<input type="text" th:field="*{url}" />
<br/>
<input type="submit" value="Create Team" />
</form>
团队名称:
名称不正确
网址: