Java 如果发生错误,Thymeleaf Spring th:block和th:each不会再次运行
我正在验证定义角色名称的字段。这是一种通过邮寄的形式。关键是当发生错误并再次调用时:Java 如果发生错误,Thymeleaf Spring th:block和th:each不会再次运行,java,spring,thymeleaf,Java,Spring,Thymeleaf,我正在验证定义角色名称的字段。这是一种通过邮寄的形式。关键是当发生错误并再次调用时: modelAndView.setViewName ("admin/role"); return modelAndView; 它不会在th:block中重新生成列表。但当这个过程正确完成时,它会显示我的正常页面 成功流程: @RequestMapping(value = "/admin/role", method = RequestMethod.GET) public ModelAndView role(Mod
modelAndView.setViewName ("admin/role");
return modelAndView;
它不会在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;
}
@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;
}
如果发生错误:
@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;
}
@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;
}
获取方法:
@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;
}
@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;
}
@RequestMapping(value=“/admin/role”,method=RequestMethod.GET)
公共模型和视图角色(模型){
ModelAndView ModelAndView=新建ModelAndView();
Authentication auth=SecurityContextHolder.getContext().getAuthentication();
User=userService.findUserByEmail(auth.getName());
List pages=pageService.getAllPages();
角色=新角色();
添加对象(“角色”,角色);
添加对象(“页面”,页面);
modelAndView.addObject(“用户名”,user.getName());
modelAndView.setViewName(“管理员/角色”);
返回模型和视图;
}
发布方法:
@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;
}
@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;
}
@RequestMapping(value=“/admin/role”,method=RequestMethod.POST)
public ModelAndView createNewRole(@Valid Role Role、BindingResult、BindingResult、,
@RequestParam(value=“pgids”,required=false)整数[]pgids
){
ModelAndView ModelAndView=新建ModelAndView();
Authentication auth=SecurityContextHolder.getContext().getAuthentication();
User=userService.findUserByEmail(auth.getName());
modelAndView.addObject(“用户名”,user.getName());
Role roleExist=roleService.findByAuthority(Role.getAuthority());
if(roleExist!=null){
绑定结果
.rejectValue(“authority”、“error.app_role”、“已使用提供的名称注册了一个角色”);
}
if(bindingResult.hasErrors()){
modelAndView.setViewName(“管理员/角色”);
}否则{
如果(pgids!=null){
对于(int i=0;i
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()后没有添加变量页面。因此,“如果”如下所示:
if (bindingResult.hasErrors()){
List<Page> pages = pageService.getAllPages();
modelAndView.addObject("pages", pages);
modelAndView.setViewName("admin/role");
}
if(bindingResult.hasErrors()){
List pages=pageService.getAllPages();
添加对象(“页面”,页面);
modelAndView.setViewName(“管理员/角色”);
}
当您提交包含无效数据的表单时
bindingResult.hasErrors()
将返回true,并且由于if/else子句的原因,您没有输入添加页面的代码部分
所以变量
页数
不包含任何数据,并且您的th:each
循环将不起任何作用
如果您想打印页面,即使输入了无效数据,您也可以在
bindingResult.hasErrors()
为真时删除else,您没有设置模型对象pages
,那么为什么您会对${pages}感到惊讶呢
为空且th:each
无需迭代?无关:你为什么使用
而不是将th:each
放在
本身上?@Andreas这就是我寻求帮助的原因,这是一个我没有考虑的细节。对你来说,这太明显了,我感谢你的帮助,但也不是要贬低我的问题,或者是吗?你说得很对,我完全忘记了那部分。非常感谢。