首先在maven repository中找到jsoup jar包,贴到pom.xml中
在自己对应的实体文件创建相关属性
Blog
//博客里存在的图片,主要用于缩略图
private List imageList = new LinkedList()
不需要进行数据库相关操作,因为image节点存在于Blog 的content 属性中,只需要利用jsoup提取其中的img节点
在IndexController控制器中提取图片
package com.java1234.controller;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import com.java1234.entity.Blog;
import com.java1234.entity.PageBean;
import com.java1234.service.BlogService;
import com.java1234.util.PageUtil;
import com.java1234.util.StringUtil;
/**
* 主页Contrller
* @author Administrator
*
*/
@Controller
@RequestMapping("/")
public class IndexController {
@Resource
private BlogService blogService;
/**
* 请求主页
* @return
*/
@RequestMapping("/index")
public ModelAndView index(@RequestParam(value="page",required=false)String page,@RequestParam(value="typeId",required=false)String typeId,@RequestParam(value="releaseDateStr",required=false)String releaseDateStr,HttpServletRequest request)throws Exception{
ModelAndView mav=new ModelAndView();
if(StringUtil.isEmpty(page)){
page="1";
}
PageBean pageBean=new PageBean(Integer.parseInt(page),10);
Map<String,Object> map=new HashMap<String,Object>();
map.put("start", pageBean.getStart());
map.put("size", pageBean.getPageSize());
map.put("typeId", typeId);
map.put("releaseDateStr", releaseDateStr);
List<Blog> blogList=blogService.list(map);
//jsoup提取缩略图核心代码
for(Blog blog:blogList){
List<String> imageList=blog.getImageList();
String blogInfo=blog.getContent();
Document doc=Jsoup.parse(blogInfo);
Elements jpgs=doc.select("img[src$=.jpg]");
for(int i=0;i<jpgs.size();i++){
Element jpg=jpgs.get(i);
imageList.add(jpg.toString());
//保证每次显示三张缩略图
if(i==2){
break;
}
}
}//结束
mav.addObject("blogList", blogList);
StringBuffer param=new StringBuffer();
if(StringUtil.isNotEmpty(typeId)){
param.append("typeId="+typeId+"&");
}
if(StringUtil.isNotEmpty(releaseDateStr)){
param.append("releaseDateStr="+releaseDateStr+"&");
}
mav.addObject("pageCode", PageUtil.genPagination(request.getContextPath()+"/index.html", blogService.getTotal(map), Integer.parseInt(page), 10, param.toString()));
mav.addObject("pageTitle", "java开源博客系统");
mav.addObject("mainPage", "foreground/blog/list.jsp");
mav.setViewName("mainTemp");
return mav;
}
/**
* 源码下载
* @return
* @throws Exception
*/
@RequestMapping("/download")
public ModelAndView download()throws Exception{
ModelAndView mav=new ModelAndView();
mav.addObject("pageTitle", "本站源码下载页面_java开源博客系统");
mav.addObject("mainPage", "foreground/system/download.jsp");
mav.setViewName("mainTemp");
return mav;
}
}
list.jsp 页面显示
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<div class="data_list">
<div class="data_list_title">
<img src="${pageContext.request.contextPath}/static/images/list_icon.png"/>
最新博客</div>
<div class="datas">
<ul>
<c:forEach var="blog" items="${blogList }">
<li style="margin-bottom: 30px">
<span class="date"><a href="${pageContext.request.contextPath}/blog/articles/${blog.id }.html"><fmt:formatDate value="${blog.releaseDate }" type="date" pattern="yyyy年MM月dd日"/></a></span>
<span class="title"><a href="${pageContext.request.contextPath}/blog/articles/${blog.id }.html">${blog.title }</a></span>
<span class="summary">摘要: ${blog.summary }...</span>
> <span class="img">
> <c:forEach var="image" items="${blog.imageList }">
> <a href="/blog/articles/${blog.id }.html">${image}</a>
>
> </c:forEach>
> </span>
<span class="info">发表于 <fmt:formatDate value="${blog.releaseDate }" type="date" pattern="yyyy-MM-dd HH:mm"/> 阅读(${blog.clickHit }) 评论(${blog.replyHit }) </span>
</li>
<hr style="height:5px;border:none;border-top:1px dashed gray;padding-bottom: 10px;" />
</c:forEach>
</ul>
</div>
</div>
<div>
<nav>
<ul class="pagination pagination-sm">${pageCode }</ul>
</nav>
</div>
页面效果