js写的评论分页(还不错)

复制代码 代码如下:


<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
+ request.getServerName() + ":" + request.getServerPort()
+ path + "https://www.jb51.net/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>微客服</title>
<style type="text/css">
body{
text-align: center;
margin: 0;
padding: 0;
color: #500f60;
background: url("../images/bj_4.jpg");
background-repeat:no-repeat;
background-attachment: scroll;
background-size:100% 100%;
overflow-x: hidden;
}
li{
list-style-type: none;
}
a:link{
list-style-type: none;
}
img{
width: 100%;
}
#static{
margin: 0 auto;
text-align: left;
width: 90%;
margin-top: 20px;
}
.aa{
padding-left: 16px;
}
#bott{
border: 3px #590303 solid;
border-radius: 6px 6px 6px 6px;
-moz-border-radius: 6px;
width: 90%;
margin-left: 5%;
}
.tex{
border-top: none;
border-bottom: none;
border-left: none;
border-right: none;
background: fixed;
}
</style>
</head>
<script type="text/javascript">
$(function() {
var row = 5;
//动态加评论信息
function loadCommentInfo(page) {
$("#page").text(page);
$.ajax({
type : "POST",
url : "<%=basePath%>findClickCommentByPage.action",
data: "page="+page+"&row="+row,
success : function(data) {
var list = data.list;
var row = "";
$("#comments").empty();
for ( var i = 0; i < list.length; i++) {
row = "<h3><span style=\"color: #000; font-size: 0.5em; padding-left: 70%;\">"+list[i].time+"</span></h3>"+
"<h3 style=\"padding-left: 12px;\">"+list[i].content+"</h3>";
if(list[i].repcontent!=null){
row+="<h3 style=\"padding-left: 12px; word-wrap: break-word; word-break: normal;\">回复:"+list[i].repcontent+"</h3>";
}
"<hr size=\"5px;\" color=\"#f0f\">";
$(row).appendTo($("#comments"));
}
$("#pagetag").val(page);
}
});
};
function getTotalContent() {
$.ajax({
type : "POST",
url : "<%=basePath%>getTotalNum.action",
success : function(data) {
$("#count").text(data.total);
if(parseInt(data.total)==0){
$("#page").text(0);
}
var pagenum = parseInt(data.total/row);
$("#totalpage").text(parseInt(data.total%row==0?pagenum:pagenum+1));
},
});
}
$("#submit").click(function(){
var content = $("#content").val();
if(content==""){
alert("内容不能为空!");
return;
}
$.post("<%=basePath%>addClickComment.action","content="+content,function(data){
if(data.success == true) {
alert("发表成功!");
$("#content").val("");
loadCommentInfo(parseInt($("#pagetag").val()));
getTotalContent();
}
});
});
$("#pre").click(function(){
var page = parseInt($("#pagetag").val());
if(page>1){
page--;
loadCommentInfo(page);
}
});
$("#next").click(function(){
var page = parseInt($("#pagetag").val());
if(page<parseInt($("#totalpage").text())){
page++;
loadCommentInfo(page);
}
});
window.onload = loadCommentInfo(1);
window.onload = getTotalContent();
});
</script>
<body>
<input type="hidden" value="1">
<div>
<div>
<img src="https://www.jb51.net/pic/top4.jpg">
<a href="https://www.jb51.net/<%=basePath%>jsp/index.jsp"><img src="https://www.jb51.net/pic/fan_2.png"></a>
</div>
<button>上一页</button>
<button>下一页</button>
<p>第<span></span>/<span></span>页</p>
<h1>评论(<span></span>)</h1>
<hr size="5px;" color="#590303">
<div>
</div>
<h2>发表评论</h2>
<h3>您的评论:</h3>
<div>
<textarea rows="7" cols="100%"></textarea>
</div>
<button>发表</button>
</div>
</body>
</html>


dao层

复制代码 代码如下:


package dfml.daoImpl;

import java.sql.SQLException;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Component;

import dfml.dao.ClickCommentDao;
import dfml.pojo.ClickComment;

@Component
public class ClickCommentDaoImpl implements ClickCommentDao{

private HibernateTemplate hibernateTemplate;

@Resource
public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
this.hibernateTemplate = hibernateTemplate;
}
//添加一条评论信息
@Override
public boolean addClickComment(ClickComment clickComment) {
boolean isSuccess = false;
try {
hibernateTemplate.save(clickComment);
isSuccess = true;
} catch (Exception e) {
isSuccess = false;
e.printStackTrace();
}
return isSuccess;
}
//分页查找评论信息
@SuppressWarnings("unchecked")
@Override
public List<ClickComment> findClickCommentByPage(final int page, final int row) {
List<ClickComment> list = this.hibernateTemplate
.executeFind(new HibernateCallback<Object>() {
@Override
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Criteria c = session.createCriteria(ClickComment.class);
c.setFirstResult((page - 1) * row);
c.setMaxResults(row);
c.addOrder(Order.desc("time"));
return c.list();
}
});
return list;
}
//得到评论的个数
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public Long getClickCommentCount() {
final String hql = "select count(*) from ClickComment";
Long result = null;
result = (Long) hibernateTemplate.execute(new HibernateCallback() {
public Object doInHibernate(Session arg0)
throws HibernateException, SQLException {
Query query = arg0.createQuery(hql);
return query.uniqueResult();
}
});
return result;
}
//更新评论信息
@Override
public boolean updateClickComment(ClickComment clickComment) {
boolean isSuccess=false;
try {
hibernateTemplate.update(clickComment);
isSuccess=true;
} catch (Exception e) {
e.printStackTrace();
isSuccess=false;
}
return isSuccess;
}
//根据id查找评论信息
@Override
public ClickComment findClickCommentById(int id) {
return (ClickComment) hibernateTemplate.find("from ClickComment where id = ?",
id).get(0);
}
//删除评论信息
@Override
public boolean deleteClickComment(ClickComment clickComment) {
boolean isSuccess=false;
try {
hibernateTemplate.delete(clickComment);
isSuccess=true;
} catch (Exception e) {
e.printStackTrace();
isSuccess=false;
}
return isSuccess;
}
//查询所有的评论
@SuppressWarnings("unchecked")
@Override
public List<ClickComment> findAllClickComment() {
return hibernateTemplate.find("from ClickComment");
}

}


struts配置

复制代码 代码如下:

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wdwgyj.html