1.修改Type实体类;
Type中添加:
@NotBlank(message = "分类名称不能为空") //新增
private String name;
2.修改TypeRepository接口;
TypeRepository中添加:
Type findByName (String name);
3.修改TypeService接口;
TypeServicer中添加:
Type saveType(Type type);
Type getTypeByName(String name);
4.修改TypeServiceImpl类;
TypeServiceImpl中添加:
@Override
public Type saveType(Type type) {
return typeRepository.save(type);
}
@Override
public Type getTypeByName(String name) {
return typeRepository.findByName(name);
}
5.修改TypeControllerr类;
TypeController中添加:
@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";
}
6.修改types-input.html文件;
types-input.html中添加:
<!-- <div class="ui error message"></div>-->
<div class="ui negative message" th:if="${#fields.hasErrors('name')}">
<i type="button" class="close icon" onclick="window.history.go(-1)" ></i>
<p th:errors="*{name}">提交信息不符合规则</p>
</div>
Test
1.登陆成功后进入分类界面;
2.点击新增,进入类别新增界面;
测试新增空类别;
1.修改TypeService接口;
TypeServicer中添加:
void delete(Long id);
2.修改TypeServiceImpl类;
TypeServiceImpl中添加:
@Override
public void delete(Long id) {
typeRepository.deleteById(id);
}
3.修改TypeControllerr类;
TypeController中添加:
public String delete(@PathVariable Long id,RedirectAttributes attributes){
typeService.delete(id);
attributes.addFlashAttribute("message","删除成功");
return "redirect:/admin/types";
}
1.修改TypeService接口;
TypeServicer中添加:
Type getType(Long id);
Type updateType(Long id,Type type);
2.修改TypeServiceImpl类;
TypeServiceImpl中添加:
@Override
@Override
public Type getType(Long id) {
return typeRepository.findById(id).orElse(null);
}
@Override
public Type updateType(Long id, Type type) {
Type type1 = typeRepository.findById(id).orElse(null);
if(type1==null){
System.out.println("未获得更新对象");
return null;
}
BeanUtils.copyProperties(type,type1);
return typeRepository.save(type1);
}
}
3.修改TypeControllerr类;
TypeController中添加:
@RequestMapping("/types/{id}/toUpdate")
public String toUpdate(@PathVariable Long id,Model model){
System.out.println("id:"+id);
model.addAttribute("type",typeService.getType(id));
System.out.println("根据id查得数据为:"+typeService.getType(id));
return "admin/types-input";
}
@RequestMapping("/types/update/{id}")
public String update(@Valid Type type,BindingResult result,
@PathVariable Long id,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);
System.out.println("type2"+type2);
if (type2!=null){
attributes.addFlashAttribute("message","更新成功");
}else{
attributes.addFlashAttribute("message","更新失败");
}
return "redirect:/admin/types";
}
Test
点击编辑按钮,进入类别更新界面;
输入符合的类别名称,点击提交进行更新,更新成功;
package com.roger0123.news.po;
import javax.persistence.*;
@Entity
@Table(name="t_tag")
public class Tag {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@NotBlank(message = "标签名称不能为空")
private String name;
public Tag(){
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Tag{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
2.dao下创建TagRepository接口;
TagRepository:
package com.roger0123.news.dao;
import com.roger0123.news.po.Tag;
import org.springframework.data.jpa.repository.JpaRepository;
public interface TagRepository extends JpaRepository {
Tag findByName(String name);
}
3.service下创建TagService接口;
TagService:
package com.roger0123.news.service;
import com.roger0123.news.po.Tag;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
public interface TagService {
Page<Tag> listTag(Pageable pageable);
Tag saveTag(Tag tag);
Tag getTagByName(String name);
void deleteTag(Long id);
Tag getTag(Long id);
Tag updateTag(Long id,Tag tag);
}
4.service.impl下创建TagServiceImpl类;
TagServiceImpl类:
package com.roger0123.news.service.impl;
import com.roger0123.news.dao.TagRepository;
import com.roger0123.news.po.Tag;
import com.roger0123.news.service.TagService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
@Service
public class TagServiceImpl implements TagService {
@Autowired
private TagRepository tagRepository;
@Override
public Page<Tag> listTag(Pageable pageable) {
return tagRepository.findAll(pageable);
}
@Override
public Tag saveTag(Tag tag) {
return tagRepository.save(tag);
}
@Override
public Tag getTagByName(String name) {
return tagRepository.findByName(name);
}
@Override
public void deleteTag(Long id) {
tagRepository.deleteById(id);
}
@Override
public Tag getTag(Long id) {
return tagRepository.findById(id).orElse(null);
}
@Override
public Tag updateTag(Long id, Tag tag) {
Tag tag1 = tagRepository.findById(id).orElse(null);
if(tag1==null){
System.out.println("未获得更新对象");
return null;
}
BeanUtils.copyProperties(tag,tag1);
return tagRepository.save(tag1);
}
}
5.web.admin下创建TagController类;
TagController:
package com.roger0123.news.web.admin;
import com.roger0123.news.po.Tag;
import com.roger0123.news.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import javax.validation.Valid;
@Controller
@RequestMapping("/admin")
public class TagController {
@Autowired
private TagService tagService;
@RequestMapping("/tags")
public String tags(@PageableDefault(size = 3,sort = {
"id"},direction = Sort.Direction.DESC)
Pageable pageable, Model model){
model.addAttribute("page",tagService.listTag(pageable));
return "admin/tags";
}
@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}/delete")
public String deleteTag(@PathVariable Long id,RedirectAttributes attributes){
tagService.deleteTag(id);
attributes.addFlashAttribute("message","删除成功");
return "redirect:/admin/tags";
}
@RequestMapping("/tags/{id}/toUpdate")
public String toUpdate(@PathVariable Long id,Model model){
System.out.println("id:"+id);
model.addAttribute("tag",tagService.getTag(id));
System.out.println("根据id查得数据为:"+tagService.getTag(id));
return "admin/tags-input";
}
@RequestMapping("/tags/update/{id}")
public String update(@Valid Tag tag, BindingResult result,
@PathVariable Long id, RedirectAttributes attributes){
System.out.println("传入tag:"+tag);
Tag tag1 =tagService.getTagByName(tag.getName());
if (tag1!=null){
result.rejectValue("name","nameError","不能添加重复的分类");
}
if (result.hasErrors()){
return "admin/tags-input";
}
Tag tag2 = tagService.updateTag(id,tag);
System.out.println("tag2"+tag2);
if (tag2!=null){
attributes.addFlashAttribute("message","更新成功");
}else{
attributes.addFlashAttribute("message","更新失败");
}
return "redirect:/admin/tags";
}
}
tags-input.html中添加:
<!-- <div class="ui error message"></div>-->
<div class="ui negative message" th:if="${#fields.hasErrors('name')}">
<i type="button" class="close icon" onclick="window.history.go(-1)" ></i>
<p th:errors="*{name}">提交信息不符合规则</p>
</div>