实现一:
大前提:商城只卖四种商品(book,clothes,electric,snack),要在首页上分四行分别显示这四种商品;
先上一个效果图:
实现类:
Product.java
BooksDaoImpl.java(接口BooksDao.java)
ClothesDaoImpl.java(接口ClothesDao.java)
ElectricDaoImpl.java(接口ElectricDao.java)
SnacksDaoImpl.java(接口SnacksDao.java)
IndexProductsServlet.java
index.jsp
代码、相关功能实现及描述如下:
Product.java
【一个java bean,存放商品属性以及getter、setter方法】
import java.io.Serializable;
public class Product implements Serializable {
private static final long serialVersionUID = 1L;
private Integer id;
private String name;
private String title;
private double price;
private String imgURL;
private String manufacture;
private Integer counts;
private double countBack;
private double cashBack;
public double getCashBack() {
return cashBack;
}
public void setCashBack(double cashBack) {
this.cashBack = cashBack;
}
public double getCountBack() {
return countBack;
}
public void setCountBack(double countBack) {
this.countBack = countBack;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public String getImgURL() {
return imgURL;
}
public void setImgURL(String imgURL) {
this.imgURL = imgURL;
}
public String getManufacture() {
return manufacture;
}
public void setManufacture(String manufacture) {
this.manufacture = manufacture;
}
public Integer getCounts() {
return counts;
}
public void setCounts(Integer counts) {
this.counts = counts;
}
}
BooksDaoImpl.java
(接口BooksDao.java)【操作数据库,在该项目中是查询商品ClothesDaoImpl.java、ElectricDaoImpl.java、SnacksDaoImpl.java类似,代码略】
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.ymw.domain.PageBean;
import com.ymw.domain.Product;
import com.ymw.domain.User;
import com.ymw.exception.DataBaseException;
import com.ymw.util.DBUtil;
/** * 执行数据查询 * *在selectAllBooks方法中查询所有商品; * */
public class BooksDaoImpl implements BooksDao {
@Override
public List<Product> selectAllBooks() {
List<Product> books = new ArrayList<Product>();
Connection connection = DBUtil.getConnection();
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try {
String sql = "select * from books";
preparedStatement = connection.prepareStatement(sql);
resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
Product book = new Product();
book.setCounts(resultSet.getInt("counts"));
book.setId(resultSet.getInt("id"));
book.setImgURL(resultSet.getString("imgURL"));
book.setManufacture(resultSet.getString("manufacture"));
book.setName(resultSet.getString("name"));
book.setPrice(resultSet.getDouble("price"));
book.setTitle(resultSet.getString("title"));
books.add(book);
}
} catch (SQLException e) {
throw new DataBaseException();
} catch (Exception e) {
e.printStackTrace();
} finally {
}
return books;
}
}
IndexProductsServlet.java
【连接数据库操作和显示】
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.ymw.exception.DataBaseException;
import com.ymw.dao.BooksDao;
import com.ymw.dao.BooksDaoImpl;
import com.ymw.dao.ClothesDao;
import com.ymw.dao.ClothesDaoImpl;
import com.ymw.dao.ElectricDao;
import com.ymw.dao.ElectricDaoImpl;
import com.ymw.dao.SnacksDao;
import com.ymw.dao.SnacksDaoImpl;
import com.ymw.domain.Product;
/** * * 调用*DaoImpl中的方法查询回所有的商品,然后进行页面跳转 * */
public class IndexProductsServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
BooksDao bookDao = new BooksDaoImpl();
ClothesDao clothesDao = new ClothesDaoImpl();
ElectricDao electricDao = new ElectricDaoImpl();
SnacksDao snacksDao = new SnacksDaoImpl();
try {
List<Product> books = bookDao.selectAllBooks();
List<Product> clothes = clothesDao.selectAllClothes();
List<Product> electric = electricDao.selectAllElectric();
List<Product> snacks = snacksDao.selectAllSnacks();
request.setAttribute("books", books);
request.setAttribute("clothes", clothes);
request.setAttribute("electric", electric);
request.setAttribute("snacks", snacks);
request.getRequestDispatcher("index.jsp")
.forward(request, response);
} catch (DataBaseException e) {
e.printStackTrace();
request.getRequestDispatcher("dataBase.jsp").forward(request,
response);
} catch (Exception e) {
e.printStackTrace();
request.getRequestDispatcher("error.jsp")
.forward(request, response);
}
}
}
index.jsp
(主要代码)【在该jsp中使用了(c:forEach)的几个属性展示出每种id从1到6的商品,这种方法有点死板,在这个功能上可以生成相应范围内的六个随机数来作为要展示的每种商品的Id】
<% List<Product> books = (List<Product>) request.getAttribute("books"); List<Product> clothes = (List<Product>) request.getAttribute("clothes"); List<Product> electric = (List<Product>) request.getAttribute("electric"); List<Product> snacks = (List<Product>) request.getAttribute("snacks"); if (books != null || clothes != null || electric != null || snacks != null) { %>
<div class="main">
<div class="hot">
<h2>今日特价</h2>
<ul class="product clearfix">
<c:forEach var="book" items="${requestScope.books}" begin="1" end="6" step="1">
<li>
<dl>
<dt>
<a href="findOneBook.do?id=${book.id}&aaa=1" target="_blank"><img src="${book.imgURL}" /></a>
</dt>
<dd class="title">
<a href="findOneBook.do?id=${book.id}&aaa=1" target="_blank">${book.title}</a>
</dd>
<dd class="price">${book.price}</dd>
</dl>
</li>
</c:forEach>
<c:forEach var="cloth" items="${requestScope.clothes}" begin="1" end="6" step="1">
<li>
<dl>
<dt>
<a href="findOneClothes.do?id=${cloth.id}&aaa=1" target="_blank"><img src="${cloth.imgURL}" /></a>
</dt>
<dd class="title">
<a href="findOneClothes.do?id=${cloth.id}&aaa=1" target="_blank">${cloth.title}</a>
</dd>
<dd class="price">${cloth.price}</dd>
</dl>
</li>
</c:forEach>
</ul>
</div>
<div class="spacer clear"></div>
<div class="hot">
<h2>热卖推荐</h2>
<ul class="product clearfix">
<c:forEach var="e" items="${requestScope.electric}" begin="1" end="6" step="1">
<li>
<dl>
<dt>
<a href="findOneElectric.do?id=${e.id}&aaa=1" target="_blank"><img src="${e.imgURL}" /></a>
</dt>
<dd class="title">
<a href="findOneElectric.do?id=${e.id}&aaa=1" target="_blank">${e.title}</a>
</dd>
<dd class="price">${e.price}</dd>
</dl>
</li>
</c:forEach>
<c:forEach var="snack" items="${requestScope.snacks}" begin="1" end="6" step="1">
<li>
<dl>
<dt>
<a href="findOneSnack.do?id=${snack.id}&aaa=1" target="_blank"><img src="${snack.imgURL}" /></a>
</dt>
<dd class="title">
<a href="findOneSnack.do?id=${snack.id}&aaa=1" target="_blank">${snack.title}</a>
</dd>
<dd class="price">${snack.price}</dd>
</dl>
</li>
</c:forEach>
</ul>
</div>
</div>
<%} else { response.sendRedirect("indexProducts.do"); } %>
在接下来的博客中会继续更新其它形式的首页实现,如:商品图片自动跳转、显示最近浏览的商品等。