一、新闻页面的书写
因为此次新闻页面的与之前分类功能的书写差距不大,主要是代码上有一些小细节复杂一些,所以本次便不再赘述,直接上代码
1.NewsController
// An highlighted block
@Controller
@RequestMapping("/admin/news")
public class NewsController {
@Autowired
private NewsService newsService;
@Autowired//引入
private TypeService typeService;
@Autowired//引入
private TagService tagService;
@RequestMapping//查询功能,与之前类似
public String list(@PageableDefault(size=5,sort={
"updateTime"},direction= Sort.Direction.DESC) Pageable pageable,
Model model){
//pageable用来存放查询条件
Page<News> page=newsService.findByPageable(pageable);//根据页码查询
model.addAttribute("page",page);//将查询出的结果存放在model,通过model传给前段时
model.addAttribute("types",typeService.listType());
return "admin/news";//跳转到news界面
}
@RequestMapping("input/{id}")//新增与编辑
public String toInput(@PathVariable Long id,Model model){
if(id==-1){
//新增
model.addAttribute("news",new News());//新增,因为没有数据,所以需要new创建一个
}else{
//更新
News news= newsService.findNewsById(id);//根据id查出新闻
String tagIds=tagService.getTagIds(news.getTags());//得到TagId
news.setTagIds(tagIds);//传TagId
model.addAttribute("news",news);
//将查出的新闻放进model,传递给前端
}
List<Type> types=typeService.listType();//通过typeService将所有的分类数据展示出来
model.addAttribute("types",types);//将查询出的数据放进model
model.addAttribute("tags",tagService.listTag());
List<Tag> tags=tagService.listTag();
return "admin/news-input";//跳转到新增页面
}
@RequestMapping