👽 将IndexController的各个部分提取成单独的Controller,方便后期维护。

👽 将IndexController的各个部分提取成单独的Controller,方便后期维护。
pull/1/head
RYAN0UP_ 2018-04-26 15:24:20 +08:00
parent d7d0d991db
commit d0d7e90a16
19 changed files with 955 additions and 667 deletions

View File

@ -6,7 +6,7 @@ import cc.ryanc.halo.model.dto.Theme;
import cc.ryanc.halo.service.AttachmentService;
import cc.ryanc.halo.service.OptionsService;
import cc.ryanc.halo.util.HaloUtil;
import cc.ryanc.halo.web.controller.BaseController;
import cc.ryanc.halo.web.controller.core.BaseController;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;

View File

@ -2,6 +2,7 @@ package cc.ryanc.halo.repository;
import cc.ryanc.halo.model.domain.Category;
import cc.ryanc.halo.model.domain.Post;
import cc.ryanc.halo.model.domain.Tag;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
@ -135,4 +136,13 @@ public interface PostRepository extends JpaRepository<Post,Long>{
Page<Post> findPostByYearAndMonth(@Param("year") String year,@Param("month") String month,Pageable pageable);
List<Post> findPostByCategories(Category category);
/**
*
*
* @param tag tag
* @param pageable pageable
* @return page
*/
Page<Post> findPostsByTags(Tag tag,Pageable pageable);
}

View File

