一、原理
用户查看上,点击查看评价,能够看到该商品的评价
根据评价信息,我们确定comments包含id,gid,uname,comment,add_time(评价id,商品id,发表者,评价信息,添加时间)
1、数据库建立comments表
SQL语句复习
insert into comments(gid,uname,comment)values(1001,'www','hello')
select * from commentsdelete from comments where id=1 and uname='www'insert into comments(gid,uname,comment)values(1001,'www','hello')
insert into comments(gid,uname,comment)values(1001,'www','test')
2、设计Comment.java(Bean)
3、设计商品评价DAO(commentDAO.java)
(1)list<Comment>searcher(int gid)查询某商品的评价
(2)list<Comment>Searcher(int gid,PageInfo pageInfo) 分页查询商品评价
(3)add(Commentc)添加评价
....可以根据需要添加更改评价,删除评价等
update(int id,String uname,String msg)//id为评价信息id,建议可以更改评价内容,其他不能更改,发表留言的方可删除
delete(int uname,int id) //删除评价,可以考虑只有发表评价用户可以删除
二、评价Bean设计
package mybean;
public class Comment { private int id; private String uname; private int gid; private String comment; private String add_time; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUname() { return uname; } public void setUname(String uname) { this.uname = uname; } public int getGid() { return gid; } public void setGid(int gid) { this.gid = gid; } public String getComment() { return comment; } public void setComment(String comment) { this.comment = comment; } public String getAdd_time() { return add_time; } public void setAdd_time(String add_time) { this.add_time = add_time; } }三、评价DAO设计
1、实现查看评价和添加评价
package mybean;
import java.sql.*;import java.util.*;public class CommentDAO { public LinkedList<Comment> searcher(int gid){ Connection conn=null; PreparedStatement ps=null; ResultSet rs=null; LinkedList<Comment> cs=new LinkedList<Comment>(); String sql="select * from comments where gid=?"; try { Class.forName(Const.DRIVER); conn=DriverManager.getConnection(Const.URL, Const.USER, Const.PASSWORD); ps=conn.prepareStatement(sql); ps.setInt(1, gid); rs=ps.executeQuery(); while(rs.next()){ Comment c=new Comment(); c.setId(rs.getInt("id")); c.setUname(rs.getString("uname")); c.setGid(rs.getInt("gid")); c.setComment(rs.getString("comment")); c.setAdd_time(rs.getString("add_time")); cs.add(c); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } try{ if(rs!=null) rs.close(); if(ps!=null) ps.close(); if(conn!=null) conn.close(); }catch(Exception e){ e.printStackTrace(); } return cs; } public boolean add(Comment c){ Connection conn=null; PreparedStatement ps=null; String sql="insert into comments(uname,gid,comment) values(?,?,?)"; boolean flag=false; try{ Class.forName(Const.DRIVER); conn=DriverManager.getConnection(Const.URL, Const.USER,Const.PASSWORD); ps=conn.prepareStatement(sql); ps.setString(1,c.getUname()); ps.setInt(2, c.getGid()); ps.setString(3,c.getComment()); int n=ps.executeUpdate(); if(n>=1){ flag=true; } }catch(Exception e){ e.printStackTrace(); } //关闭链接 try{ if(ps!=null) ps.close(); if(conn!=null) conn.close(); }catch(Exception e){ e.printStackTrace(); } return flag; }2、实现删除评价和修改评价(主要语句)
public boolean delete(int id,String uname){
//删除某条评价,要求只有添加者可以删除
String sql="delete from comments where id=? and uname=?";
ps=conn.prepareStatement(sql);
ps.setInt(1,id); ps.setString(2,uname); int n=ps.executeUpdate();}
public boolean update(int id,String msg,String uname){
//修改某条评价信息,要求只有添加者可以修改
String sql="update comments set comment=? where id=? and uname=?";
ps=conn.prepareStatement(sql);
ps.setString(1,msg); ps.setInt(2, id); ps.setString(3,uname); int n=ps.executeUpdate();}
四、查看和发表某商品评价设计
1、detail.jsp页,添加“评价”超级链接<a href="comment.jsp?gid<%=g.getGid()%>">评价</a>
2、comment.jsp页面查看评价和发表评价
初始静态页面:comment.jsp
<html> <head> <title>发表评价页面</title> <link rel="stylesheet" type="text/css" href="css/style.css"> </head> <body> <div class="wrap"> <div class="icon_center"> <img src="p_w_picpaths/g1.png " style="width:200px;float:left" /> <ul> <li>编号:10002</li> <li>名称:小米 M4 </li> <li>描述:双卡双四核智能,5.0大屏</li> <li>价格:2117</li> <li>数量:20</li> </ul> <div class="clear"></div> </div> <div class="icon_center"> <ul class="left" > <li>评价者:admin </li> <li>评价时间:2013-10-1</li> <li style="width:200px">信息:ceshi</li> </ul> <hr> <ul class="left" > <li>评价者:admin </li> <li>评价时间:2013-10-1</li> <li style="width:200px">信息:发表留言</li> </ul> <hr> <ul class="left" > <li>评价者:admin </li> <li>评价时间:2013-10-1</li> <li style="width:200px">信息:发表留言2</li> </ul> <hr> <ul class="left" > <li>评价者:admin </li> <li>评价时间:2014-03-1</li> <li style="width:200px">信息:发表留言fkkffk</li> </ul> <hr> <ul class="left" > <li>评价者:无名氏 </li> <li>评价时间:2014-03-1</li> <li style="width:200px">信息:我测试</li> </ul> <hr> <div class="clear"></div> </div> <div class="icon_center"> <FORM METHOD=POST ACTION="/shop/CommentAddServlet"> <TEXTAREA NAME="content" ROWS="4" COLS="40">发表留言</TEXTAREA> <br/> <INPUT TYPE="hidden" NAME="gid" value="10002"> <INPUT TYPE="hidden" NAME="addUser" value="无名氏"> <INPUT TYPE="submit" value="发表"> </FORM> </div> </div> </body> </html>动态:comment.jsp
<%@ page language="java" import="java.util.*,mybean.*" pageEncoding="utf-8"%>
<%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><html> <head> <title>发表评价页面</title> <link rel="stylesheet" type="text/css" href="css/style.css"> </head> <body> <% int gid=Integer.parseInt(request.getParameter("gid")); GoodsDAO gdao=new GoodsDAO(); Goods g=gdao.getById(gid); %> <div class="wrap"> <div class="icon_center"> <img src="<%=g.getPicture() %>" style="width:200px;float:left" /> <ul> <li>编号:<%=g.getGid() %></li> <li>名称:<%=g.getName() %> </li> <li>价格:<%=g.getPrice() %></li> </ul> <div class="clear"></div> </div> <div class="icon_center"> <% CommentDAO cdao=new CommentDAO(); LinkedList<Comment>cs=cdao.searcher(gid); for(Comment c:cs){ %> <ul class="left" > <li>评价者:<%=c.getUname() %> </li> <li>评价时间:<%=c.getAdd_time() %></li> <li style="width:200px">评价:<%=c.getComment() %></li> </ul> <hr> <%} %> <div class="clear"></div> </div> <% User user=(User)session.getAttribute("user"); if(user!=null) { %> <div class="icon_center"> <FORM METHOD=POST ACTION="<%=basePath%>servlet/CommentServlet?type=add"> <TEXTAREA NAME="comment" ROWS="4" COLS="40">发表评价</TEXTAREA> <br/> <INPUT TYPE="hidden" NAME="gid" value="<%=gid%>"> <INPUT TYPE="hidden" NAME="uname" value="<%=user.getName()%>"> <INPUT TYPE="submit" value="发表"> </FORM> </div> <%} %> </div> </body> </html>3、控制器Servlet处理添加发表评价
servlet包下建立CommentServlet
package servlet;
import java.io.*;import javax.servlet.*;import javax.servlet.http.*;import mybean.*;public class CommentServlet extends HttpServlet { public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); String type=request.getParameter("type"); if("add".equals(type)){ CommentDAO cdao=new CommentDAO(); Comment c=new Comment(); int gid=Integer.parseInt(request.getParameter("gid")); String uname=request.getParameter("uname"); String comment=request.getParameter("comment"); System.out.println(comment); c.setGid(gid); c.setUname(uname); c.setComment(comment); cdao.add(c); //response.sendRedirect("../index.jsp"); request.getRequestDispatcher("../comment.jsp").forward(request,response); } }}解决:
(1)Servlet中gid传递到comment.jsp页的问题
request.getRequestDispatcher("../comment.jsp").forward(request,response);
(2)servlet-->comment.jsp添加评价发表时路径问题
原路径/shop/servlet/CommentServlet 转换为/shop/servlet/servlet/CommentServlet
解决方式,使用决定对路径访问
<FORM METHOD=POST ACTION="<%=basePath%>servlet/CommentServlet?type=add">
五、自主实现删除评价、用户修改留言