Springboot项目前台界面
- 确定前台各页面路径
<a href="#" th:href="@{/}" class="m-item item m-mobile-hide " th:classappend="${n==1} ? 'active'">首页</a>
<a href="#" th:href="@{/types/-1}" class="m-item item m-mobile-hide" th:classappend="${n==2} ? 'active'">分类</a>
<a href="#" th:href="@{/tags/-1}" class="m-item item m-mobile-hide" th:classappend="${n==3} ? 'active'">标签</a>
<a href="#" th:href="@{/archives}" class="m-item item m-mobile-hide" th:classappend="${n==4} ? 'active'">归档</a>
<a href="#" th:href="@{/about}" class="m-item item m-mobile-hide" th:classappend="${n==5} ? 'active'">关于我</a>
前台首页
前台首页index.html展示内容有分类、标签和新闻。
control层IndexController
新建类IndexController,分页展示新闻、展示前五的分类和标签
@Controller
@RequestMapping
public class IndexController {
@Autowired
private NewsServcie newsServcie;
@Autowired
private TypeService typeService;
@Autowired
private TagService tagService;
@RequestMapping
public String index(@PageableDefault(size = 5, sort = {"updateTime"}, direction = Sort.Direction.DESC) Pageable pageable,
Model model){
Page<News> page = newsServcie.findByPageable(pageable);
List<Type> types=typeService.findTop(5);
List<Tag> tags=tagService.findTop(5);
model.addAttribute("page", page);
model.addAttribute("types",types);
model.addAttribute("tags",tags);
return "index";
}
}
service层
- 在typeServicel新建findTop()方法
根据newsList里的数量进行排序,用pageable记录,还有当前页和每页显示条数,再传到typeDao
@Override
public List<Type> findTop(int i) {
Sort sort=Sort.by(Sort.Direction.DESC,"newsList.size");
Pageable pageable = PageRequest.of(0, i, sort);
return typeDao.findTop(pageable);
}
- 在tagServicel同样
@Override
public List<Tag> findTop(int i) {
Sort sort=Sort.by(Sort.Direction.DESC,"newsList.size");
Pageable pageable = PageRequest.of(0, i, sort);
return tagDao.findTop(pageable);
}
index.html里把分类和标签展示出来
<a href="#" th:href="@{/types/{id}(id=${type.id})}" target="_blank" class="item" th:each="type : ${types}" >
<span th:text="${type.name}" >学习日志</span>
<div class="ui teal basic left pointing label" th:text="${#arrays.length(type.newsList)}" >13</div>
</a>
<a href="#" target="_blank" th:href="@{/tags/{id}(id=${tag.id})}" class="ui teal basic left pointing label m-margin-tb-tiny" th:each="tag:${tags}" >
<span th:text="${tag.name}" >方法论</span> <div class="detail" th:text="${#arrays.length(tag.newsList)}">23</div>
</a>
结果展示
前台分类
- 默认显示的是第一条也就是数量最多的那一条,如果从主界面的其他分类跳转过来,就根据分类的id判断。最后用model传到页面显示
control层TypeShowController
@Controller
public class TypeShowController {
@Autowired
private TypeService typeService;
@Autowired
private NewsServcie newsServcie;
@RequestMapping("/types/{id}")
public String types(@PageableDefault(size = 5,sort = {"updateTime"},direction = Sort.Direction.DESC)Pageable pageable,
@PathVariable Long id, Model model){
List<Type> types = typeService.findTop(7);
if(id==-1){
id=types.get(0).getId();
}
NewsQuery newsQuery=new NewsQuery();
newsQuery.setTypeId(id.toString());
Page<News> page = newsServcie.searchNews(pageable, newsQuery);
model.addAttribute("page",page);
model.addAttribute("types",types);
return "types";
}
}
结果演示
前台标签
- 和分类页面基本类似
control层TagShowController
@Controller
public class TagShowController {
@Autowired
private TagService tagService;
@Autowired
private NewsServcie newsServcie;
@RequestMapping("/tags/{id}")
public String tags(@PageableDefault(size = 5,sort = {"updateTime"},direction = Sort.Direction.DESC) Pageable pageable,
@PathVariable Long id, Model model){
List<Tag> tags = tagService.findTop(7);
if(id==-1){
id=tags.get(0).getId();
}
Page<News> page=newsServcie.searchNews(pageable,id);
model.addAttribute("page",page);
model.addAttribute("tags",tags);
return "tags";
}
}
service层
- 在NewsService新建一个方法searchNews(pageable,id)
@Override
public Page<News> searchNews(Pageable pageable, Long id) {
return newsDao.findAll(new Specification<News>() {
@Override
public Predicate toPredicate(Root<News> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
Join join=root.join("tags");
return criteriaBuilder.equal(join.get("id"),id);
}
}, pageable);
}
结果展示
归纳
- 该页面显示新闻总数,并按照年份进行归纳,用model展示到页面当中去。
control层ArchivesController
我们希望service层有个方法把我们的新闻和年份分类然后放到Hash,还有一个计算总新闻数的方法,将得到的对象用model显示在界面中
@Controller
public class ArchivesController {
@Autowired
private NewsServcie newsServcie;
@RequestMapping("archives")
public String arvhives(Model model){
HashMap<String, List<News>> map=newsServcie.archiveNews();
Long count=newsServcie.countNews();
model.addAttribute("newsCount",count);
model.addAttribute("archiveMap",map);
return "archives";
}
}
service层
- 在newsService里添加这两个方法,通过dao层得到总新闻数,还有给年份分组,得到每年的新闻
@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;
}
dao层
- newsDao中添加两个方法,根据年份分组并排序,得到每年的新闻。
public interface NewsDao extends JpaRepository<News,Long> , JpaSpecificationExecutor<News> {
@Query("select function('date_format',n.updateTime,'%Y') as year from News n group by function('date_format',n.updateTime,'%Y') order by year desc ")
List<String> findGroupYear();
@Query("select n from News n where function('date_format',n.updateTime,'%Y') = ?1")
List<News> findByYear(String y);
}