@ -1,6 +1,7 @@
package cc.ryanc.halo.service;
import cc.ryanc.halo.model.domain.Post;
import cc.ryanc.halo.model.domain.Tag;
import cc.ryanc.halo.model.dto.Archive;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
@ -179,6 +180,15 @@ public interface PostService {
*/
List<Post> findPostByYear(String year);
/**
*
*
* @param tag tag
* @param pageable pageable
* @return page
*/
Page<Post> findPostsByTags(Tag tag,Pageable pageable);
/**
* rss
*

View File

@ -1,6 +1,7 @@
package cc.ryanc.halo.service.impl;
import cc.ryanc.halo.model.domain.Post;
import cc.ryanc.halo.model.domain.Tag;
import cc.ryanc.halo.model.dto.Archive;
import cc.ryanc.halo.repository.PostRepository;
import cc.ryanc.halo.service.PostService;
@ -280,6 +281,18 @@ public class PostServiceImpl implements PostService {
return postRepository.findPostByYearAndMonth(year,month,pageable);
}
/**
*
*
* @param tag tag
* @param pageable pageable
* @return page
*/
@Override
public Page<Post> findPostsByTags(Tag tag, Pageable pageable) {
return postRepository.findPostsByTags(tag,pageable);
}
/**
* rss
*

View File

@ -1,533 +0,0 @@
package cc.ryanc.halo.web.controller;
import cc.ryanc.halo.model.domain.*;
import cc.ryanc.halo.model.dto.Archive;
import cc.ryanc.halo.model.dto.HaloConst;
import cc.ryanc.halo.service.*;
import cc.ryanc.halo.util.HaloUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.websocket.server.PathParam;
import java.util.*;
/**
* @author : RYAN0UP
* @date : 2017/11/23
* @version : 1.0
* description :
*/
@Slf4j
@Controller
@RequestMapping(value = {"/","index"})
public class IndexController extends BaseController{
@Autowired
private PostService postService;
@Autowired
private LinkService linkService;
@Autowired
private CategoryService categoryService;
@Autowired
private TagService tagService;
@Autowired
private CommentService commentService;
@Autowired
private MenuService menuService;
@Autowired
private UserService userService;
@Autowired
private GalleryService galleryService;
@Autowired
private MailService mailService;
/**
*
*
* @param model model
* @return
*/
@GetMapping
public String index(Model model){
//调用方法渲染首页
return this.index(model,1);
}
/**
*
*
* @param model model
* @param page
* @param size
* @return /themes/{theme}/index
*/
@GetMapping(value = "page/{page}")
public String index(Model model,
@PathVariable(value = "page") Integer page){
Sort sort = new Sort(Sort.Direction.DESC,"postDate");
//默认显示10条
Integer size = 10;
//尝试加载设置选项,用于设置显示条数
if(!StringUtils.isBlank(HaloConst.OPTIONS.get("index_posts"))){
size = Integer.parseInt(HaloConst.OPTIONS.get("index_posts"));
}
//所有文章数据,分页
Pageable pageable = new PageRequest(page-1,size,sort);
Page<Post> posts = postService.findPostByStatus(0,pageable);
model.addAttribute("posts",posts);
//文章总数
model.addAttribute("postsCount",postService.findAllPosts().size());
model.addAttribute("is_home",true);
//用户信息
User user = userService.findUser();
model.addAttribute("user",user);
//所有分类目录
List<Category> categories = categoryService.findAllCategories();
model.addAttribute("categories",categories);
//菜单列表
List<Menu> menus = menuService.findAllMenus();
model.addAttribute("menus",menus);
//归档数据,包含[year,month,count,List<Post>]
List<Archive> archives = postService.findPostGroupByYearAndMonth();
model.addAttribute("archives",archives);
//设置选项
model.addAttribute("options",HaloConst.OPTIONS);
return this.render("index");
}
/**
* ajax
*
* @param page page
* @return List<Post></>
*/
@GetMapping(value = "next")
@ResponseBody
public List<Post> ajaxIndex(@PathParam(value = "page") Integer page){
Sort sort = new Sort(Sort.Direction.DESC,"postDate");
//默认显示10条
Integer size = 10;
//尝试加载设置选项,用于设置显示条数
if(!StringUtils.isBlank(HaloConst.OPTIONS.get("index_posts"))){
size = Integer.parseInt(HaloConst.OPTIONS.get("index_posts"));
}
//文章数据,只获取文章,没有分页
Pageable pageable = new PageRequest(page-1,size,sort);
List<Post> posts = postService.findPostByStatus(0,pageable).getContent();
return posts;
}
/**
*
*
* @param postUrl
* @param model model
* @return /themes/{theme}/post
*/
@GetMapping(value = "archives/{postUrl}")
public String getPost(@PathVariable String postUrl, Model model){
Post post = postService.findByPostUrl(postUrl);
//获得当前文章的发布日期
Date postDate = post.getPostDate();
try {
//查询当前文章日期之前的所有文章
List<Post> beforePosts = postService.findByPostDateBefore(postDate);
//查询当前文章日期之后的所有文章
List<Post> afterPosts = postService.findByPostDateAfter(postDate);
if(null!=beforePosts&&beforePosts.size()>0){
model.addAttribute("beforePost",beforePosts.get(beforePosts.size()-1));
}
if(null!=afterPosts&&afterPosts.size()>0){
model.addAttribute("afterPost",afterPosts.get(afterPosts.size()-1));
}
}catch (Exception e){
log.error("未知错误:{0}",e.getMessage());
}
model.addAttribute("post",post);
//文章总数
model.addAttribute("postsCount",postService.findAllPosts().size());
//用户信息
User user = userService.findUser();
model.addAttribute("user",user);
//所有分类目录
List<Category> categories = categoryService.findAllCategories();
model.addAttribute("categories",categories);
//归档数据,包含[year,month,count,List<Post>]
List<Archive> archives = postService.findPostGroupByYearAndMonth();
//菜单列表
List<Menu> menus = menuService.findAllMenus();
model.addAttribute("menus",menus);
model.addAttribute("archives",archives);
//设置选项
model.addAttribute("options",HaloConst.OPTIONS);
return this.render("post");
}
/**
*
*
* @param postId postId
* @return List<Comment></>
*/
@GetMapping(value = "/getComment/{postId}",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@ResponseBody
public List<Comment> getComment(@PathVariable Long postId){
Optional<Post> post = postService.findByPostId(postId);
Sort sort = new Sort(Sort.Direction.DESC,"commentDate");
Pageable pageable = new PageRequest(0,10,sort);
List<Comment> comments = commentService.findCommentsByPostAndCommentStatus(post.get(),pageable,2).getContent();
if(null==comments){
return null;
}
return comments;
}
/**
*
*
* @param model model
* @return /themes/{theme}/about
*/
@GetMapping(value = "/about")
public String about(Model model){
model.addAttribute("about","709831589");
List<Category> categories = categoryService.findAllCategories();
model.addAttribute("categories",categories);
//设置选项
model.addAttribute("options",HaloConst.OPTIONS);
return this.render("about");
}
/**
*
*
* @return /themes/{theme}/gallery
*/
@GetMapping(value = "/gallery")
public String gallery(Model model){
List<Gallery> galleries = galleryService.findAllGalleries();
model.addAttribute("galleries",galleries);
//用户信息
User user = userService.findUser();
model.addAttribute("user",user);
model.addAttribute("is_gallery",true);
//设置选项
model.addAttribute("options",HaloConst.OPTIONS);
return this.render("gallery");
}
/**
*
*
* @param model model
* @return /themes/{theme}/links
*/
@GetMapping(value = "/links")
public String links(Model model){
//所有友情链接
List<Link> links = linkService.findAllLinks();
model.addAttribute("links",links);
//用户信息
User user = userService.findUser();
model.addAttribute("user",user);
model.addAttribute("is_links",true);
//文章总数
model.addAttribute("postsCount",postService.findAllPosts().size());
//菜单列表
List<Menu> menus = menuService.findAllMenus();
model.addAttribute("menus",menus);
//所有分类目录
List<Category> categories = categoryService.findAllCategories();
model.addAttribute("categories",categories);
//归档数据,包含[year,month,count,List<Post>]
List<Archive> archives = postService.findPostGroupByYearAndMonth();
model.addAttribute("archives",archives);
//设置选项
model.addAttribute("options",HaloConst.OPTIONS);
return this.render("links");
}
/**
*
*
* @param model model
* @return /themes/{theme}/tags
*/
@GetMapping(value = "/tags")
public String tags(Model model){
//所有标签
List<Tag> tags = tagService.findAllTags();
model.addAttribute("tags",tags);
//所有分类目录
List<Category> categories = categoryService.findAllCategories();
model.addAttribute("categories",categories);
User user = userService.findUser();
model.addAttribute("user",user);
//文章总数
model.addAttribute("postsCount",postService.findAllPosts().size());
//菜单列表
List<Menu> menus = menuService.findAllMenus();
model.addAttribute("menus",menus);
//归档数据,包含[year,month,count,List<Post>]
List<Archive> archives = postService.findPostGroupByYearAndMonth();
model.addAttribute("archives",archives);
//设置选项
model.addAttribute("options",HaloConst.OPTIONS);
return this.render("tags");
}
/**
*
*
* @param model model
* @param cateUrl cateUrl
* @return string
*/
@GetMapping(value = "categories/{cateUrl}")
public String categories(Model model,
@PathVariable("cateUrl") String cateUrl){
List<Post> posts;
return null;
}
/**
*
*
* @param model model
* @return
*/
@GetMapping(value = "/archives")
public String archives(Model model){
return this.archives(model,1);
}
/**
*
*
* @param model model
* @param page page
* @return /themes/{theme}/archives
*/
@GetMapping(value = "/archives/page/{page}")
public String archives(Model model,
@PathVariable(value = "page") Integer page){
//所有文章数据分页material主题适用
Sort sort = new Sort(Sort.Direction.DESC,"postDate");
Pageable pageable = new PageRequest(page-1,5,sort);
Page<Post> posts = postService.findPostByStatus(0,pageable);
model.addAttribute("posts",posts);
//文章总数
model.addAttribute("postsCount",postService.findAllPosts().size());
model.addAttribute("is_archives",true);
//包含[List<Post>,year,month,count]
List<Archive> archives = postService.findPostGroupByYearAndMonth();
model.addAttribute("archives",archives);
//包含[List<Post>,year,count]
List<Archive> archivesLess = postService.findPostGroupByYear();
model.addAttribute("archivesLess",archivesLess);
//用户信息
User user = userService.findUser();
model.addAttribute("user",user);
//菜单列表
List<Menu> menus = menuService.findAllMenus();
model.addAttribute("menus",menus);
//所有分类目录
List<Category> categories = categoryService.findAllCategories();
model.addAttribute("categories",categories);
//是否是归档页,用于判断输出链接
model.addAttribute("isArchives","true");
//设置选项
model.addAttribute("options",HaloConst.OPTIONS);
return this.render("archives");
}
/**
*
*
* @param model model
* @param year year
* @param month month
* @return /themes/{theme}/archives
*/
@GetMapping(value = "/archives/{year}/{month}")
public String archives(Model model,
@PathVariable(value = "year") String year,
@PathVariable(value = "month") String month){
log.info(year);
log.info(month);
//根据年月查出的文章数据,分页
Sort sort = new Sort(Sort.Direction.DESC,"post_date");
Pageable pageable = new PageRequest(0,5,sort);
Page<Post> posts = postService.findPostByYearAndMonth(year,month,pageable);
model.addAttribute("posts",posts);
//文章总数
model.addAttribute("postsCount",postService.findAllPosts().size());
//用户信息
User user = userService.findUser();
model.addAttribute("user",user);
//分类目录
List<Category> categories = categoryService.findAllCategories();
model.addAttribute("categories",categories);
//菜单列表
List<Menu> menus = menuService.findAllMenus();
model.addAttribute("menus",menus);
//归档数据,包含[year,month,count,List<Post>]
List<Archive> archives = postService.findPostGroupByYearAndMonth();
model.addAttribute("archives",archives);
//是否是归档页,用于判断输出链接
model.addAttribute("isArchives","true");
//设置选项
model.addAttribute("options",HaloConst.OPTIONS);
return this.render("archives");
}
/**
* rss
*
* @return rss
*/
@GetMapping(value = {"feed","feed.xml","atom.xml"},produces = {MediaType.APPLICATION_ATOM_XML_VALUE,MediaType.APPLICATION_RSS_XML_VALUE})
@ResponseBody
public String feed(){
String rssPosts = HaloConst.OPTIONS.get("rss_posts");
if(StringUtils.isBlank(rssPosts)){
rssPosts = "20";
}
//获取文章列表并根据时间排序
Sort sort = new Sort(Sort.Direction.DESC,"postDate");
Pageable pageable = new PageRequest(0,Integer.parseInt(rssPosts),sort);
Page<Post> postsPage = postService.findPostByStatus(0,pageable);
List<Post> posts = postsPage.getContent();
return postService.buildRss(posts);
}
/**
* sitemap
*
* @return sitemap
*/
@GetMapping(value = {"sitemap","sitemap.xml"},produces = MediaType.APPLICATION_XML_VALUE)
@ResponseBody
public String siteMap(){
//获取文章列表并根据时间排序
Sort sort = new Sort(Sort.Direction.DESC,"postDate");
Pageable pageable = new PageRequest(0,999,sort);
Page<Post> postsPage = postService.findPostByStatus(0,pageable);
List<Post> posts = postsPage.getContent();
return postService.buildSiteMap(posts);
}
/**
*
*
* @param comment comment
* @param post post
* @param request request
* @return truefalse
*/
@PostMapping(value = "/newComment")
@ResponseBody
public boolean newComment(@ModelAttribute("comment") Comment comment,
@ModelAttribute("post") Post post,
HttpServletRequest request){
if(""==comment.getCommentAuthor() || "".equals(comment.getCommentAuthor())){
comment.setCommentAuthor("小猪佩琪");
}
comment.setCommentAuthorEmail(comment.getCommentAuthorEmail().toLowerCase());
comment.setPost(post);
comment.setCommentDate(new Date());
comment.setCommentAuthorIp(HaloUtil.getIpAddr(request));
comment.setIsAdmin(0);
commentService.saveByComment(comment);
if("true".equals(HaloConst.OPTIONS.get("smtp_email_enable")) && "true".equals(HaloConst.OPTIONS.get("new_comment_notice"))){
try {
//发送邮件到博主
Map<String,Object> map = new HashMap<>();
map.put("author",userService.findUser().getUserDisplayName());
map.put("pageName",postService.findByPostId(post.getPostId()).get().getPostTitle());
map.put("blogUrl",HaloConst.OPTIONS.get("blog_url"));
map.put("visitor",comment.getCommentAuthor());
map.put("commentContent",comment.getCommentContent());
mailService.sendTemplateMail(userService.findUser().getUserEmail(),"有新的评论",map,"common/mail/mail_admin.ftl");
}catch (Exception e){
log.error("邮件服务器未配置:{0}",e.getMessage());
}
}
return true;
}
}

View File

@ -11,7 +11,7 @@ import cc.ryanc.halo.service.LogsService;
import cc.ryanc.halo.service.PostService;
import cc.ryanc.halo.service.UserService;
import cc.ryanc.halo.util.HaloUtil;
import cc.ryanc.halo.web.controller.BaseController;
import cc.ryanc.halo.web.controller.core.BaseController;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;

View File

@ -8,7 +8,7 @@ import cc.ryanc.halo.service.CommentService;
import cc.ryanc.halo.service.MailService;
import cc.ryanc.halo.service.UserService;
import cc.ryanc.halo.util.HaloUtil;
import cc.ryanc.halo.web.controller.BaseController;
import cc.ryanc.halo.web.controller.core.BaseController;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
@ -28,7 +28,6 @@ import javax.websocket.server.PathParam;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

View File

@ -8,7 +8,7 @@ import cc.ryanc.halo.service.LogsService;
import cc.ryanc.halo.service.PostService;
import cc.ryanc.halo.service.TagService;
import cc.ryanc.halo.util.HaloUtil;
import cc.ryanc.halo.web.controller.BaseController;
import cc.ryanc.halo.web.controller.core.BaseController;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;

View File

@ -6,7 +6,7 @@ import cc.ryanc.halo.model.dto.LogsRecord;
import cc.ryanc.halo.service.LogsService;
import cc.ryanc.halo.service.OptionsService;
import cc.ryanc.halo.util.HaloUtil;
import cc.ryanc.halo.web.controller.BaseController;
import cc.ryanc.halo.web.controller.core.BaseController;
import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;

View File

@ -1,53 +1,53 @@
package cc.ryanc.halo.web.controller;
import cc.ryanc.halo.model.domain.Comment;
import cc.ryanc.halo.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import javax.servlet.http.HttpSession;
/**
* @author : RYAN0UP
* @date : 2017/12/15
* @version : 1.0
* description:
*/
public abstract class BaseController {
/**
*
*/
public static String THEME = "halo";
@Autowired
private CommentService commentService;
/**
*
*
* @param pageName pageName
* @return
*/
public String render(String pageName){
StringBuffer themeStr = new StringBuffer("themes/");
themeStr.append(THEME);
themeStr.append("/");
return themeStr.append(pageName).toString();
}
/**
*
*
* @param session session
*/
protected void getNewComments(HttpSession session){
Sort sort = new Sort(Sort.Direction.DESC,"commentDate");
Pageable pageable = new PageRequest(0,999,sort);
Page<Comment> comments = commentService.findAllComments(1,pageable);
session.removeAttribute("newComments");
session.setAttribute("newComments",comments.getContent());
}
}
package cc.ryanc.halo.web.controller.core;
import cc.ryanc.halo.model.domain.Comment;
import cc.ryanc.halo.service.CommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import javax.servlet.http.HttpSession;
/**
* @author : RYAN0UP
* @date : 2017/12/15
* @version : 1.0
* description:
*/
public abstract class BaseController {
/**
*
*/
public static String THEME = "halo";
@Autowired
private CommentService commentService;
/**
*
*
* @param pageName pageName
* @return
*/
public String render(String pageName){
StringBuffer themeStr = new StringBuffer("themes/");
themeStr.append(THEME);
themeStr.append("/");
return themeStr.append(pageName).toString();
}
/**
*
*
* @param session session
*/
protected void getNewComments(HttpSession session){
Sort sort = new Sort(Sort.Direction.DESC,"commentDate");
Pageable pageable = new PageRequest(0,999,sort);
Page<Comment> comments = commentService.findAllComments(1,pageable);
session.removeAttribute("newComments");
session.setAttribute("newComments",comments.getContent());
}
}

View File

@ -1,74 +1,74 @@
package cc.ryanc.halo.web.controller;
import cc.ryanc.halo.model.dto.HaloConst;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import javax.servlet.http.HttpServletRequest;
/**
* @author : RYAN0UP
* @date : 2017/12/26
* @version : 1.0
* description:
*/
@Slf4j
@Controller
public class CommonController implements ErrorController{
private static final String ERROR_PATH = "/error";
/**
* 404500
*
* @param request request
* @return string
*/
@GetMapping(value = ERROR_PATH)
public String handleError(HttpServletRequest request){
Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
if(statusCode==404) {
return "redirect:/404";
}else{
return "redirect:/500";
}
}
/**
* 404
*
* @param model model
* @return string
*/
@GetMapping(value = "/404")
public String fourZeroFour(Model model){
//设置选项
model.addAttribute("options", HaloConst.OPTIONS);
return "common/404";
}
/**
* 500
*
* @param model model
* @return string
*/
@GetMapping(value = "/500")
public String fiveZeroZero(Model model){
//设置选项
model.addAttribute("options", HaloConst.OPTIONS);
return "common/500";
}
/**
* Returns the path of the error page.
*
* @return the error path
*/
@Override
public String getErrorPath() {
return ERROR_PATH;
}
}
package cc.ryanc.halo.web.controller.core;
import cc.ryanc.halo.model.dto.HaloConst;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import javax.servlet.http.HttpServletRequest;
/**
* @author : RYAN0UP
* @date : 2017/12/26
* @version : 1.0
* description:
*/
@Slf4j
@Controller
public class CommonController implements ErrorController{
private static final String ERROR_PATH = "/error";
/**
* 404500
*
* @param request request
* @return string
*/
@GetMapping(value = ERROR_PATH)
public String handleError(HttpServletRequest request){
Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
if(statusCode==404) {
return "redirect:/404";
}else{
return "redirect:/500";
}
}
/**
* 404
*
* @param model model
* @return string
*/
@GetMapping(value = "/404")
public String fourZeroFour(Model model){
//设置选项
model.addAttribute("options", HaloConst.OPTIONS);
return "common/404";
}
/**
* 500
*
* @param model model
* @return string
*/
@GetMapping(value = "/500")
public String fiveZeroZero(Model model){
//设置选项
model.addAttribute("options", HaloConst.OPTIONS);
return "common/500";
}
/**
* Returns the path of the error page.
*
* @return the error path
*/
@Override
public String getErrorPath() {
return ERROR_PATH;
}
}

View File

@ -1,4 +1,4 @@
package cc.ryanc.halo.web.controller;
package cc.ryanc.halo.web.controller.core;
import cc.ryanc.halo.model.domain.*;
import cc.ryanc.halo.model.dto.HaloConst;

View File

@ -0,0 +1,214 @@
package cc.ryanc.halo.web.controller.front;
import cc.ryanc.halo.model.domain.Category;
import cc.ryanc.halo.model.domain.Menu;
import cc.ryanc.halo.model.domain.Post;
import cc.ryanc.halo.model.domain.User;
import cc.ryanc.halo.model.dto.Archive;
import cc.ryanc.halo.model.dto.HaloConst;
import cc.ryanc.halo.service.CategoryService;
import cc.ryanc.halo.service.MenuService;
import cc.ryanc.halo.service.PostService;
import cc.ryanc.halo.service.UserService;
import cc.ryanc.halo.web.controller.core.BaseController;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.Date;
import java.util.List;
/**
* @author : RYAN0UP
* @version : 1.0
* @date : 2018/4/26
*/
@Slf4j
@Controller
@RequestMapping(value = "/archives")
public class ArchivesController extends BaseController {
@Autowired
private PostService postService;
@Autowired
private UserService userService;
@Autowired
private CategoryService categoryService;
@Autowired
private MenuService menuService;
/**
*
*
* @param model model
* @return
*/
@GetMapping
public String archives(Model model){
return this.archives(model,1);
}
/**
*
*
* @param model model
* @param page page
* @return /themes/{theme}/archives
*/
@GetMapping(value = "page/{page}")
public String archives(Model model,
@PathVariable(value = "page") Integer page){
//所有文章数据分页material主题适用
Sort sort = new Sort(Sort.Direction.DESC,"postDate");
Pageable pageable = new PageRequest(page-1,5,sort);
Page<Post> posts = postService.findPostByStatus(0,pageable);
model.addAttribute("posts",posts);
//文章总数
model.addAttribute("postsCount",postService.findAllPosts().size());
model.addAttribute("is_archives",true);
//包含[List<Post>,year,month,count]
List<Archive> archives = postService.findPostGroupByYearAndMonth();
model.addAttribute("archives",archives);
//包含[List<Post>,year,count]
List<Archive> archivesLess = postService.findPostGroupByYear();
model.addAttribute("archivesLess",archivesLess);
//用户信息
User user = userService.findUser();
model.addAttribute("user",user);
//菜单列表
List<Menu> menus = menuService.findAllMenus();
model.addAttribute("menus",menus);
//所有分类目录
List<Category> categories = categoryService.findAllCategories();
model.addAttribute("categories",categories);
//是否是归档页,用于判断输出链接
model.addAttribute("isArchives","true");
//设置选项
model.addAttribute("options",HaloConst.OPTIONS);
return this.render("archives");
}
/**
*
*
* @param model model
* @param year year
* @param month month
* @return /themes/{theme}/archives
*/
@GetMapping(value = "{year}/{month}")
public String archives(Model model,
@PathVariable(value = "year") String year,
@PathVariable(value = "month") String month){
log.info(year);
log.info(month);
//根据年月查出的文章数据,分页
Sort sort = new Sort(Sort.Direction.DESC,"post_date");
Pageable pageable = new PageRequest(0,5,sort);
Page<Post> posts = postService.findPostByYearAndMonth(year,month,pageable);
model.addAttribute("posts",posts);
//文章总数
model.addAttribute("postsCount",postService.findAllPosts().size());
//用户信息
User user = userService.findUser();
model.addAttribute("user",user);
//分类目录
List<Category> categories = categoryService.findAllCategories();
model.addAttribute("categories",categories);
//菜单列表
List<Menu> menus = menuService.findAllMenus();
model.addAttribute("menus",menus);
//归档数据,包含[year,month,count,List<Post>]
List<Archive> archives = postService.findPostGroupByYearAndMonth();
model.addAttribute("archives",archives);
//是否是归档页,用于判断输出链接
model.addAttribute("isArchives","true");
//设置选项
model.addAttribute("options",HaloConst.OPTIONS);
return this.render("archives");
}
/**
*
*
* @param postUrl
* @param model model
* @return /themes/{theme}/post
*/
@GetMapping(value = "{postUrl}")
public String getPost(@PathVariable String postUrl, Model model){
Post post = postService.findByPostUrl(postUrl);
//获得当前文章的发布日期
Date postDate = post.getPostDate();
try {
//查询当前文章日期之前的所有文章
List<Post> beforePosts = postService.findByPostDateBefore(postDate);
//查询当前文章日期之后的所有文章
List<Post> afterPosts = postService.findByPostDateAfter(postDate);
if(null!=beforePosts&&beforePosts.size()>0){
model.addAttribute("beforePost",beforePosts.get(beforePosts.size()-1));
}
if(null!=afterPosts&&afterPosts.size()>0){
model.addAttribute("afterPost",afterPosts.get(afterPosts.size()-1));
}
}catch (Exception e){
log.error("未知错误:{0}",e.getMessage());
}
model.addAttribute("post",post);
//文章总数
model.addAttribute("postsCount",postService.findAllPosts().size());
//用户信息
User user = userService.findUser();
model.addAttribute("user",user);
//所有分类目录
List<Category> categories = categoryService.findAllCategories();
model.addAttribute("categories",categories);
//归档数据,包含[year,month,count,List<Post>]
List<Archive> archives = postService.findPostGroupByYearAndMonth();
//菜单列表
List<Menu> menus = menuService.findAllMenus();
model.addAttribute("menus",menus);
model.addAttribute("archives",archives);
//设置选项
model.addAttribute("options",HaloConst.OPTIONS);
return this.render("post");
}
}

View File

@ -0,0 +1,34 @@
package cc.ryanc.halo.web.controller.front;
import cc.ryanc.halo.model.domain.Post;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
/**
* @author : RYAN0UP
* @version : 1.0
* @date : 2018/4/26
*/
@Controller
@RequestMapping(value = "categories")
public class CategoriesController {
/**
*
*
* @param model model
* @param cateUrl cateUrl
* @return string
*/
@GetMapping(value = "{cateUrl}")
public String categories(Model model,
@PathVariable("cateUrl") String cateUrl){
List<Post> posts;
return null;
}
}

View File

@ -0,0 +1,102 @@
package cc.ryanc.halo.web.controller.front;
import cc.ryanc.halo.model.domain.Comment;
import cc.ryanc.halo.model.domain.Post;
import cc.ryanc.halo.model.dto.HaloConst;
import cc.ryanc.halo.service.CommentService;
import cc.ryanc.halo.service.MailService;
import cc.ryanc.halo.service.PostService;
import cc.ryanc.halo.service.UserService;
import cc.ryanc.halo.util.HaloUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.*;
/**
* @author : RYAN0UP
* @version : 1.0
* @date : 2018/4/26
*/
@Slf4j
@Controller
public class CommentsController {
@Autowired
private CommentService commentService;
@Autowired
private PostService postService;
@Autowired
private UserService userService;
@Autowired
private MailService mailService;
/**
*
*
* @param postId postId
* @return List<Comment></>
*/
@GetMapping(value = "/getComment/{postId}",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@ResponseBody
public List<Comment> getComment(@PathVariable Long postId){
Optional<Post> post = postService.findByPostId(postId);
Sort sort = new Sort(Sort.Direction.DESC,"commentDate");
Pageable pageable = new PageRequest(0,10,sort);
List<Comment> comments = commentService.findCommentsByPostAndCommentStatus(post.get(),pageable,2).getContent();
if(null==comments){
return null;
}
return comments;
}
/**
*
*
* @param comment comment
* @param post post
* @param request request
* @return truefalse
*/
@PostMapping(value = "/newComment")
@ResponseBody
public boolean newComment(@ModelAttribute("comment") Comment comment,
@ModelAttribute("post") Post post,
HttpServletRequest request){
if(""==comment.getCommentAuthor() || "".equals(comment.getCommentAuthor())){
comment.setCommentAuthor("小猪佩琪");
}
comment.setCommentAuthorEmail(comment.getCommentAuthorEmail().toLowerCase());
comment.setPost(post);
comment.setCommentDate(new Date());
comment.setCommentAuthorIp(HaloUtil.getIpAddr(request));
comment.setIsAdmin(0);
commentService.saveByComment(comment);
if("true".equals(HaloConst.OPTIONS.get("smtp_email_enable")) && "true".equals(HaloConst.OPTIONS.get("new_comment_notice"))){
try {
//发送邮件到博主
Map<String,Object> map = new HashMap<>();
map.put("author",userService.findUser().getUserDisplayName());
map.put("pageName",postService.findByPostId(post.getPostId()).get().getPostTitle());
map.put("blogUrl",HaloConst.OPTIONS.get("blog_url"));
map.put("visitor",comment.getCommentAuthor());
map.put("commentContent",comment.getCommentContent());
mailService.sendTemplateMail(userService.findUser().getUserEmail(),"有新的评论",map,"common/mail/mail_admin.ftl");
}catch (Exception e){
log.error("邮件服务器未配置:{0}",e.getMessage());
}
}
return true;
}
}

View File

@ -0,0 +1,134 @@
package cc.ryanc.halo.web.controller.front;
import cc.ryanc.halo.model.domain.Category;
import cc.ryanc.halo.model.domain.Menu;
import cc.ryanc.halo.model.domain.Post;
import cc.ryanc.halo.model.domain.User;
import cc.ryanc.halo.model.dto.Archive;
import cc.ryanc.halo.model.dto.HaloConst;
import cc.ryanc.halo.service.CategoryService;
import cc.ryanc.halo.service.MenuService;
import cc.ryanc.halo.service.PostService;
import cc.ryanc.halo.service.UserService;
import cc.ryanc.halo.web.controller.core.BaseController;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.websocket.server.PathParam;
import java.util.List;
/**
* @author : RYAN0UP
* @version : 1.0
* @date : 2018/4/26
*/
@Controller
@RequestMapping(value = {"/","index"})
public class IndexController extends BaseController {
@Autowired
private PostService postService;
@Autowired
private UserService userService;
@Autowired
private CategoryService categoryService;
@Autowired
private MenuService menuService;
/**
*
*
* @param model model
* @return
*/
@GetMapping
public String index(Model model){
//调用方法渲染首页
return this.index(model,1);
}
/**
*
*
* @param model model
* @param page
* @param size
* @return /themes/{theme}/index
*/
@GetMapping(value = "page/{page}")
public String index(Model model,
@PathVariable(value = "page") Integer page){
Sort sort = new Sort(Sort.Direction.DESC,"postDate");
//默认显示10条
Integer size = 10;
//尝试加载设置选项,用于设置显示条数
if(!StringUtils.isBlank(HaloConst.OPTIONS.get("index_posts"))){
size = Integer.parseInt(HaloConst.OPTIONS.get("index_posts"));
}
//所有文章数据,分页
Pageable pageable = new PageRequest(page-1,size,sort);
Page<Post> posts = postService.findPostByStatus(0,pageable);
model.addAttribute("posts",posts);
//文章总数
model.addAttribute("postsCount",postService.findAllPosts().size());
model.addAttribute("is_home",true);
//用户信息
User user = userService.findUser();
model.addAttribute("user",user);
//所有分类目录
List<Category> categories = categoryService.findAllCategories();
model.addAttribute("categories",categories);
//菜单列表
List<Menu> menus = menuService.findAllMenus();
model.addAttribute("menus",menus);
//归档数据,包含[year,month,count,List<Post>]
List<Archive> archives = postService.findPostGroupByYearAndMonth();
model.addAttribute("archives",archives);
//设置选项
model.addAttribute("options",HaloConst.OPTIONS);
return this.render("index");
}
/**
* ajax
*
* @param page page
* @return List<Post></>
*/
@GetMapping(value = "next")
@ResponseBody
public List<Post> ajaxIndex(@PathParam(value = "page") Integer page){
Sort sort = new Sort(Sort.Direction.DESC,"postDate");
//默认显示10条
Integer size = 10;
//尝试加载设置选项,用于设置显示条数
if(!StringUtils.isBlank(HaloConst.OPTIONS.get("index_posts"))){
size = Integer.parseInt(HaloConst.OPTIONS.get("index_posts"));
}
//文章数据,只获取文章,没有分页
Pageable pageable = new PageRequest(page-1,size,sort);
List<Post> posts = postService.findPostByStatus(0,pageable).getContent();
return posts;
}
}

View File

@ -0,0 +1,65 @@
package cc.ryanc.halo.web.controller.front;
import cc.ryanc.halo.model.domain.Post;
import cc.ryanc.halo.model.dto.HaloConst;
import cc.ryanc.halo.service.PostService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
/**
* @author : RYAN0UP
* @version : 1.0
* @date : 2018/4/26
*/
@Controller
public class OthersController {
@Autowired
private PostService postService;
/**
* rss
*
* @return rss
*/
@GetMapping(value = {"feed","feed.xml","atom.xml"},produces = {MediaType.APPLICATION_ATOM_XML_VALUE,MediaType.APPLICATION_RSS_XML_VALUE})
@ResponseBody
public String feed(){
String rssPosts = HaloConst.OPTIONS.get("rss_posts");
if(StringUtils.isBlank(rssPosts)){
rssPosts = "20";
}
//获取文章列表并根据时间排序
Sort sort = new Sort(Sort.Direction.DESC,"postDate");
Pageable pageable = new PageRequest(0,Integer.parseInt(rssPosts),sort);
Page<Post> postsPage = postService.findPostByStatus(0,pageable);
List<Post> posts = postsPage.getContent();
return postService.buildRss(posts);
}
/**
* sitemap
*
* @return sitemap
*/
@GetMapping(value = {"sitemap","sitemap.xml"},produces = MediaType.APPLICATION_XML_VALUE)
@ResponseBody
public String siteMap(){
//获取文章列表并根据时间排序
Sort sort = new Sort(Sort.Direction.DESC,"postDate");
Pageable pageable = new PageRequest(0,999,sort);
Page<Post> postsPage = postService.findPostByStatus(0,pageable);
List<Post> posts = postsPage.getContent();
return postService.buildSiteMap(posts);
}
}

View File

@ -0,0 +1,116 @@
package cc.ryanc.halo.web.controller.front;
import cc.ryanc.halo.model.domain.*;
import cc.ryanc.halo.model.dto.Archive;
import cc.ryanc.halo.model.dto.HaloConst;
import cc.ryanc.halo.service.*;
import cc.ryanc.halo.web.controller.core.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import java.util.List;
/**
* @author : RYAN0UP
* @version : 1.0
* @date : 2018/4/26
*/
@Controller
public class PagesController extends BaseController {
@Autowired
private GalleryService galleryService;
@Autowired
private UserService userService;
@Autowired
private PostService postService;
@Autowired
private MenuService menuService;
@Autowired
private CategoryService categoryService;
@Autowired
private LinkService linkService;
/**
*
*
* @param model model
* @return /themes/{theme}/about
*/
@GetMapping(value = "/about")
public String about(Model model){
model.addAttribute("about","709831589");
//设置选项
model.addAttribute("options",HaloConst.OPTIONS);
return this.render("about");
}
/**
*
*
* @return /themes/{theme}/gallery
*/
@GetMapping(value = "/gallery")
public String gallery(Model model){
List<Gallery> galleries = galleryService.findAllGalleries();
model.addAttribute("galleries",galleries);
//用户信息
User user = userService.findUser();
model.addAttribute("user",user);
model.addAttribute("is_gallery",true);
//设置选项
model.addAttribute("options",HaloConst.OPTIONS);
return this.render("gallery");
}
/**
*
*
* @param model model
* @return /themes/{theme}/links
*/
@GetMapping(value = "/links")
public String links(Model model){
//所有友情链接
List<Link> links = linkService.findAllLinks();
model.addAttribute("links",links);
//用户信息
User user = userService.findUser();
model.addAttribute("user",user);
model.addAttribute("is_links",true);
//文章总数
model.addAttribute("postsCount",postService.findAllPosts().size());
//菜单列表
List<Menu> menus = menuService.findAllMenus();
model.addAttribute("menus",menus);
//所有分类目录
List<Category> categories = categoryService.findAllCategories();
model.addAttribute("categories",categories);
//归档数据,包含[year,month,count,List<Post>]
List<Archive> archives = postService.findPostGroupByYearAndMonth();
model.addAttribute("archives",archives);
//设置选项
model.addAttribute("options",HaloConst.OPTIONS);
return this.render("links");
}
}

View File

@ -0,0 +1,124 @@
package cc.ryanc.halo.web.controller.front;
import cc.ryanc.halo.model.domain.*;
import cc.ryanc.halo.model.dto.Archive;
import cc.ryanc.halo.model.dto.HaloConst;
import cc.ryanc.halo.service.*;
import cc.ryanc.halo.web.controller.core.BaseController;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
/**
* @author : RYAN0UP
* @version : 1.0
* @date : 2018/4/26
*/
@Controller
@RequestMapping(value = "/tags")
public class TagsController extends BaseController {
@Autowired
private TagService tagService;
@Autowired
private CategoryService categoryService;
@Autowired
private UserService userService;
@Autowired
private PostService postService;
@Autowired
private MenuService menuService;
/**
*
*
* @param model model
* @return /themes/{theme}/tags
*/
@GetMapping
public String tags(Model model){
//所有标签
List<Tag> tags = tagService.findAllTags();
model.addAttribute("tags",tags);
//所有分类目录
List<Category> categories = categoryService.findAllCategories();
model.addAttribute("categories",categories);
User user = userService.findUser();
model.addAttribute("user",user);
//文章总数
model.addAttribute("postsCount",postService.findAllPosts().size());
//菜单列表
List<Menu> menus = menuService.findAllMenus();
model.addAttribute("menus",menus);
//归档数据,包含[year,month,count,List<Post>]
List<Archive> archives = postService.findPostGroupByYearAndMonth();
model.addAttribute("archives",archives);
//设置选项
model.addAttribute("options",HaloConst.OPTIONS);
return this.render("tags");
}
/**
*
*
* @param tagUrl
* @param model model
* @return string
*/
@GetMapping(value = "{tagUrl}")
public String tags(Model model,
@PathVariable("tagUrl") String tagUrl){
return this.tags(model,tagUrl,1);
}
/**
*
* @param model model
* @param tagUrl
* @param page
* @return string
*/
@GetMapping(value = "{tagUrl}/page/{page}")
public String tags(Model model,
@PathVariable("tagUrl") String tagUrl,
@PathVariable("page") Integer page){
Tag tag = tagService.findByTagUrl(tagUrl);
Sort sort = new Sort(Sort.Direction.DESC,"postDate");
Integer size = 10;
if(!StringUtils.isBlank(HaloConst.OPTIONS.get("index_posts"))){
size = Integer.parseInt(HaloConst.OPTIONS.get("index_posts"));
}
Pageable pageable = new PageRequest(page-1,size,sort);
Page<Post> posts = postService.findPostsByTags(tag,pageable);
model.addAttribute("posts",posts);
User user = userService.findUser();
model.addAttribute("user",user);
//菜单列表
List<Menu> menus = menuService.findAllMenus();
model.addAttribute("menus",menus);
//设置选项
model.addAttribute("options",HaloConst.OPTIONS);
return this.render("index");
}
}