【springboot thymeleaf】实体上的非空校验@NotBlank(message = “提示信息”) 在前端页面展示的方式
举例
实体类
@Setter
@Getter
@ToString
@NoArgsConstructor
@Entity
@Table(name = "t_type")
public class Type {
@Id
@GeneratedValue
private Long id;
@NotBlank(message = "分类名称不能为空")
private String name;
}
html页面
<form action="admin/types" th:action="@{/admin/types}" th:object="${type}" method="post">
<input type="hidden" name="id">
<div>
<div>
<label>名称</label>
<input type="text" name="name" th:value="*{name}" placeholder="名称">
</div>
</div>
<div>
</div>
<div>
<button type="button" onclick="window.history.go(-1)" >返回</button>
<button type="submit">提交</button>
</div>
</form>
Controller类
@GetMapping("/types/to_add_type_page")
public String toAddTypePage(Model model){
model.addAttribute("type",new Type());
return "admin/types-input";
}
@Controller
@RequestMapping("/admin")
public class TypeController {
@Autowired
private TypeService typeService;
@PostMapping("/types")
public String saveType(@Valid Type type, BindingResult result, RedirectAttributes attributes){
if (result.hasErrors()){
return "admin/types-input";
}
Type type1 = typeService.saveType(type);
return "redirect:/admin/types";
}
}