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 + "/";
%>
<!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" name="viewport">
<script type="text/javascript" src="http://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" id="pagetag" value="1">
<div id="static">
<div style="text-align: left; z-index: 999">
<img src="../pic/top4.jpg">
<a href="<%=basePath%>jsp/index.jsp"><img src="../pic/fan_2.png" style="width: 10%; margin-top: -25px;"></a>
</div>
<button style="background: #520202; border-top: none;border: 3px #520202 solid; border-radius: 2px 2px 2px 2px;-moz-border-radius: 6px; color: #FFEA00;" id="pre">上一页</button>
<button style="background: #520202; border-top: none;border: 3px #520202 solid; border-radius: 2px 2px 2px 2px;-moz-border-radius: 6px;float: right; color: #FFEA00;" id="next">下一页</button>
<p style="background: #520202; color: #FFEA00;">第<span id="page"></span>/<span id="totalpage"></span>页</p>
<h1 class="aa">评论(<span id="count"></span>)</h1>
<hr size="5px;" color="#590303">
<div id="comments">
</div>
<h2 class="aa">发表评论</h2>
<h3 class="aa">您的评论:</h3>
<div id="bott">
<textarea rows="7" class="tex" cols="100%" id="content"></textarea>
</div>
<button style="margin-left:80%; border-top: none;border: 3px #520202 solid; border-radius: 6px 6px 6px 6px;-moz-border-radius: 6px; color: #FFEA00; background: #520202;" id="submit">发表</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配置


代码如下:

<package name="clickComment" extends="json-default" namespace="/">
<action name="addClickComment" method="addClickComment" class="clickCommentAction">
<result name="add" type="json">
<param name="root">map</param>
</result>
</action>
<action name="findClickCommentByPage" method="findClickCommentByPage" class="clickCommentAction">
<result name="findByPage" type="json">
<param name="root">map</param>
</result>
</action>
<action name="getTotalNum" method="getTotalNum" class="clickCommentAction">
<result name="total" type="json">
<param name="root">map</param>
</result>
</action>
<action name="updateClickComment" method="updateClickComment" class="clickCommentAction">
<result name="update" type="json">
<param name="root">map</param>
</result>
</action>
<action name="findAllClickComment" method="findAllClickComment" class="clickCommentAction">
<result name="list" type="json">
<param name="root">map</param>
</result>
</action>
<action name="deleteClickComment" method="deleteClickComment" class="clickCommentAction">
<result name="delete" type="json">
<param name="root">map</param>
</result>
</action>

action


代码如下:

package dfml.action;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.opensymphony.xwork2.ActionSupport;

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

@Component("clickCommentAction")
@Scope("prototype")
public class ClickCommentAction extends ActionSupport{

private static final long serialVersionUID = 1L;

private ClickCommentDao clickCommentDao;
private Map<String, Object> map;
private String content;// 评论内容
private String repcontent;// 回复评论
private int page;
private int row;
private int rows;
private int id;
public void setId(int id) {
this.id = id;
}
public Map<String, Object> getMap() {
return map;
}
@Resource
public void setClickCommentDao(ClickCommentDao clickCommentDao) {
this.clickCommentDao = clickCommentDao;
}
public void setContent(String content) {
this.content = content;
}
public void setRepcontent(String repcontent) {
this.repcontent = repcontent;
}
public void setPage(int page) {
this.page = page;
}
public void setRow(int row) {
this.row = row;
}
public void setRows(int rows) {
this.rows = rows;
}
//添加评论 用于微信用户
public String addClickComment() {
boolean isSuccess = false;
map = new HashMap<String, Object>();
ClickComment clickComment = new ClickComment();
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
clickComment.setTime(format.format(new Date(System.currentTimeMillis())));
clickComment.setContent(content);
try {
isSuccess = clickCommentDao.addClickComment(clickComment);
} catch (Exception e) {
isSuccess = false;
e.printStackTrace();
}
map.put("success", isSuccess);
return "add";
}
//分页查找评论 用户微信用户
public String findClickCommentByPage() {
map = new HashMap<String, Object>();
map.put("list", clickCommentDao.findClickCommentByPage(page, row));
return "findByPage";
}
//查询评论条数 用于微信用户
public String getTotalNum(){
map = new HashMap<String, Object>();
map.put("total", clickCommentDao.getClickCommentCount());
return "total";
}
//回复评论 用于后台管理
public String updateClickComment(){
boolean isSuccess=false;
map=new HashMap<String, Object>();
ClickComment clickComment =clickCommentDao.findClickCommentById(id);
if(clickComment!=null){
clickComment.setRepcontent(repcontent);
isSuccess=clickCommentDao.updateClickComment(clickComment);
}
map.put("success", isSuccess);
return "update";
}
//查询所有评论 用于后台管理
public String findAllClickComment(){
map=new HashMap<String, Object>();
List<ClickComment> lists=clickCommentDao.findClickCommentByPage(page, rows);
List<ClickComment> listss=clickCommentDao.findAllClickComment();
map.put("rows", lists);
map.put("total", listss.size());
map.put("list", listss);
return "list";
}
//删除评论 用于后台管理
public String deleteClickComment(){
boolean isSuccess=false;
map=new HashMap<String, Object>();
ClickComment clickComment =clickCommentDao.findClickCommentById(id);
if(clickComment!=null){
isSuccess=clickCommentDao.deleteClickComment(clickComment);
}
map.put("success", isSuccess);
return "delete";
}

}

