1.设置上一条、下一条数据
2.使用昌言插件
3.点击链接帖子的访问数加一
1.在mapper中设置分页:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.open1111.dao.ArticleDao">
<resultMap type="Article" id="ArticleResult">
<result property="id" column="id"/>
<result property="title" column="title"/>
<result property="publishDate" column="publishDate"/>
<result property="content" column="content"/>
<result property="summary" column="summary"/>
<result property="titleColor" column="titleColor"/>
<result property="click" column="click"/>
<result property="isRecommend" column="isRecommend"/>
<result property="isSlide" column="isSlide"/>
<result property="keyWords" column="keyWords"/>
<association property="arcType" column="typeId" select="com.open1111.dao.ArcTypeDao.findById"></association>
</resultMap>
<select id="getNewest" resultMap="ArticleResult">
select * from t_article order by publishDate desc limit 0,7
</select>
<select id="getRecommend" resultMap="ArticleResult">
select * from t_article where isRecommend=1 order by publishDate desc limit 0,7
</select>
<select id="getSlide" resultMap="ArticleResult">
select * from t_article where isSlide=1 order by publishDate desc limit 0,5
</select>
<select id="getIndex" parameterType="Integer" resultMap="ArticleResult">
select * from t_article where typeId=#{typeId} order by publishDate desc limit 0,8
</select>
<select id="findById" parameterType="Integer" resultMap="ArticleResult">
select * from t_article where id=#{id}
</select>
<select id="getLastArticle" parameterType="Integer" resultMap="ArticleResult">
SELECT * FROM t_article WHERE id < #{id} ORDER BY id DESC LIMIT 1;
</select>
<select id="getNextArticle" parameterType="Integer" resultMap="ArticleResult">
SELECT * FROM t_article WHERE id > #{id} ORDER BY id ASC LIMIT 1;
</select>
<update id="update" parameterType="Article">
update t_article
<set>
<if test="click!=0">
click=#{click},
</if>
</set>
where id=#{id}
</update>
</mapper>
dao:
package com.open1111.dao;
import java.util.List;
import com.open1111.entity.Article;
/**
* 帖子Dao接口
* @author user
*
*/
public interface ArticleDao {
/**
* 获取最新的7条帖子
* @return
*/
public List<Article> getNewest();
/**
* 获取最新7条推荐的帖子
* @return
*/
public List<Article> getRecommend();
/**
* 获取最新5条幻灯的帖子
* @return
*/
public List<Article> getSlide();
/**
* 根据帖子类别来查找最新的8条数据
* @param typeId
* @return
*/
public List<Article> getIndex(Integer typeId);
/**
* 通过id查询帖子
* @param id
* @return
*/
public Article findById(Integer id);
/**
* 获取上一个帖子
* @param id
* @return
*/
public Article getLastArticle(Integer id);
/**
* 获取下一个帖子
* @param id
* @return
*/
public Article getNextArticle(Integer id);
/**
* 更新帖子
* @param article
* @return
*/
public Integer update(Article article);
}
service:
package com.open1111.service;
import java.util.List;
import com.open1111.entity.Article;
/**
* 帖子Service接口
* @author user
*
*/
public interface ArticleService {
/**
* 获取最新的7条帖子
* @return
*/
public List<Article> getNewest();
/**
* 获取最新7条推荐的帖子
* @return
*/
public List<Article> getRecommend();
/**
* 获取最新5条幻灯的帖子
* @return
*/
public List<Article> getSlide();
/**
* 根据帖子类别来查找最新的8条数据
* @param typeId
* @return
*/
public List<Article> getIndex(Integer typeId);
/**
* 通过id查询帖子
* @param id
* @return
*/
public Article findById(Integer id);
/**
* 获取上一个帖子
* @param id
* @return
*/
public Article getLastArticle(Integer id);
/**
* 获取下一个帖子
* @param id
* @return
*/
public Article getNextArticle(Integer id);
/**
* 更新帖子
* @param article
* @return
*/
public Integer update(Article article);
}
seviceImpl
package com.open1111.service.impl;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.open1111.dao.ArticleDao;
import com.open1111.entity.Article;
import com.open1111.service.ArticleService;
/**
* 帖子Service实现类
* @author user
*
*/
@Service("articleService")
public class ArticleServiceImpl implements ArticleService{
@Resource
private ArticleDao articleDao;
public List<Article> getNewest() {
return articleDao.getNewest();
}
public List<Article> getRecommend() {
return articleDao.getRecommend();
}
public List<Article> getSlide() {
return articleDao.getSlide();
}
public List<Article> getIndex(Integer typeId) {
return articleDao.getIndex(typeId);
}
public Article findById(Integer id) {
return articleDao.findById(id);
}
public Article getLastArticle(Integer id) {
return articleDao.getLastArticle(id);
}
public Article getNextArticle(Integer id) {
return articleDao.getNextArticle(id);
}
public Integer update(Article article) {
return articleDao.update(article);
}
}
controller:
package com.open1111.controller;
import java.util.Arrays;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import com.open1111.entity.ArcType;
import com.open1111.entity.Article;
import com.open1111.service.ArticleService;
import com.open1111.util.NavUtil;
import com.open1111.util.StringUtil;
/**
* 帖子Controller层
* @author user
*
*/
@Controller
@RequestMapping("/article")
public class ArticleController {
@Resource
private ArticleService articleService;
@RequestMapping("/{id}")
public ModelAndView details(@PathVariable("id") Integer id,HttpServletRequest request)throws Exception{
ModelAndView mav=new ModelAndView();
Article article=articleService.findById(id);
String keyWords=article.getKeyWords();
if(StringUtil.isNotEmpty(keyWords)){
String arr[]=keyWords.split(" ");
mav.addObject("keyWords", StringUtil.filterWhite(Arrays.asList(arr)));
}else{
mav.addObject("keyWords", null);
}
mav.addObject("article", article);
article.setClick(article.getClick()+1);
articleService.update(article);
mav.addObject("pageCode", this.genUpAndDownPageCode(articleService.getLastArticle(id), articleService.getNextArticle(id), request.getServletContext().getContextPath()));
ArcType arcType=article.getArcType();
mav.addObject("navCode",NavUtil.genArticleNavigation(arcType.getTypeName(), arcType.getId(), article.getTitle()));
mav.setViewName("article");
return mav;
}
/**
* 获取下一篇帖子和上一篇帖子代码
* @param lastArticle
* @param nextArticle
* @param projectContext
* @return
*/
public String genUpAndDownPageCode(Article lastArticle,Article nextArticle,String projectContext){
StringBuffer pageCode=new StringBuffer();
if(lastArticle==null || lastArticle.getId()==null){
pageCode.append("<p>上一篇:没有了</p>");
}else{
pageCode.append("<p>上一篇:<a href=‘"+projectContext+"/article/"+lastArticle.getId()+".html‘>"+lastArticle.getTitle()+"</a></p>");
}
if(nextArticle==null || nextArticle.getId()==null){
pageCode.append("<p>下一篇:没有了</p>");
}else{
pageCode.append("<p>下一篇:<a href=‘"+projectContext+"/article/"+nextArticle.getId()+".html‘>"+nextArticle.getTitle()+"</a></p>");
}
return pageCode.toString();
}
}
页面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>${article.title }_Open1111官方网站 -南通小锋网络科技有限公司</title>
<META NAME="Author" CONTENT="Java1234_小锋老师">
<meta name="keywords"
content="${article.keyWords }" />
<meta name="description"
content="${article.summary }" />
<link rel="stylesheet"
href="${pageContext.request.contextPath}/static/css/open1111.css">
<script src="${pageContext.request.contextPath}/static/js/jQuery.js"></script>
</head>
<body>
<jsp:include page="/common/head.jsp"></jsp:include>
<jsp:include page="/common/menu.jsp"></jsp:include>
<div class="content">
<div class="w960">
<div class="pLeft">
<div class="data_list">
<div class="dataHeader navi">${navCode }</div>
<div class="article_title"><h2><strong>${article.title }</strong></h2></div>
<div class="share">
<div class="bshare-custom"><a title="分享到QQ空间" class="bshare-qzone"></a><a title="分享到新浪微博" class="bshare-sinaminiblog"></a><a title="分享到人人网" class="bshare-renren"></a><a title="分享到腾讯微博" class="bshare-qqmb"></a><a title="分享到网易微博" class="bshare-neteasemb"></a><a title="更多平台" class="bshare-more bshare-more-icon more-style-addthis"></a><span class="BSHARE_COUNT bshare-share-count">0</span></div><script type="text/javascript" charset="utf-8" src="http://static.bshare.cn/b/buttonLite.js#style=-1&uuid=&pophcol=2&lang=zh"></script><script type="text/javascript" charset="utf-8" src="http://static.bshare.cn/b/bshareC0.js"></script>
</div>
<div class="article_info">
发布时间:『<fmt:formatDate value="${article.publishDate }" pattern="yyyy-MM-dd HH:mm" />』
帖子类别:『${article.arcType.typeName}』 阅读次数:${article.click}
</div>
<div class="article_summary">
${article.summary }
</div>
<div class="article_content">
${article.content }
</div>
<div class="article_keyWord">
<font><strong>关键字:</strong></font>
<c:choose>
<c:when test="${keyWords==null }">
无
</c:when>
<c:otherwise>
<c:forEach var="keyWord" items="${keyWords }">
<a href="http://zhannei.baidu.com/cse/search?q=${keyWord }&s=5738221493575509323&entry=1" target="_blank">${keyWord }</a>
</c:forEach>
</c:otherwise>
</c:choose>
</div>
<div class="article_lastAndNextPage">
${pageCode }
</div>
</div>
<div>
<!--PC版-->
<div id="SOHUCS" sid="${article.id }"></div>
<script charset="utf-8" type="text/javascript" src="http://changyan.sohu.com/upload/changyan.js" ></script>
<script type="text/javascript">
window.changyan.api.config({
appid: ‘cysGfqVIm‘,
conf: ‘prod_b63f4160d3c1165b9a6d353811cc8e2d‘
});
</script>
</div>
</div>
<div class="pRight">
<div class="data_list">
<div class="dataHeader">站长推荐</div>
<div class="datas">
<ul>
<c:forEach var="article" items="${recommendArticleList }">
<li><a target="_blank"
href="${pageContext.request.contextPath}/article/${article.id}.html"
title="${article.title }"><font
color="${article.titleColor }">${fn:substring(article.title,0,16) }</font></a></li>
</c:forEach>
</ul>
</div>
</div>
<div class="data_list" style="margin-top: 10px;">
<div class="dataHeader">最近更新</div>
<div class="datas">
<ul>
<c:forEach var="article" items="${newestArticleList }">
<li><a target="_blank"
href="${pageContext.request.contextPath}/article/${article.id}.html"
title="${article.title }"><font
color="${article.titleColor }">${fn:substring(article.title,0,16) }</font></a></li>
</c:forEach>
</ul>
</div>
</div>
</div>
</div>
</div>
<jsp:include page="/common/foot.jsp"></jsp:include>
</body>
</html>