我正在验证定义角色名称的字段。这是一种通过邮寄发送的形式。关键是当发生错误并再次调用时:
modelAndView.setViewName ("admin/role");
return modelAndView;
它不会在<td>
内重新生成th: block
列表。但是当这个过程正确完成时,它会显示我的正常页面。
获取方法:
@RequestMapping(value = "/admin/role", method = RequestMethod.GET)
public ModelAndView role(Model model){
ModelAndView modelAndView = new ModelAndView();
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
User user = userService.findUserByEmail(auth.getName());
List<Page> pages = pageService.getAllPages();
Role role = new Role();
modelAndView.addObject("role", role);
modelAndView.addObject("pages", pages);
modelAndView.addObject("userName", user.getName());
modelAndView.setViewName("admin/role");
return modelAndView;
}
POST方式:
@RequestMapping(value = "/admin/role", method = RequestMethod.POST)
public ModelAndView createNewRole(@Valid Role role,BindingResult bindingResult,
@RequestParam(value = "pgids" , required = false) Integer [] pgids
){
ModelAndView modelAndView = new ModelAndView();
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
User user = userService.findUserByEmail(auth.getName());
modelAndView.addObject("userName", user.getName());
Role roleExist = roleService.findByAuthority(role.getAuthority());
if (roleExist != null){
bindingResult
.rejectValue("authority","error.app_role", "There is already a role registered with the name provided");
}
if (bindingResult.hasErrors()){
modelAndView.setViewName("admin/role");
}else{
if(pgids != null) {
for (int i = 0; i < pgids.length; i++) {
System.out.println(pgids[i]);
}
}
roleService.saveRole(role, pgids);
List<Page> pages = pageService.getAllPages();
modelAndView.addObject("successMessage", "Role has been created successfully");
modelAndView.addObject("role", new Role());
modelAndView.addObject("pages", pages);
modelAndView.setViewName("admin/role");
}
return modelAndView;
}
HTML表单
<form autocomplete="off" action="#" th:action="@{/admin/role}" method="post" role="form">
<div class="row">
<div class="col-md-12 bottommargin-sm">
<label>Name of the Role</label>
<label class="errormsg" th:if="${#fields.hasErrors('role.authority')}" th:errors="${role.authority}"></label>
<input type="text" th:field="${role.authority}" class="sm-form-control"
placeholder="Example Name"/>
</div>
<div class="col-md-12">
<label>Catalogue of Pages</label>
<table class="table table-hover">
<thead>
<tr>
<th class="col-sm-2 center">Asign</th>
<th class="col-sm-4 center">Name</th>
<th class="col-sm-6 center">URL</th>
</tr>
</thead>
<tbody>
<th:block th:each="page : ${pages}">
<tr>
<td class="col-sm-2 center">
<input type="checkbox" name="pgids" th:value="${page.id}"/>
</td>
<td class="col-sm-4 center" th:utext="${page.name}"></td>
<td class="col-sm-6 center" th:utext="${page.url}"></td>
</tr>
</th:block>
</tbody>
</table>
<button type="submit" class="button nomargin rightmargin-sm button-reef-blue">Create
</button>
</div>
<div class="col-sm-12">
<label th:utext="${successMessage}" class="successmsg"></label>
</div>
</div>
更新
如底部所述,一旦进入bindingResult.hasErrors(),我就错误地不添加变量页面。所以,如果&#39;如下:
if (bindingResult.hasErrors()){
List<Page> pages = pageService.getAllPages();
modelAndView.addObject("pages", pages);
modelAndView.setViewName("admin/role");
}
答案 0 :(得分:1)
当您提交包含无效数据的表单时
bindingResult.hasErrors()
将返回true,并且由于if / else子句,您不会输入添加页面的代码部分。
所以变量
页
不包含任何数据,th:each
循环将不执行任何操作。
如果您想要在输入无效数据时打印页面,您只需删除else
。