1、原理css
用戶查看上,點擊查看評價,可以看到該商品的評價html
根據評價信息,咱們肯定comments包含id,gid,uname,comment,add_time(評價id,商品id,發表者,評價信息,添加時間)java
一、數據庫創建comments表sql
SQL語句複習
數據庫
insert into comments(gid,uname,comment)values(1001,'www','hello')
select * from comments
delete from comments where id=1 and uname='www'session
insert into comments(gid,uname,comment)values(1001,'www','hello')jsp
insert into comments(gid,uname,comment)values(1001,'www','test')ide
二、設計Comment.java(Bean)測試
三、設計商品評價DAO(commentDAO.java)this
(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) //刪除評價,能夠考慮只有發表評價用戶能夠刪除
2、評價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;
}
}
3、評價DAO設計
一、實現查看評價和添加評價
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;
}
二、實現刪除評價和修改評價(主要語句)
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();
}
4、查看和發表某商品評價設計
一、detail.jsp頁,添加「評價」超級連接<a href="comment.jsp?gid<%=g.getGid()%>">評價</a>
二、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>
三、控制器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">
5、自主實現刪除評價、用戶修改留言