前台页面-评论
1.点击新闻跳转到评论
// An highlighted block
@RequestMapping("/news/{id}")
public String news(@PathVariable Long id,Model model){//展现详情页
News news = newsService.findNewsById(id);
model.addAttribute("news",news);
return "news";
}
2.保存评论
// An highlighted block
//CommentController.java
@PostMapping("/comments")
public String save(Comment comment, HttpSession session){//保存评论
User user=(User)session.getAttribute("user");//得到用户登录记录
if (user == null) {//若是管理员
comment.setAdminComment(false);
}else{//若不是管理员
comment.setAdminComment(true);
}
commentService.save(comment);//添加评论
Long newsId=comment.getNews().getId();
return "redirect:/comments/"+newsId;
}
//CommentServiceImpl
@Service
public class CommentServiceImpl implements CommentService {
@Autowired
private CommentDao commentDao;
@Override
public void save(Comment comment) {
if(comment.getParentComment().getId()==-1){
comment.setParentComment(null);
}
commentDao.save(comment);
}
}
3.关于我页面
// An highlighted block
@RequestMapping("/about")
public String about(){//关于我
return "about";
}
4.回复评论
// An highlighted block
//CommentController.java
@RequestMapping("/comments/{newsId}")
public String comments(@PathVariable Long newsId, Model model){
List<Comment> comments=commentService.findCommentByNewsId(newsId);//根据新闻id查询评论
model.addAttribute("comments",comments);
return "news :: commentList";
}
//CommentDao
public interface CommentDao extends JpaRepository<Comment,Long> {
List<Comment> findByNewsIdAndParentCommentNull(Long newsId, Sort sort);
}
//CommentServiceImpl
@Override
public List<Comment> findCommentByNewsId(Long newsId) {
Sort sort=Sort.by("createTime");
List<Comment> comments=commentDao.findByNewsIdAndParentCommentNull(newsId,sort);
return null;
}
5.搜索
// An highlighted block
//IndexController.java
@RequestMapping("/search")
public String search(@PageableDefault(size =3,sort={"updateTime"},direction = Sort.Direction.DESC)Pageable pageable,
String query,Model model){//搜索
Pageable page=newsService.findNewsByQuery(query,pageable);
model.addAttribute("page",page);
model.addAttribute("query",query);
return "search";
}
//NewsServiceImpl.java
@Override
public Pageable findNewsByQuery(String query, Pageable pageable) {
return newsDao.findByquery("%"+query+"%",pageable);//模糊查询+
}
//NewsDao.java
@Query("select n from News n where n.title like ?1 or n.content like ?1")
Pageable findByquery(String s, Pageable pageable);
6.左下角最新新闻
// An highlighted block
//IndexController.java
@RequestMapping("/news/lastestNews")
public String lastestNews(Model model){
List<News> latestNewsList=newsService.findTop(3);
model.addAttribute("latestNewsList",latestNewsList);
return "_fragments :: lastestNewsList1";
}
//NewsDao
@Query("select n from News n")
List<News> findTop(Pageable pageable);
}
7.登录拦截过滤
// An highlighted block
//LoginInterceptor
public class LoginInterceptor extends HandlerInterceptorAdapter {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response,Object handler) throws Exception{
if(request.getSession().getAttribute("user")==null){
response.sendRedirect("/admin");
return false;
}
return true;
}
}
//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");
}
}
8.权限因为还未讲完下次再一起总结