pojo


代码如下:

package dfml.pojo;

import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

//评论表
@Entity
public class ClickComment implements Serializable{

private static final long serialVersionUID = 1L;
private Integer id;
private String time;// 评论时间
private String content;// 评论内容
private String name;// 评论人
private String repcontent;//回复评论

@Id
@GeneratedValue
public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}
public String getRepcontent() {
return repcontent;
}
public void setRepcontent(String repcontent) {
this.repcontent = repcontent;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getContent() {
return content;
}

public void setContent(String content) {
this.content = content;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}

(0)

相关推荐

  • Vue.js实现文章评论和回复评论功能

    本来想把这个页面用jade渲染出来.评论部分用vue,但是想了想觉得麻烦,最后还是整个用vue的组件搞定他吧. 先上在线demo:http://jsbin.com/ceqifo/1/edit?js,output 再上效果图 可直接评论,点击别人的评论能回复别人的评论. html <div id="comment"> <article-content v-bind:article="article"></article-content&g

  • vue.js实现用户评论、登录、注册、及修改信息功能

    vue.js实现用户评论.登录.注册.及修改用户部分信息功能代码.效果图如下: 登入后: 登入前: 登录框: 注册框: html代码部分: <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>index</title> <link rel="stylesheet" href=&quo

  • JS类定义原型方法的两种实现的区别评论很多

    我们知道,给JavaScript类添加原形(prototype)方法是很简单的.而且常用的有下面这两种方法,可是这两种方法在使用时有区别吗?     JScript Class:  复制代码 代码如下: function JSClass()   {        } Extends prototype method:  复制代码 代码如下: JSClass.prototype.MethodA = function()   { }; Or   复制代码 代码如下: function = JSCla

  • JavaScript实现QQ聊天消息展示和评论提交功能

    QQ聊天消息显示,提交评论等实现原理,具体内容如下 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <style type="text/css"> * { margin: 0; padding: 0; } .bos { margin: 100px auto; width: 350px; posi

  • 原生js实现回复评论功能

    实现原理 功能1.删除状态 用removeChild()方法即可 功能2.最上面的点赞 判断文字的内容是否为赞,做相应操作改变存放赞数量的容器文本内容 功能3.回复评论 创建一个新的评论添加到评论列表里 功能4.回复里的点赞 判断我是否点了赞,做相应操作 功能5.回复或者删除 判断字符串回复还是删除,做相应操作 代码用了事件代理,还有三元运算判断,减少代码量 每行代码都有详细的注释 一眼看到那么多的代码不要烦躁 其实你要把每个功能单独去看都是很简单的dom操作 一点点消化,读懂每一行代码 完整代

  • js实现简单的星级选择器提交效果适用于评论等

    星级选择器,提交代码适用于评论等 没有用到js库,就是单独的js代码   复制代码 代码如下: <form action="" method="get"> <div id="star_level" star_width="14"> <p>服务</p> <ul class="star_rating"> <li style="displ

  • JS扩展Z-Blog图片验证码的单击自动刷新与评论内容自动保存

    在script/common.js最后加入下面的话可以实现图片验证码的自动刷新与评论内容自动保存(ForIE) 由于Z-Blog的JS扩展机制不够好,在不重建的情况下扩展东西只能用window.onload(IE). 既然用了window.onload,那就干脆用window.clipboardData了. 大家是不是期盼这两个功能很久了?没有了Ajax的,这两个功能就太重要太重要太重要了. 实际这个函数因为先天缺陷并不完美,下一版,构想在每一页下面加上一句话读一次LoadExtraScript

  • 一个jsp+AJAX评论系统第1/2页

    这是一个简单的评论系统,使用了JDOM(这边使用Jdom-b9),实例使用JSP作为视图,结合使用AJAX(用到prototype-1.4),Servlet和JavaBean作为后台处理,使用xml文件存储数据. 1.应用目录结构如下: data   |--comment.xml js   |--prototype.js   |--ufo.js(UTF-8格式)                                                                    

  • Json实现异步请求提交评论无需跳转其他页面

    主要将代码粘贴,通过阅读代码理解其中的相关逻辑. html代码: <form id="form1" runat="server"> <p> 评论:</p> <p> 姓名:<input type="text" name="username" id="username1" /></p> <p> 内容:<textarea n

  • 使用AngularJS和PHP的Laravel实现单页评论的方法

    完整代码:https://github.com/scotch-io/laravel-angular-comment-app 目前,Laravel和Angular均已经成为了Web发展世界里非常著名的工具.Laravel以给PHP社区引入的伟大内容著称,Angular以其惊人的前端工具及简单著称.组合这两大框架似乎是合乎逻辑的下一步. 在我们的使用环境下,我们将使用Laravel作为后端的RESTful API,Angular作为前端,以创建一个简单的单页的评论应用. 下面是一个简单的例子,展示了

随机推荐