一、实现商品分类在前台主页显示
1、数据库添加typeGoods表,typeId char(2),typeName varchar(20)
2、Bean设计TypeGoods.java
package mybean;
public class TypeGoods {
private String typeId;
private String typeName;
public String getTypeId() {
return typeId;
}
public void setTypeId(String typeId) {
this.typeId = typeId;
}
public String getTypeName() {
return typeName;
}
public void setTypeName(String typeName) {
this.typeName = typeName;
}
}
3、TypeGoodsDAO类的list方法
package mybean;
import java.sql.*;
import java.util.LinkedList;
public class TypeGoodsDAO {
public LinkedList<TypeGoods> list()
{
Connection conn=null;
PreparedStatement ps=null;
ResultSet rs=null;
String sql="select * from typeGoods";
LinkedList<TypeGoods> ts=new LinkedList<TypeGoods>();
try{
Class.forName(Const.DRIVER);
conn=DriverManager.getConnection(Const.URL, Const.USER,Const.PASSWORD);
ps=conn.prepareStatement(sql);
rs=ps.executeQuery();
while(rs.next()){
TypeGoods t=new TypeGoods();
t.setTypeId(rs.getString("typeId"));
t.setTypeName(rs.getString("typeName"));
ts.add(t);
}
}catch(Exception e){
e.printStackTrace();
}
try {
if(rs!=null)
{rs.close();}
if(ps!=null)
{ps.close();}
if(conn!=null)
{conn.close();}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return ts;
}
}
说明:为实现通用化,将常用的变量,定义为常量存储于Const类中
package mybean;
public class Const {
public final static String URL="jdbc:sqlserver://localhost:1433;database=ebuy";
public final static String USER="shopping";
public final static String PASSWORD="shopping";
public final static String DRIVER="com.microsoft.sqlserver.jdbc.SQLServerDriver";
}
4、前台首页显示left.jsp部分代码
<ul class="list_con">
<%
TypeGoodsDAO tdao=new TypeGoodsDAO();
LinkedList<TypeGoods> ts=tdao.list();
for(TypeGoods t:ts)
{
%>
<li><a href="index.jsp?no=1&type=<%=t.getTypeId()%>"><%=t.getTypeName() %> </a></li>
<%} %>
</ul>
5、菜单栏显示商品类别
二、商品分类显示
1、GoodsDAO中添加search方法
Class.forName(Const.DRIVER);
conn=DriverManager.getConnection(Const.URL, Const.USER, Const.PASSWORD);
ps=conn.prepareStatement(sql);
ps.setString(1,type);//1
rs=ps.executeQuery();
while(rs.next()){
Goods g=new Goods();
g.setGid(rs.getInt("gid"));
g.setName(rs.getString("name"));
g.setPrice(rs.getFloat("price"));
g.setNum(rs.getInt("num"));
g.setContent(rs.getString("content"));
g.setPicture(rs.getString("picture"));
gs.add(g);
}
} 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) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return gs;
}
2、right页确定是显示部分商品还是显示全部商品
<% String no=request.getParameter("no");
String type=request.getParameter("type");
String txt="";
if(no==null||"1".equals(no))
{
GoodsDAO dao=new GoodsDAO();
LinkedList<Goods> gs=null;
if(no==null){
txt="全部商品";
gs=dao.list();
}else if(no.equals("1")){
System.out.println(type);
gs=dao.search(type);
txt="部分商品";
}
%>
<div class="current">
<p>当前位置:首页><%=txt %></p>
</div>
<div class="right_down">
<%
for(Goods g:gs){
%>
<div class="post">
<a href="detail.jsp?gid=<%=g.getGid()%>"><img src=\‘#\‘" /></a>
<ul>
<li>名称:<a href="detail.jsp?gid=<%=g.getGid()%>"><%=g.getName() %></a></li>
<li>价格:<%=g.getPrice() %></li>
<li>库存数量:<%=g.getNum() %>
</ul>
</div>
<%}
}%>
</div>
3、显示商品详细信息detail.jsp
<%@ page language="java" import="java.util.*,mybean.*" pageEncoding="UTF-8"%>
<%
String gid=request.getParameter("gid");
GoodsDAO dao=new GoodsDAO();
Goods g=dao.getById(Integer.parseInt(gid));
%>
<div class="detail">
<form method="GET" action="doCar.jsp">
<img src="images/product.jpg">
<ul>
<li>编号:<%=g.getGid() %></li>
<li>名称:<%=g.getName() %></li>
<li>价格:<%=g.getPrice() %></li>
<li><a href="comment.jsp?gid=<%=g.getGid()%>">查看评价</a></li>
<li>数量:<input name="num" size="4" type="text">
<input value="购买" type="submit"></li>
<input name="gid" value="<%=g.getGid()%>" type="hidden">
<input name="action" value="buy" type="hidden">
</ul>
</form>
</div>
<div class="describe">商品描述:<%=g.getContent()%></div>
说明:我们可以看到详细信息是在独立页显示的,尝试实现在首页的右侧显示
4、实现详细信息在右侧显示
right页更改
<%@ page language="java" import="java.util.*,mybean.*" pageEncoding="UTF-8"%>
<% String no=request.getParameter("no");
String type=request.getParameter("type");
String gid=request.getParameter("gid");
String txt="";
if(no==null||"1".equals(no))
{
GoodsDAO dao=new GoodsDAO();
LinkedList<Goods> gs=null;
if(no==null){
txt="全部商品";
gs=dao.list();
}else if(no.equals("1")){
System.out.println(type);
gs=dao.search(type);
txt="部分商品";
}
%>
<div class="current">
<p>当前位置:首页><%=txt %></p>
</div>
<div class="right_down">
<%
for(Goods g:gs){
%>
<div class="post">
<a href="index.jsp?no=detail&gid=<%=g.getGid()%>"><img src=\‘#\‘" /></a>
<ul>
<li>名称:<a href="index.jsp?no=detail&gid=<%=g.getGid()%>"><%=g.getName() %></a></li>
<li>价格:<%=g.getPrice() %></li>
<li>库存数量:<%=g.getNum() %>
</ul>
</div>
<%}
}
if("detail".equals(no)){
txt="详细信息";
%>
<jsp:include page="detail.jsp">
<jsp:param value="<%=gid%>" name="gid"/>
</jsp:include>
<%
}
%>
</div>
<div class="clear"></div>