中软国际实训日记第十一天-7.31

目录

1.归档

2.搜索

3.评论

4.底部优化

5.拦截器

正文

1.归档

1.1控制层

@RequestMapping("archives")
    public String archives(Model model){
        HashMap<String, List<News>> map=newsService.archiveNews();
        Long count=newsService.countNews();
        model.addAttribute("newsCount",count);
        model.addAttribute("archiveMap",map);
    return "archives";
    }

1.2实现层

@Override
      public Long countNews() {
            return newsDao.count();
      }

      @Override
      public HashMap<String, List<News>> archiveNews() {
           LinkedHashMap<String,List<News>> map=new LinkedHashMap<>();
           List<String> years=newsDao.findGroupYear();
           for(String y:years){
                 List<News>news=newsDao.findByYear(y);
                 map.put(y,news);
           }
           return map;
      }

1.3界面
在这里插入图片描述

2.搜索

2.1控制层

 @RequestMapping("/search")
    public String search(@PageableDefault(size=3,sort={"updateTime"},direction = Sort.Direction.DESC)
                         Pageable pageable,
                         String query,
                         Model model){
        Page<News> page=newsService.findNewsByQuery(query,pageable);
        model.addAttribute("page",page);
        model.addAttribute("query",query);
        return "search";
    }

2.2实现层

  @Override
      public Page<News> findNewsByQuery(String query, Pageable pageable) {
            return newsDao.findByquery(query,pageable);

      }

2.3界面
在这里插入图片描述

3.评论

3.1控制层

@PostMapping("/comments")
    public String save(Comment comment, HttpSession session){
        User user=(User)session.getAttribute("name");
        if(user==null){
            comment.setAdminComment(false);
        }else{
            comment.setAdminComment(true);
        }
        commentService.save(comment);
        Long newsId=comment.getNews().getId();
        return "redirect:/comments/"+newsId;
    }
    @RequestMapping("/comments/{newsId}")
    public String comments(@PathVariable Long newsId, Model model){
        List<Comment> comments=commentService.findCommentByNewsId(newsId);
        model.addAttribute("comments",comments);
        return "news :: commentList";
    }

3.2实现层

@Override
    public void save(Comment comment) {
        if(comment.getParentComment().getId()==-1){
            comment.setParentComment(null);
        }
        commentDao.save(comment);
    }

    @Override
    public List<Comment> findCommentByNewsId(Long newsId) {
        Sort sort=Sort.by("createTime");
        List<Comment> comments=commentDao.findByNewsIdAndParentCommentNull(newsId,sort);
        return comments;
    }

3.3界面

4.底部优化

4.1控制层

 @RequestMapping("/footer/lastestNews")
    public String lastestNews(Model model){
        List<News> lastestNewsList=newsService.findTop(3);
        model.addAttribute("lastestNewsList", lastestNewsList);
        return "_fragments::lastestNewsList1";
    }

4.2实现层

 @Override
      public List<News> findTop(int i) {
            Sort sort=Sort.by(Sort.Direction.DESC,"updateTime");
            Pageable pageable= PageRequest.of(0,i,sort);
            return newsDao.findTop(pageable);
      }

4.3界面
在这里插入图片描述

5.拦截器

5.在登录时需要先登录,不能直接进入界面

WebConfig.java

@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {
    @Override
    public void addInterceptors(InterceptorRegistry registry){
        registry.addInterceptor(new LoginInterceptor())
                .addPathPatterns("/admin/**")
                .excludePathPatterns("/admin")
                .excludePathPatterns("/admin/login");
    }

LoginInterceptor.java

public class LoginInterceptor extends HandlerInterceptorAdapter {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response,Object handle)throws Exception{
        if(request.getSession().getAttribute("user")==null){
           response.sendRedirect("/admin");
           return false;
        }
        return true;
    }
©️2020 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页