<%@ page language="java" import="java.util.*,yjy.bean.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>购物网</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<style type="text/css">
div{
border:1px solid grey;
}
.all{
width:1024px;
margin:0px auto;
}
.top{
height: 40px;
line-height: 40px;
}
.top a{
text-decoration: none;
display:inline-block;
width: 60px;
}
.header{
}
form{
display:inline-block;
}
.header input{
height:40px;
vertical-align: bottom;
}
input[type="text"]{
width:300px
}
.nav{
height: 50px;
}
ul {
list-style-type: none;
margin: 0;
padding: 0;
overflow: hidden;
background-color: red;
}
li {
float: left;
}
li a {
display:inline-block;
color: white;
text-align: center;
padding: 14px 16px;
text-decoration: none;
}
li a:hover {
background-color: #111;
}
.main{
}
.mainList{
display:flex;
flex-wrap:wrap;
}
ul.pagination {
display: inline-block;
padding: 0;
margin: 0;
}
ul.pagination li {display: inline;}
ul.pagination li a {
color: black;
float: left;
padding: 8px 16px;
text-decoration: none;
}
ul.pagination li a.active {
background-color: red;
color: white;
}
ul.pagination li a:hover:not(.active) {background-color: #ddd;}
.page{
display:flex;
justify-content:flex-end;
}
.footer{
height: 50px;
}
.other{
display:flex;
justify-content:space-around;
}
</style>
</head>
<body>
<div class="all">
<div class="top" >
<a href="login.jsp">登录</a>
<a href="register.jsp">注册</a>
</div>
<div class="header" >
<img alt="" src="image/home/logo.png">
<form action="">
<input type="text">
<input type="image" src="image/home/search.png">
</form>
<img alt="" src="image/home/cart.png">
</div>
<div class="nav" >
<ul>
<li><a class="active" href="#home">主页</a></li>
<li><a href="#news">新闻</a></li>
<li><a href="#contact">联系</a></li>
<li><a href="#about">关于</a></li>
</ul>
</div>
<div class="main" >
<div class="mainList">
<%
//HomeData homeData=(HomeData)request.getAttribute("homeData");
HomeData homeData=(HomeData)request.getAttribute("homeData");
for(int i=0;i<homeData.getGoodList().size();i++){
Goods goods=homeData.getGoodList().get(i);
%>
<div class="item">
<img alt="" src="<%=goods.getImgpath() %>">
<div class="introduce">
<%=goods.getInfo() %>
</div>
<div class="other">
<%=goods.getPrice() %> <button>加入购物车</button>
</div>
</div>
<%} %>
</div>
<div class="page">
<ul class="pagination">
<li><a href="#">«</a></li>
<li><a href="#">1</a></li>
<li><a class="active" href="#">2</a></li>
<li><a href="#">3</a></li>
<li><a href="#">4</a></li>
<li><a href="#">»</a></li>
</ul>
</div>
</div>
<div class="footer" >footer</div>
</div>
</body>
</html>
package yjy.bean;
import java.util.*;
public class HomeData {
public int numPerPage=8;
private int curPage;
private int kind;
private String keyword;
private int total;
private ArrayList<Goods> goodList;
public HomeData(int curPage, int kind, String keyword) {
super();
this.curPage = curPage;
this.kind = kind;
this.keyword = keyword;
}
public HomeData() {
super();
}
public int getNumPerPage() {
return numPerPage;
}
public void setNumPerPage(int numPerPage) {
this.numPerPage = numPerPage;
}
public int getCurPage() {
return curPage;
}
public void setCurPage(int curPage) {
this.curPage = curPage;
}
public int getKind() {
return kind;
}
public void setKind(int kind) {
this.kind = kind;
}
public String getKeyword() {
return keyword;
}
public void setKeyword(String keyword) {
this.keyword = keyword;
}
public int getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public ArrayList<Goods> getGoodList() {
return goodList;
}
public void setGoodList(ArrayList<Goods> goodList) {
this.goodList = goodList;
}
}
package yjy.controller;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import yjy.bean.HomeData;
import yjy.service.GoodsService;
public class HomeController extends HttpServlet {
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//设置默认值
int curPage=1;
int kind=0;
String keyword=null;
//设置获取参数
//数据封装
HomeData homeData=new HomeData(curPage,kind,keyword);
//调用业务逻辑处理数据
GoodsService.prepareForHome(homeData);
request.setAttribute("homeData", homeData);
request.getRequestDispatcher("homepage.jsp").forward(request, response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
}
package yjy.service;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.print.attribute.standard.PagesPerMinuteColor;
import Test.DBHelp;
import yjy.bean.Goods;
import yjy.bean.HomeData;
public class GoodsService {
public static void prepareForHome(HomeData homeData){
Connection conn=DBHelp.getConn();
int curPge=homeData.getCurPage();
int kind=homeData.getKind();
//int numPerPage=homeData.numPerPage;
int page=homeData.numPerPage;
String sql=null;
if(kind==0){
sql="select top "+page+ " * from goods where id not in(select top "+(curPge-1)*page+" id from goods)";
System.out.println("sql="+sql);
}else{
sql="select top "+page+ " * from goods where kind="+kind+" id not in(select top "+(curPge-1)*page+" id from goods kind="+kind+")";
System.out.println("sql="+sql);
}
ArrayList<Goods> arr=new ArrayList<Goods>();
PreparedStatement ps;
try {
ps = conn.prepareStatement(sql);
ResultSet rs=ps.executeQuery();
while(rs.next()){
int id = rs.getInt("id");
String name = rs.getString("name");
String info = rs.getString("info");
String imgpath = rs.getString("imgpath");
int sort = rs.getInt("kind");
BigDecimal price = rs.getBigDecimal("price");
int num = rs.getInt("stock");
Goods goods = new Goods(id,name,info,imgpath,sort,price,num);
arr.add(goods);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
homeData.setGoodList(arr);
}
}