1.功能介绍
前面我们实现了商品所有信息的显示,接下来要实现某一商品详细信息的显示。那么我们就要思考了,我们如何通过页面跳转将我们对应的商品传递到下一个页面。其实在前面写index.jsp主页面的时候,用户点击对应的商品会跳转到details.jsp这个页面,我们可以在这里的<a>标签中将我们商品的id传进去:
<a href="details.jsp?id=<%=good.getId() %>" class="prod_details">详情</a>
这样我们就可以显示该条商品的详细信息了。
ok,商品的id有了,接下来我们就是根据id从数据库中获取该商品的信息。
2.获取单条数据
和前面一样,对数据库的操作,我们使用GoodDao这个类实现,接下来是获取单条记录的详细实现:
//根据商品的id获取数据
public Good getGoodById(int id){
Connection conn=null;
PreparedStatement prep=null;
ResultSet rs=null;
Good good=new Good();
try {
conn=DBHelper.getConnection();
String sql="select * from good where id=?";
prep=(PreparedStatement) conn.prepareStatement(sql);
prep.setInt(1, id);
rs=prep.executeQuery();
if(rs.next())
{
good.setId(rs.getInt("id"));
good.setName(rs.getString("name"));
good.setPrice(rs.getDouble("price"));
good.setInfor(rs.getString("infor"));
good.setImage(rs.getString("image"));
}
return good;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
3.详细页面的实现
ok,这样我们就可以通过这个方法获取一条记录,接下来我们要做的就是将数据显示出来。仍然和前面一样,我们的details.jsp页面很多,现贴出关键代码:
<!-- 找到你啦,我们要用的东西在这里 -->
<div class="center_content">
<%
//根据地址里面的id参数,获取到数据库中的商品
GoodDao dao=new GoodDao();
Good good=new Good();
good=dao.getGoodById(Integer.parseInt(request.getParameter("id")));
%>
<!-- 商品详细信息处 -->
<div class="center_title_bar"><%=good.getName() %></div>
<div class="prod_box_big">
<div class="top_prod_box_big"></div>
<div class="center_prod_box_big">
<div class="product_img_big">
<a href="javascript:popImage('images/big_pic.jpg','Some Title')" title="header=[Zoom] body=[ ] fade=[on]"><img src="images/<%=good.getImage() %>" alt="" title="" border="0" /></a>
</div>
<div class="details_big_box">
<div class="product_title_big"><%=good.getName() %></div>
<div class="specifications">
介绍: <span class="blue"><%=good.getInfor() %></span><br />
</div>
<div class="prod_price_big"><span class="price">价格:<%=good.getPrice() %></span></div>
<a href="#" class="addtocart">加入购物车</a>
</div>
</div>
<div class="bottom_prod_box_big"></div>
</div>
</div>
4.页面效果
好啦,功能和页面都写好了,我们来看一看点击商品“大哥大”的效果:
这里1处是我们商品的详细信息,通过2处我们可以发现,页面跳转的时候传过来的id=5,<a>传递参数是get方式传递的。
好,这两个页面的功能都实现了,接下来要进入到本工程我们关键的地方了:使用cookie显示最近浏览商品的前5条信息。