java-SpringBoot的学习心得(3)——分类管理与标签
程序员文章站
2024-03-04 12:08:35
...
分类管理
上次已实现分页功能,这次新增编辑,删除,增加操作
- 编辑:通过id来更改分类内容
@RequestMapping("/types/{id}/toUpdate")
public String toUpdate(@PathVariable Long id,Model model){
model.addAttribute("type",typeService.getType(id));
return "admin/types-input";
}
@RequestMapping("/types/update/{id}")
public String update(@Valid Type type,@PathVariable Long id,BindingResult result,RedirectAttributes attributes){
System.out.println("type"+type);
Type type1 = typeService.getTypeByName(type.getName());
if(type1!=null){
result.rejectValue("name","nameError","不能添加重复的分类");
}
if(result.hasErrors()){
return "admin/types-input";
}
Type type2 = typeService.updateType(id, type);
if(type2==null){
attributes.addFlashAttribute("message","更新失败");
}else{
attributes.addFlashAttribute("message","更新成功");
}
return "redirect:/admin/types";
}
- 删除:通过id来删除
@RequestMapping("/types/{id}/delete")
public String delete(@PathVariable Long id,RedirectAttributes attributes){
typeService.delete(id);
attributes.addFlashAttribute("message","删除成功");
return "redirect:/admin/types";
}
- 添加:直接添加内容,id自动增加
@GetMapping("/types/input")
public String input(Model model){
model.addAttribute("type",new Type());
return "admin/types-input";
}
@PostMapping("/types/add")
public String add(@Valid Type type, BindingResult result, RedirectAttributes attributes){
Type type1 = typeService.getTypeByName(type.getName());
if(type1!=null){
result.rejectValue("name","nameError","不能添加重复的分类");
}
if(result.hasErrors()){
return "admin/types-input";
}
Type type2 = typeService.saveType(type);
if(type2==null){
attributes.addFlashAttribute("message","新增失败");
}else{
attributes.addFlashAttribute("message","新增成功");
}
return "redirect:/admin/types";
}
效果演示
- 编辑: 输入同样内容会提示,编辑失败
- 删除:点击删除小雨记1就被删除了
- 添加:用的界面跟编辑一样
标签
标签的实现与分类类似,都是同样的操作
@GetMapping("/tags/input")
public String input(Model model){
model.addAttribute("tag",new Tag());
return "admin/tags-input";
}
@PostMapping("/tags/add")
public String add(@Valid Tag tag, BindingResult result, RedirectAttributes attributes){
Tag tag1 =tagService.getTagByName(tag.getName());
if(tag1!=null){
result.rejectValue("name","nameError","不能添加重复的标签");
System.out.println(result);
}
if(result.hasErrors()){
return "admin/tags-input";
}
Tag tag2 = tagService.saveTag(tag);
if(tag2==null){
attributes.addFlashAttribute("message","更新失败");
}else{
attributes.addFlashAttribute("message","更新成功");
}
return "redirect:/admin/tags";
}
@RequestMapping("/tags/{id}/toUpdate")
public String toUpdate(@PathVariable Long id,Model model){
model.addAttribute("tag",tagService.getTag(id));
return "admin/tags-input";
}
@RequestMapping("/tags/update/{id}")
public String update(@Valid Tag tag, @PathVariable Long id, BindingResult result, RedirectAttributes attributes){
System.out.println("type"+tag);
Tag tag1 = tagService.getTagByName(tag.getName());
if(tag1!=null){
result.rejectValue("name","nameError","不能添加重复的分类");
}
if(result.hasErrors()){
return "admin/types-input";
}
Tag tag2 = tagService.updateTag(id, tag);
if(tag2==null){
attributes.addFlashAttribute("message","更新失败");
}else{
attributes.addFlashAttribute("message","更新成功");
}
return "redirect:/admin/tags";
}
@RequestMapping("/tags/{id}/delete")
public String delete(@PathVariable Long id,RedirectAttributes attributes){
tagService.deleteTag(id);
attributes.addFlashAttribute("message","删除成功");
return "redirect:/admin/tags";
}