中软国际实习学习第十一、十二天
第十一、十二天主要是对新闻的种类进行分类、对各条新闻的标签进行区分,对新闻进行删减,新增等等的操作
新闻分类、新增、删除
// 分页全查
@GetMapping("/news")
public String news(@PageableDefault(size = 3) Pageable pageable, Model model) {
// 不仅需要传递当前页的数据 还需要传递 所有的类别
model.addAttribute("types", typeService.listType());
model.addAttribute("page", newsService.listNews(pageable));
return "admin/news";
}
// 新增的前置操作
@GetMapping("/news/input")
public String toAdd(Model model) {
// 传递一个空的New对象 所有类别 所有标签
model.addAttribute("news", new News());
model.addAttribute("types", typeService.listType());
model.addAttribute("tags", tagService.listTag());
return "admin/news-input";
}
// 新增/编辑
@PostMapping("/news/add")
public String addOrUpdate(News news, HttpSession session, RedirectAttributes attributes) {
// 得到用户对象 将用户对象存储news对象中
news.setUser((User) session.getAttribute("user"));
// 得到Type对象 将Type对象存储到News对象中 原因是前台穿的Type对象 只有id值
news.setType(typeService.getTypeById(news.getType().getId()));
// 得到Tags对象 TagIds 前台页面封装过的 id字符串
news.setTags(tagService.listTag(news.getTagIds()));
// System.out.println(news.getTagIds());
News n;
// 判断 当前是 保存还是 更新
if (news.getId() == null) {
n = newsService.save(news);
} else {
n = newsService.updateNews(news.getId(), news);
}
if (n == null) {
attributes.addFlashAttribute("message", "操作失败");
} else {
attributes.addFlashAttribute("message", "操作成功");
}
return "redirect:/admin/news";
}
//删除
@GetMapping("/news/{id}/delete")
public String delete(@PathVariable Long id, RedirectAttributes attributes) {
newsService.deleteById(id);
attributes.addFlashAttribute("message", "删除成功");
return "redirect:/admin/news";
}
新闻标签分类
public class TagController {
@Autowired
TagService tagService;
@GetMapping("/tags")
public String tags(@PageableDefault(size = 5, sort = "id",direction = Sort.Direction.DESC) Pageable pageable, Model model) {
model.addAttribute("page", tagService.listTag(pageable));
return "admin/tags";
}
// 新增 跳转到新增页面
@GetMapping("/tags/input")
public String toAdd(Model model) {
model.addAttribute("tag", new Tag());
return "admin/tags-input";
}
@PostMapping("/tags/add")
public String add(Tag tag, BindingResult result, RedirectAttributes attributes) {
Tag t1 = tagService.getTagByName(tag.getName());
if (t1 != null) {
// 当名称重复时 像tag对象中的name属性存放一条 nameError : 不能添加重复的标签名
result.rejectValue("name", "nameError", "不能添加重复的标签名");
return "admin/tags-input";
}
// 判断是否保存成功
Tag t2 = tagService.save(tag);
if (t2 == null) {
attributes.addFlashAttribute("message", "新增失败");
} else {
attributes.addFlashAttribute("message", "新增成功");
}
return "redirect:/admin/tags";
}
新闻编辑删除
//删除
@GetMapping("/news/{id}/delete")
public String delete(@PathVariable Long id, RedirectAttributes attributes) {
newsService.deleteById(id);
attributes.addFlashAttribute("message", "删除成功");
return "redirect:/admin/news";
}
// 编辑
@GetMapping("news/{id}/toUpdate")
public String toUpdate(@PathVariable Long id, Model model) {
News news = newsService.getNewsById(id);
news.init(); // tag的list集合 转换成了 对应的字符串
model.addAttribute("news", news);
model.addAttribute("types", typeService.listType());
model.addAttribute("tags", tagService.listTag());
return "admin/news-input";
}
新闻新增删除在标签当中的操作
// 删除
@GetMapping("/tags/{id}/delete")
public String delete(@PathVariable long id, RedirectAttributes attributes) {
tagService.deleteById(id);
attributes.addFlashAttribute("message", "删除成功");
return "redirect:/admin/tags";
}
//==============
// 编辑
@GetMapping("/tags/{id}/toUpdate")
public String toUpdate(@PathVariable long id, Model model) {
Tag t1 = tagService.getTagById(id);
model.addAttribute("tag", t1);
return "admin/tags-input";
}
@PostMapping("/tags/update/{id}")
public String update(Tag tag, BindingResult result, @PathVariable long id, RedirectAttributes attributes) {
Tag tag1 = tagService.getTagByName(tag.getName());
if (tag1 != null) {
result.rejectValue("name", "error", "该标签已存在");
return "admin/tags-input";
}
Tag tag2 = tagService.save(tag);
if (tag2 == null) {
attributes.addFlashAttribute("message", "更新失败");
} else {
attributes.addFlashAttribute("message", "更新成功");
}
return "redirect:/admin/tags";
}