图片 2

增删改查,Java实现分页显示数据

By admin in 编程 on 2019年5月21日

JDBC案例
明日内容介绍
案例:删除选中商品
案例:条件查询
案例:分页查询

 

(以下使用ssm+jsp完毕展现全数电影为例子)

 

时间:2016-12-11 01:41

一.封装三个PageBean类

package com.entity;import java.util.List;public class PageBean{

//已知多少    

private int pageNum;    //当前页,从呼吁这边传过来。    

private int pageSize;    //每页突显的数码条数。    

private int totalRecord;    //总的笔录条数。查询数据库获得的多寡     
  

//必要总结得来    

private int totalPage;
   //总页数,通过totalRecord和pageSize总计能够得来    

//初始索引,相当于大家在数据库中要从第几行数据初叶拿,有了startIndex和pageSize, 
 

 //就知道了limit语句的八个数据,就会获得每页需求出示的数据了    

private int startIndex;                        

//将每页要显得的数目放在list集合中    

private List list;       

 //分页展现的页数,例如在页面上呈现一,二,三,四,伍页,start就为一,end就为5,那个也是算过来的
  

private int start;    

private int end;        

//通过pageNum,pageSize,totalRecord总计得来tatalPage和startIndex   

 //构造方法少将pageNum,pageSize,totalRecord获得    

public PageBean(int pageNum,int pageSize,int totalRecord) {

       this.pageNum = pageNum;

       this.pageSize = pageSize;

       this.totalRecord = totalRecord;

        //totalPage 总页数        

        if(totalRecord%pageSize==0){

         
 //表达整除,正好每页呈现pageSize条数据,未有剩余一页要来得少于pageSize条数据的 

          this.totalPage = totalRecord / pageSize;

       }else{

          //不整除,就要在加1页,来呈现多余的数量。

            this.totalPage = totalRecord / pageSize +1;

        }

       //初步索引

        this.startIndex = (pageNum-1)*pageSize ;

        //展现十页,这里协和能够安装,想体现几页就自身通过下边算法修改

        this.start = 1;

        this.end = 10;

        //彰显页数的算法

        if(totalPage <=10){

            //总页数都自愧不及拾,那么end就为总页数的值了。

            this.end = this.totalPage; 

         }else{ 

          
//总页数大于拾,那么将要依照方今是第几页,来判别start和end为多少了,

            this.start = pageNum – 4;

            this.end = pageNum + 5;

            if(start < 1){ 

                 //前肆页不适合那一个规则,

                 this.start = 1;

                 this.end = 10; 

             }

            if(end > this.totalPage){

               
//比如当前页是倒数第叁页或然最终壹页,也深闭固拒不符合地点这些规则

                this.end = totalPage; 

               this.start = end – 10; 

           }

        }

    }

//getting,setting

public int getPageNum() {return pageNum;}

public void setPageNum(int pageNum) {this.pageNum = pageNum;}

public int getPageSize() {return pageSize;}

public void setPageSize(int pageSize) {this.pageSize = pageSize;}

public int getTotalRecord() {return totalRecord;}

public void setTotalRecord(int totalRecord) {this.totalRecord =
totalRecord;}

public int getTotalPage() {return totalPage;}

public void setTotalPage(int totalPage) {this.totalPage = totalPage;}

public int getStartIndex() {return startIndex;}

public void setStartIndex(int startIndex) {this.startIndex =
startIndex;}

public ListgetList() {return list;}

public void setList(Listlist) {this.list = list;}

public int getStart() {return start;}

public void setStart(int start) {this.start = start;}

public int getEnd() {return end;}

public void setEnd(int end) {this.end = end;}

}

前几日内容上学指标
删除选中商品
条件查询
分页查询

 

贰.mybatis.xml文件(数据库操作)

//获取具备电影

<select id=”getAllMovie” resultType=”com.entity.Movie”>

    select * from movie

</select>

//获取分页电影

<select id=”getPageMovie”
parameterType=”com.entity.SelectCondition”
resultType=”com.entity.Movie”>

    select * from movie limit #{startIndex},#{pageSize}

</select>

第2章案例:删除选中商品

 

3.DAO层

//获取具备电影

public ListgetAllMovies() throws IOException{

List list=sqlSession.selectList(“getAllMovie”);

return list;

}

//  获取分页电影

//SelectCondition为一个类,用于封装多少个查询条件,这里带有startIndex、pageSize多少个规范

public ListgetPageMovie(SelectCondition selectCondition) throws
IOException{

List list= sqlSession.selectList(“getPageMovie”,selectCondition);

return list;

}

壹.1案例深入分析

一、分页的帮助和益处:
    只询问壹页,没有须要查询全数数据,能够进步成效。

4.servier层

public PageBean getPageMovie(int pageNum,int pageSize) throws
IOException{

//pageNum为当前页码

List alllist=movieDao.getAllMovies();//获取具备电影

int totalRecord=alllist.size();//获得电影总的数量

PageBean pb = new PageBean(pageNum, pageSize, totalRecord);

int startIndex=pb.getStartIndex();

SelectCondition selectCondition=new SelectCondition();

selectCondition.setStartIndex(startIndex);

selectCondition.setPageSize(pageSize);

List pageList=movieDao.getPageMovie(selectCondition);//当页电影信息

pb.setList(pageList);

return pb;

}

一.二案例达成
步骤1:修改jsp,完善checkbox,name为pid,value为商品id

二、分页数据
    页面包车型客车数目都以由Servlet传递的

5.Controller

public String getAllMovie(HttpServletRequest request,

@RequestParam(value=”pageNum”,defaultValue=”1″) int pageNum) throws
IOException{

int pageSize=10;//设置一页展现电影数

PageBean pb=movieService.getPageMovie(pageNum, pageSize);

request.setAttribute(“pageBean”, pb);

return “ShowAllMovie”;

}

<input type=”checkbox” name=”pid” value=”${product.pid}” />
步骤二:编写js代码,获得全部的pid
加多id属性:<a id=”deleteAllId”
href=”javascript:void(0)”>删除选中</a>
<script type=”text/javascript”>
$(function(){
//绑定点击事件
$(“#deleteAllId”).click(function(){
//全部入选的checkbox
var all = $(“input[name=’pid’]:checked”);
if(all.length == 0){
alert(“请接纳必要上传的货品”);
return false;
}
//
if(window.confirm(“您明显要删减所选”+all.length+”个商品呢?”)){
//获得全部的id串
//注:serialize()为jquery的方法 将成分类别化成 id=xx&id=yy的格式
var ids = all.serialize();
location.href=”${pageContext.request.contextPath}/productDeleteServlet?”

    *   当前页:pageCode
        >  
假如页面未有向Servlet传递页码,那么Servlet默以为第贰页,不然根据传递页码为准。
    *   总页数:totalPages
        >   总记录数 / 每页记录数
    *   总记录数:totalRecord
        >   Dao来获取,select count(*) from customer
    *   每页记录数:称为业务数据或体系数据。
    *   当前页数据:beanList
    *   URL

6.jsp页面

<–%– 构建分页导航 –%–>

共有${requestScope.pageBean.totalRecord}部电影,共${requestScope.pageBean.totalPage
}页

<br/>

<a
href=”${pageContext.request.contextPath}/movie/getallmovie.htm?pageNum=1″>首页</a>

<–%–倘若当前页为率先页时,就未有上1页那些超链接显示 –%–>

<c:if test=”${requestScope.pageBean.pageNum ==1}”>

    <c:forEach begin=”${requestScope.pageBean.start}”
end=”${requestScope.pageBean.end}” step=”1″ var=”i”>

        <c:if test=”${requestScope.pageBean.pageNum ==
i}”>${i}</c:if>

        <c:if test=”${requestScope.pageBean.pageNum != i}”>

            <a
href=”${pageContext.request.contextPath}/movie/getallmovie.htm?pageNum=${i}”>${i}</a>

        </c:if>

</c:forEach>

    <a
href=”${pageContext.request.contextPath}/movie/getallmovie.htm?pageNum=${requestScope.pageBean.pageNum+1}”>下一页</a>

</c:if>

<–%–固然当前页不是率先页也不是终极1页,则有上壹页和下一页这一个超链接展现–%–>

<c:if test=”${requestScope.pageBean.pageNum > 1 &&
requestScope.pageBean.pageNum
<requestScope.pageBean.totalPage}”>

    <a
href=”${pageContext.request.contextPath}/movie/getallmovie.htm?pageNum=${requestScope.pageBean.pageNum-1}”>上一页</a>

    <c:forEach begin=”${requestScope.pageBean.start}”
end=”${requestScope.pageBean.end}” step=”1″ var=”i”>

        <c:if test=”${requestScope.pageBean.pageNum ==
i}”>${i}</c:if>

        <c:if test=”${requestScope.pageBean.pageNum != i}”>

            <a
href=”${pageContext.request.contextPath}/movie/getallmovie.htm?pageNum=${i}”>${i}</a>

        </c:if>

    </c:forEach>

    <a
href=”${pageContext.request.contextPath}/movie/getallmovie.htm?pageNum=${requestScope.pageBean.pageNum+1}”>下一页</a>

</c:if>

<–%– 要是当前页是最后1页,则只有上一页这一个超链接呈现,下1页没有–%–>

<c:if test=”${requestScope.pageBean.pageNum ==
requestScope.pageBean.totalPage}”>

    <a
href=”${pageContext.request.contextPath}/movie/getallmovie.htm?pageNum=${requestScope.pageBean.pageNum-1}”>上一页</a>

    <c:forEach begin=”${requestScope.pageBean.start}”
end=”${requestScope.pageBean.end}” step=”1″ var=”i”>

        <c:if test=”${requestScope.pageBean.pageNum ==
i}”>${i}</c:if>

        <c:if test=”${requestScope.pageBean.pageNum != i}”>

            <a
href=”${pageContext.request.contextPath}/movie/getallmovie.htm?pageNum=${i}”>${i}</a>

        </c:if>

    </c:forEach>

</c:if>

<–%–尾页 –%–>

<a
href=”${pageContext.request.contextPath}/movie/getallmovie.htm?pageNum=${requestScope.pageBean.totalPage}”>尾页</a>

  • ids;
    } else {
    $(“input[name=’pid’]”).removeProp(“checked”);
    }
    });
    });
    </script>

三、数据的传递
   
那几个分页数据总要在各层之间往来传递,能够把这一个分页数据封装到2个JavaBean中,它就叫分页Bean,举个例子:PageBean。

步骤3:编写servlet
public class ProductDeleteAllServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

import java.util.List;

public void doGet(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
//一 获得全部的id
String[] pids = request.getParameterValues(“pid”);
//2 删除
ProductService productService = new ProductService();
productService.deleteAll(pids);

 

//三重定向查询全数
response.sendRedirect(request.getContextPath() +
“/productFindAllServlet”);
}

public class PageBean<T> {

步骤4:编写service
/**
* 删除全体
* @param pids
*/
public void deleteAll(String… pids) {
productDao.delete(pids);
}

    // 当前页码pageCode

步骤5:编写dao
/**
* 删除全体
* @param pids
*/
public void deleteAll(String… pids) {
try {
QueryRunner queryRunner = new QueryRunner(C3P0Utils.getDataSource());
//删除sql语句
String sql = “delete from product where pid = ?”;
//将有所的pid拼凑3个贰维数组,列表示id,行表示个数
Object[][] params = new Object[pids.length][];
for (int i = 0; i < pids.length; i++) {
params[i] = new Object[]{pids[i]};
}
queryRunner.batch(sql, params);
} catch (Exception e) {
throw new RuntimeException(e);
}
}

    private int pageCode;

第三章案例:分页查询
二.一案例介绍
实在支出中,条件查询时十一分常见的壹种功效,上2个案例中大家曾经做到。接着大家再来学习另一种也要命普及的法力:分页查询。

 

2.二有关文化:sql分页
格式:select * from 表 limit ?,?;
参数1:开端索引 startIndex ,私下认可值:0。必须是正数。
参数贰:每页突显个数 pageSize
例如:
select * from products limit 0,5; #首先页,每页展现五条。
select * from products limit 5,5; #第二页,每页展现伍条。
select * from products limit 10,5; #其三页,每页展现五条。
select * from products limit ?,5; #第pageNumber页,每页呈现5条。
startIndex = (pageNumber – 1) * pageSize;

    /*

二.三案例深入分析

     * 总页数

二.四案例完毕
步骤1:编写PageBean,提供构造方法,统壹算法的编写制定。

     * 通过测算得出,不容许外界设置值

public class PageBean<T> {
private int pageNumber; //当前页(浏览器传递)
private int pageSize; //每页突显个数(固定值,也能够是浏览器传递)
private int totalRecord; //总记录数(数据库查询)
private int totalPage; //总分页数
private int startIndex; //初叶索引
private List<T> data; //分页数据(数据库查询)

     * 因为只可以get,所以无需成员变量,通过总计就可以得出

/**
* 为了约束使用者,必须传递三个参数,不提供无参构造
* @param pageNumber
* @param pageSize
*/
public PageBean(int pageNumber, int pageSize) {
super();
this.pageNumber = pageNumber;
this.pageSize = pageSize;

     */

}
public int getStartIndex() {
//开首索引计算
startIndex = (pageNumber – 1) * pageSize;
return startIndex;
}
public int getTotalPage() {
//总页数总括
if(totalRecord % pageSize == 0){
//整除
totalPage = totalRecord / pageSize ;
} else {
//有余数 + 1 (半页)
totalPage = totalRecord / pageSize + 1;
}

    // private int totalPages;

return totalPage;
}
//其余getter和setter方法省略

 

步骤二:显著入口

    // 总记录数

步骤三:编写servlet,得到参数
public class ProductFindAllServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
//一 得到分页参数
String pageNumberStr = request.getParameter(“pageNumber”);
Integer pageNumber = 1 ;//管理没有传递参数的动静
try {
pageNumber = Integer.parseInt(pageNumberStr);
} catch (Exception e) {
}
Integer pageSize = 2;
//固定值,Integer.parseInt(request.getParameter(“pageSize”));

    private int totalRecord;

//二 通告service,查询全部商品
ProductService productService = new ProductService();
PageBean<Product> pageBean =
productService.findAll(pageNumber,pageSize);

 

//3 选择jsp
//三.一 将查询结果存放在request成效域
request.setAttribute(“pageBean”, pageBean);
//叁.二 请求转载
request.getRequestDispatcher(“/admin/product/product_list.jsp”)
.forward(request, response);
}

    // 每页记录数

步骤四:编写service,进行分页查询,将享有的数目封装到PageBean中。
/**
* 查询全数,分页
* @return
*/
public PageBean<Product> findAll(int pageNumber , int pageSize){
//壹 总分页数、开首索引,已经在PageBean中管理了
PageBean<Product> pageBean = new
PageBean<Product>(pageNumber, pageSize);

    private int pageSize;

//二 查询总记录数
int totalRecord = productDao.getTotalRecord();
//将总记录数封装到pageBean中
pageBean.setTotalRecord(totalRecord );

 

//3 查询分页数据
List<Product> data =
productDao.findAll(pageBean.getStartIndex(),pageBean.getPageSize());
// * 将分页数据封装到PageBean中
pageBean.setData(data);

    // 当前页的笔录

//四 再次回到封装好的多少
return pageBean;
}

    private List<T> beanList;

步骤伍:编写dao,查询总记录数和分页数据
/**
* 查询总记录数
* @return
*/
public int getTotalRecord() {
try {
QueryRunner queryRunner = new QueryRunner(C3P0Utils.getDataSource());
String sql = “select count(*) from product”;
Object[] params = {};
Long numLong = (Long) queryRunner.query(sql, new ScalarHandler(),
params);
return numLong.intValue();
} catch (Exception e) {
throw new RuntimeException(e);
}
}

 

/**
* 分页查询全体
* @return
*/
public List<Product> findAll(int startIndex, int pageSize) {
try {
QueryRunner queryRunner = new QueryRunner(C3P0Utils.getDataSource());
String sql = “select * from product limit ?,?”;
Object[] params = {startIndex ,pageSize};
return queryRunner.query(sql, new
BeanListHandler<Product>(Product.class), params);
} catch (Exception e) {
throw new RuntimeException(e);
}
}

    public int getPageCode() {

步骤陆:修改jsp页面,显示分页数据
位置:/day14_page/WebContent/admin/product/product_list.jsp

        return pageCode;

步骤7:展现分页音信

    }

第${ pageBean.pageNumber }/${ pageBean.totalPage }页  
总记录数:${pageBean.totalRecord } 每页展现:${ pageBean.pageSize
}  
<c:if test=”${ pageBean.pageNumber gt 1 }”>
<a href=”${ pageContext.request.contextPath
}/productListPageServlet?pageNumber=1″>[首页]</a> |  
<a href=”${ pageContext.request.contextPath
}/productListPageServlet?pageNumber=${pageBean.pageNumber-1}”>[上一页]</a>| 
</c:if>
 
<c:forEach var=”i” begin=”1″ end=”${pageBean.totalPage }”>
<c:if test=”${ pageBean.pageNumber == i }”>
${ i }
</c:if>
<c:if test=”${ pageBean.pageNumber != i }”>
<a href=”${ pageContext.request.contextPath
}/productListPageServlet?pageNumber=${ i}”>${ i }</a>
</c:if>
</c:forEach>
 
<c:if test=”${ pageBean.pageNumber lt pageBean.totalPage }”>
<a href=”${ pageContext.request.contextPath
}/productListPageServlet?pageNumber=${pageBean.pageNumber+1}”>[下一页]</a>| 
<a href=”${ pageContext.request.contextPath
}/productListPageServlet?pageNumber=${pageBean.totalPage}”>[尾页]</a>
</c:if>

 

第一章案例:条件查询
三.一案例介绍
我们曾经到位了“查询全部商品”功效,假若我们意在从具有数据中筛选出须求的一些数据,会发觉经过肉眼比较困难,且从未须要。在付出中,大家应用“条件查询”来缓和此类主题材料。

    public void setPageCode(int pageCode) {

三.二案例剖判
“条件查询”和“查询全体”,七个职能特别相似,分裂出就在尺度的筛选上,通过页面将分类和商品名称传递给服务器,服务器获得查询条件,然后公告service拼凑sql语句实行具体查询。
大家开采只要要完结“条件查询”,必须在事先的“查询全部”时,查询全部的归类,从而能够实行标准化的取舍。

        this.pageCode = pageCode;

叁.3案例完成
步骤2:确定JavaBean,在商品Product对象中,使用的“category_id”字段存放分类id。

    }

步骤二:修改Servlet,查询所一时,查询全部分类(条件查询利用)
修改:/day14_condition/src/cn/itcast/web/servlet/ProductFindAllServlet.java

 

public class ProductFindAllServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

    public int getTotalPages() {

public void doGet(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
//1

        /*

//贰.一 通告service,查询全数商品
ProductService productService = new ProductService();
List<Product> allProduct = productService.findAll();
//二.贰 通过分类service,查询全部分类
CategoryService categoryService = new CategoryService();
List<Category> allCategory = categoryService.findAll();

         * 计算总页数

//3 选择jsp
//叁.1 将查询结果,全部商品,存放在request功效域
request.setAttribute(“allProduct”, allProduct);
//三.二 将查询结果,全部分类,存放在request功效域
request.setAttribute(“allCategory”, allCategory);
//叁.三 请求转载
request.getRequestDispatcher(“/admin/product/product_list.jsp”)
.forward(request, response);

         * 通过总记录数和每页记录数来计量总页数,当存在余数时,总页数 +

}

         */

步骤3:修改product.jsp,显示全体分类
修改:/day14_condition/WebContent/admin/product/product_list.jsp

        int totalPages = totalRecord / pageSize;

<c:forEach items=”${allCategory}” var=”category”>
<option value=”${category.cid}”>${category.cname}</option>
</c:forEach>

            return totalRecord % pageSize == 0 ? totalPages : totalPages

步骤四:修改product.jsp,完善表单。大概浏览如下:
<form
action=”${pageContext.request.contextPath}/productFindAllServlet”
method=”post”>
分类 <select name=”category_id” class=”form-control”>
商品名称:<input type=”text” name=”pname” size=”一五”
class=”form-control”/>
<button type=”submit” value=”查询”
class=”button_view”>查询</button>

  • 1;

步骤五:完善servlet,获得查询条件,并打包到javabean,然后传递给service
位置:/day14_condition/src/cn/itcast/web/servlet/ProductFindAllServlet.java

    }

此时ProductService的findAll(product) 不存在所以抛非常。
public class ProductFindAllServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
//1 获得请求参数
Product product = MyBeanUtils.populate(Product.class,
request.getParameterMap());

 

//2.一 通告service,查询全体商品
ProductService productService = new ProductService();
List<Product> allProduct = productService.findAll(product);
//2.2 …省略
步骤陆:完善service,接收条件,并拼凑sql查询条件。
/**
* 查询全体(含条件)
* @return
*/
public List<Product> findAll(Product product) throws SQLException
{
return productDao.findAll(product);
}

    // public void setTotalPages(int totalPages) {

步骤7:完善dao,使用条件和参数

    // this.totalPages = totalPages;

/**
* 查询全部
* @return
*/
public List<Product> findAll(Product product) throws SQLException
{
//1.1 拼凑sql语句,格式:and 属性 符号 ?
StringBuilder builder = new StringBuilder();
//一.2 拼凑实际参数。实际参数可再一次、参数须求各种,所以选拔List集结
List<Object> paramsList = new ArrayList<Object>();

    // }

//贰.1 分类不为空 ,增添分类标准
if(product.getCategory_id() != null &&
!””.equals(product.getCategory_id())){
builder.append(” and category_id = ? “);
paramsList.add(product.getCategory_id());
}

 

//贰.二 商品名不为空,增加商品名条件
if(product.getPname() != null && !””.equals(product.getPname())){
builder.append(” and pname like ? “);
//商品名需求模糊查询就能够,须要手动的增多%
paramsList.add(“%”+product.getPname()+”%”);
}

    public int getTotalRecord() {

//三 转换来须求的门类
String condition = builder.toString();
Object[] params = paramsList.toArray();

        return totalRecord;

QueryRunner queryRunner = new QueryRunner(C3P0Utils.getDataSource());
String sql = “select * from product where 1=1 ” + condition;
return queryRunner.query(sql, new
BeanListHandler<Product>(Product.class), params);
}

    }

三.肆查询条件回显

 

步骤一:修改servlet,将打包好的询问条件,增多到request功用域
/day14_condition/src/cn/itcast/web/servlet/ProductFindAllServlet.java

    public void setTotalRecord(int totalRecord) {

public void doGet(HttpServletRequest request, HttpServletResponse
response)
throws ServletException, IOException {
//一 得到请求参数
Product product = MyBeanUtils.populate(Product.class,
request.getParameterMap());

        this.totalRecord = totalRecord;

//二.一 通告service,查询全部商品
ProductService productService = new ProductService();
List<Product> allProduct = productService.findAll(product);
//二.二 通过分类service,查询全数分类
CategoryService categoryService = new CategoryService();
List<Category> allCategory = categoryService.findAll();

    }

//3 选择jsp
//叁.一 将查询结果,全数商品,存放在request效用域
request.setAttribute(“allProduct”, allProduct);
//三.二 将查询结果,全体分类,存放在request功能域
request.setAttribute(“allCategory”, allCategory);
//叁.三 存放条件
request.setAttribute(“product”, product);
//三.肆 伸手转载
request.getRequestDispatcher(“/admin/product/product_list.jsp”).forward(request,
response);

 

}

    public int getPageSize() {

步骤2:使用“条件product”进行标签回显
/day14_condition/WebContent/admin/product/product_list.jsp
分类
<c:forEach items=”${allCategory}” var=”category”>
<option value=”${category.cid}”
${product.category_id == category.cid ? “selected=’selected'” : “”}
>
${category.cname}
</option>
</c:forEach>
商品名称:
<input type=”text” name=”pname” size=”15″ value=”${product.pname}”
class=”form-control”/>

        return pageSize;

    }

    public void setPageSize(int pageSize) {

        this.pageSize = pageSize;

    }

 

    public List<T> getBeanList() {

        return beanList;

    }

 

    public void setBeanList(List<T> beanList) {

        this.beanList = beanList;

    }

 

    @Override

    public String toString() {

        return “PageBean [pageCode=” + pageCode + “, totalPages=” + “,
totalRecord=” + totalRecord + “, pageSize=” + pageSize + “, beanList=” +
beanList + “]”;
    }

 

    public PageBean(int pageCode, int totalRecord, int
pageSize, List<T> beanList) {

        super();

        this.pageCode = pageCode;

        this.totalRecord = totalRecord;

        this.pageSize = pageSize;

        this.beanList = beanList;

    }

 

    public PageBean() {

        super();

    }

}

四、分页在各层中的管理

    *   页面:给出分页相关的链接。
        >   页面需求给Servlet传递当前页码。
    *   Servlet:创立PageBean对象,
给PageBean对象具有的习性赋值,然后传递给页面。
        >   给Dao传递当前页码和每页记录数。
    *   Service:略
    *   Dao
        >   肩负获取:totalRecord,select count(*) from customer
        >   担任获取:BeanList<T>,select * from customer
limit x, y,从x行开始,查询y行。
        >   limit计算公式:(当前页-一) *
每页记录数,得出的正是伊始行

图片 1

5、展现分页页码列表

1 2 3 4 5 6 7 8 9 10

    *   最多突显多少个页码?
        >   暂定10个
    *   当前页在页码列表中的地点?
        >   暂定为6

   
只要求pageCode就足以做到页码列表,需求采取pageCode来推算出初始页码(begin)和告竣页码(end)

    计算公式:
        *   要是总页数 <= 10(列表长度),那么begin = 一,end =
总页数
        *   如若总页数 > 拾,使用公式总计
            >   begin = pageCode – 5
            >   end = pageCode + 4
            >   begin溢出:当begin小于1时,让begin = 1
            >   end溢出:当end > totalPages时,让end =
totalPages

陆、在超链接中保留请求参数

   
当使用多规格查询时,如若点击任范晓冬链接,会丢掉原超链接中的参数,也正是丢失请求条件,所以须要在页面包车型大巴全部超链接中都要保存参数。

   
能够把?后的壹切参数用一个字符串保存到PageBean的UENCOREL属性中,这几个职务交给Servlet。
    然后在页面中选拔${pageBean.url }来设置超链接。

——项目代码

图片 2

===============================================================================

com.wyc.cstm.dao.CustomerDao

import java.util.ArrayList;

import java.util.List;

 

import org.apache.commons.dbutils.QueryRunner;

import org.apache.commons.dbutils.handlers.BeanListHandler;

import org.apache.commons.dbutils.handlers.ScalarHandler;

 

import com.wyc.cstm.domain.Customer;

import com.wyc.cstm.domain.PageBean;

import com.wyc.jdbc.TxQueryRunner;

 

/**

 * 长久层 通过QueryRunner来操作数据库

 * 

 * @author 31067

 * 

 */

public class CustomerDao {

    private QueryRunner qr = new TxQueryRunner();

 

    // /*

    // * 查询任何客户消息

    // */

    // public List<Customer> findAll() {

    // try {

    // String sql = “select * from customer”;

    // return qr.query(sql, new
BeanListHandler<Customer>(Customer.class));

    // } catch (Exception e) {

    // throw new RuntimeException(e);

    // }

    // }

 

    public PageBean<Customer> findAll(int pageCode, int pageSize)
{

        try {

            /*

             * 1、创建PageBean对象
2、设置PageBean对象的pageCode和pageSize

             * 3、得到totalRecord,设置给pageBean
4、得到beanList,设置给pageBean 5、返回pageBean

             */

            PageBean<Customer> pageBean = new
PageBean<Customer>();

            /*

             * 设置pageCode、pageSize

             */

            pageBean.setPageCode(pageCode);

            pageBean.setPageSize(pageSize);

 

            /*

             * 得到totalRecord

             */

            String sql = “select count(*) from customer”;

            // 再次来到值类型是Object,强转为Number类型

            Number num = (Number) qr.query(sql, new ScalarHandler());

            int totalRecord = num.intValue();

            pageBean.setTotalRecord(totalRecord);

 

            /*

             * 得到beanList

             */

            sql = “select * from customer order by cname limit ?, ?”;

            // (当前页码 – 壹) * 每行记录数 = 早先行

            Object[] params = { (pageCode – 1) * pageSize, pageSize
};

            List<Customer> beanList = qr.query(sql, new
BeanListHandler<Customer>(Customer.class), params);

            pageBean.setBeanList(beanList);

 

            return pageBean;

        } catch (Exception e) {

            throw new RuntimeException(e);

        }

    }

 

    /**

     * 多准绳构成查询

     * 

     * @param criteria

     * @return

     */

    // public List<Customer> query(Customer criteria) {

    // /*

    // * 1、给出SQL模板

    // * 二、给出参数

    // * 3、调用query()方法

    // * 结果集Computer:BeanListHandler

    // */

    //

    // // 给出SQL语句的前半有的

    // StringBuilder sql = new

    // StringBuilder(“select * from customer where 1 = 1”);

    // /*

    // * 壹、判定规范,向SQL语句中追加where子句

    // * 因为无法明确是还是不是带有该参数,所以供给利用if语句来推断

    // * 2、给出参数

    // *
因为不能够分明?占位符所对应的参数,所以在认清参数时,即将加多参数

    // * 使用ArrayList来装载参数

    // */

    //

    // // 装载参数

    // List<Object> params = new ArrayList<Object>();

    //

    //

    // String cname = criteria.getCname();

    // if(cname != null && !cname.trim().isEmpty())

    // {

    // sql.append(” and cname = ?”);

    // //模糊查询

    // params.add(“%” + cname + “%”);

    // }

    //

    // String gender = criteria.getGender();

    // if(gender != null && !gender.trim().isEmpty())

    // {

    // sql.append(” and gender = ?”);

    // params.add(gender);

    // }

    //

    // String cellphone = criteria.getCellphone();

    // if(cellphone != null && !cellphone.trim().isEmpty())

    // {

    // sql.append(” and cellphone = ?”);

    // params.add(“%” + cellphone + “%”);

    // }

    //

    // String email = criteria.getEmail();

    // if(email != null && !email.trim().isEmpty())

    // {

    // sql.append(” and email = ?”);

    // params.add(“%” + email + “%”);

    // }

    //

    // /*

    // * 执行query

    // */

    // try {

    // return qr.query(sql.toString(), new

    // BeanListHandler<Customer>(Customer.class),
params.toArray());

    // } catch (SQLException e) {

    // throw new RuntimeException(e);

    // }

    // }

 

    public PageBean<Customer> query(Customer criteria, int
pageCode, int pageSize) {

        try {

            /*

             * 一、创制PageBean对象 二、设置已有总体性,pageCode和pageSize
三、获得totalRecord

             * 4、得到BeanList

             */

            /*

             * 成立PageBean对象,设置已有品质

             */

            PageBean<Customer> pageBean = new
PageBean<Customer>();

            pageBean.setPageCode(pageCode);

            pageBean.setPageSize(pageSize);

 

            /*

             * 获得tr,需求基于标准进行询问

             */

 

            // 给出SQL语句的前半某些

            StringBuilder countSql = new StringBuilder(“select count(*)
from customer”);

            StringBuilder whereSql = new StringBuilder(” where 1 = 1″);

            /*

             * 一、评定楷模,向SQL语句中扩大where子句
因为不能够显明是否带有该参数,所以必要动用if语句来推断 贰、给出参数

             *
因为无法明确?占位符所对应的参数,所以在认清参数时,将要加多参数
使用ArrayList来装载参数

             */

 

            // 装载参数

            List<Object> params = new ArrayList<Object>();

 

            String cname = criteria.getCname();

            if (cname != null && !cname.trim().isEmpty()) {

                whereSql.append(” and cname like ?”);

                // 模糊查询

                params.add(“%” + cname + “%”);

            }

 

            String gender = criteria.getGender();

            if (gender != null && !gender.trim().isEmpty()) {

                whereSql.append(” and gender = ?”);

                params.add(gender);

            }

 

            String cellphone = criteria.getCellphone();

            if (cellphone != null && !cellphone.trim().isEmpty()) {

                whereSql.append(” and cellphone like ?”);

                params.add(“%” + cellphone + “%”);

            }

            String email = criteria.getEmail();

            if (email != null && !email.trim().isEmpty()) {

                whereSql.append(” and email like ?”);

                params.add(“%” + email + “%”);

            }

 

            /*

             * 执行SQL语句 select count(*) from customer = where ….

             */

            Number num = (Number)
qr.query(countSql.append(whereSql).toString(), new ScalarHandler(),
params.toArray());
 

            int totalRecord = num.intValue();

            pageBean.setTotalRecord(totalRecord);

 

            /*

             * 得到beanList

             */

            StringBuilder sql = new StringBuilder(“select * from
customer”);

 

            /*

             * 查询beanList这一步还亟需付出limit子句

             */

            StringBuilder limitSql = new StringBuilder(” limit ?,?”);

 

            /*

             * params中必要交给limit后对应的参数值

             */

            params.add((pageCode – 1) * pageSize);

            params.add(pageSize);

 

            List<Customer> beanList =
qr.query(sql.append(whereSql).append(limitSql).toString(), new
BeanListHandler<Customer>(Customer.class), params.toArray());

            pageBean.setBeanList(beanList);

 

            return pageBean;

        } catch (Exception e) {

            throw new RuntimeException(e);

        }

    }

}

===============================================================================

com.wyc.cstm.domain.Customer

/**

 * 领域对象

 * 与表单和数据库表对应

 */

public class Customer {

    /*

     * 对应数据库表:

     * cid CHAR(32) PRIMARY KEY,

     * cname VARCHAR(40) NOT NULL,

     * gender VARCHAR(6) NOT NULL,

     * birthday CHAR(10),

     * cellphone VARCHAR(15),

     * email VARCHAR(40),

     * description VARCHAR(500)

     */

    private String cid;//主键

    private String cname;//客户姓名

    private String gender;//性别

    private String birthday;//客户出生之日

    private String cellphone;//客户手提式有线话机

    private String email;//客户邮箱

    private String description;//客户新闻描述

    public String getCid() {

        return cid;

    }

    public void setCid(String cid) {

        this.cid = cid;

    }

    public String getCname() {

        return cname;

    }

    public void setCname(String cname) {

        this.cname = cname;

    }

    public String getGender() {

        return gender;

    }

    public void setGender(String gender) {

        this.gender = gender;

    }

    public String getBirthday() {

        return birthday;

    }

    public void setBirthday(String birthday) {

        this.birthday = birthday;

    }

    public String getCellphone() {

        return cellphone;

    }

    public void setCellphone(String cellphone) {

        this.cellphone = cellphone;

    }

    public String getEmail() {

        return email;

    }

    public void setEmail(String email) {

        this.email = email;

    }

    public String getDescription() {

        return description;

    }

    public void setDescription(String description) {

        this.description = description;

    }

    @Override

    public String toString() {

        return “Customer [cid=” + cid + “, cname=” + cname + “,
gender=” + gender + “, birthday=” + birthday + “, cellphone=” +
cellphone + “, email=” + email + “, description=” + description + “]”;

    }

    public Customer(String cid, String cname, String gender, String
birthday, String cellphone, String email, String description) {

        super();

        this.cid = cid;

        this.cname = cname;

        this.gender = gender;

        this.birthday = birthday;

        this.cellphone = cellphone;

        this.email = email;

        this.description = description;

    }

    public Customer() {

        super();

    }

}

===============================================================================

com.wyc.cstm.service.CustomerService

import com.wyc.cstm.dao.CustomerDao;

import com.wyc.cstm.domain.Customer;

import com.wyc.cstm.domain.PageBean;

 

/**

 * 业务层 依赖Dao

 */

public class CustomerService {

    private CustomerDao customerDao = new CustomerDao();

 

    // /**

    // * 查询全部客户

    // * @return

    // */

    // public List<Customer> findAll(){

    // return customerDao.findAll();

    // }

 

    public PageBean<Customer> findAll(int pageCode, int pageSize)
{

        return customerDao.findAll(pageCode, pageSize);

    }

 

    /**

     * 多标准构成查询

     * 

     * @param criteria

     * @return

     */

    public PageBean<Customer> query(Customer criteria, int
pageCode, int pageSize) {

        return customerDao.query(criteria, pageCode, pageSize);

    }

}

===============================================================================

com.wyc.cstm.web.servlet.CustomerServlet

import java.io.IOException;

 

import javax.servlet.ServletException;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

 

import com.wyc.bean.CommonUtils;

import com.wyc.cstm.domain.Customer;

import com.wyc.cstm.domain.PageBean;

import com.wyc.cstm.service.CustomerService;

import com.wyc.servlet.BaseServlet;

 

/**

 * Web层

 * 

 * @author 31067

 * 

 */

public class CustomerServlet extends BaseServlet {

 

    private CustomerService customerService = new CustomerService();

 

    // public String findAll(HttpServletRequest request,
HttpServletResponse

    // response) throws ServletException, IOException {

    // /*

    // * 壹、调用service获得全部客户

    // * 贰、将整个音讯保存到request域中

    // * 3、转发到list.jsp

    // */

    // request.setAttribute(“cstmList”, customerService.findAll());

    // return “f:/list.jsp”;

    // }

 

    public String findAll(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {

        /*

         * 壹、获取页面传递的pageCode
         * 2、给定pageSize的值

         * 3、使用pageCode和pageSize调用Service方法,得到PageBean对象

         * 4、将PageBean对象保存到request域中
         * 5、转发到list.jsp

         */

        /*

         * 一、获得pageCode有两种恐怕
         *     即使pageCode参数不设有,表明pageCode = 一

         *     倘诺pageCode参数存在,只须要将参数调换到int类型就能够

         */

        int pageCode = getPageCode(request);

 

        /*

         * 2、给定pageSize的值,每页10行

         */

        int pageSize = 10;

 

        /*

         * 3、调用Service的findAll方法
         *     传递pageCode和pageSize给Service方法
         *     返回PageBean对象

         */

        PageBean<Customer> pageBean =
customerService.findAll(pageCode, pageSize);

        /*

         * 添加URL及参数

         */

        pageBean.setUrl(getUrl(request));

        /*

         * 四、将PageBean对象保存到request域中

         */

        request.setAttribute(“pageBean”, pageBean);

 

        return “f:list.jsp”;

    }

 

    private int getPageCode(HttpServletRequest request) {

        String value = request.getParameter(“pageCode”);

        if (value == null || value.trim().isEmpty()) {

            return 1;

        }

            return Integer.parseInt(value);

    }

 

    // public String query(HttpServletRequest request,
HttpServletResponse

    // response) throws ServletException, IOException {

    // /*

    // *
一、封装表单数据到Customer对象中,它只有多少个本性:cname、gender、cellphone、email

    // * 它实质上正是三个询问条件的三结合对象

    // * 2、调用Service的query()方法,得到List<Customer>

    // * 3、将List<Customer>保存到request域中

    // * 4、转发到list.jsp

    // */

    //

    // Customer criteria = CommonUtils.toBean(request.getParameterMap(),

    // Customer.class);

    // List<Customer> cstmList = customerService.query(criteria);

    // request.setAttribute(“cstmList”, cstmList);

    // return “f:list.jsp”;

    // }

    public String query(HttpServletRequest request, HttpServletResponse
response) throws Exception {

        /*

        * 0、把条件参数封装到Customer对象中 一、获取页面传递的pageCode
贰、给定pageSize的值

        *
三、使用pageCode和pageSize以及标准字符串调用Service方法,得到PageBean对象

        * 四、将PageBean对象保存到request域中 伍、转载到list.jsp

        */

        // 获取查询条件

        Customer criteria =
CommonUtils.toBean(request.getParameterMap(), Customer.class);

 

        /*

        * 管理GET请求方式编码难题

        */

        criteria = encoding(criteria);

        int pageCode = getPageCode(request);

        int pageSize = 10;

 

        PageBean<Customer> pageBean =
customerService.query(criteria, pageCode, pageSize);

 

        /*

        * 得到URL,保存到pageBean中

        */

        pageBean.setUrl(getUrl(request));

 

        request.setAttribute(“pageBean”, pageBean);

 

        return “f:list.jsp”;

    }

 

    /**

    * 管理多少的编码难点

    * 

    * @throws Exception

    */

    private Customer encoding(Customer criteria) throws Exception {

        String cname = criteria.getCname();

        String gender = criteria.getGender();

        String cellphone = criteria.getCellphone();

        String email = criteria.getEmail();

 

        if (cname != null && !cname.trim().isEmpty()) {

            cname = new String(cname.getBytes(“iso-8859-1”), “utf-8”);

            criteria.setCname(cname);

        }

        if (gender != null && !gender.trim().isEmpty()) {

            gender = new String(gender.getBytes(“iso-8859-1”), “utf-8”);

            criteria.setGender(gender);

        }

        if (cellphone != null && !cellphone.trim().isEmpty()) {

            cellphone = new String(cellphone.getBytes(“iso-8859-1”),
“utf-8”);

            criteria.setCellphone(cellphone);

        }

        if (email != null && !email.trim().isEmpty()) {

            email = new String(email.getBytes(“iso-8859-1”), “utf-8”);

            criteria.setEmail(email);

        }

 

        return criteria;

    }
 

    /**

    * 截取请求U途锐L /项目名/Servlet路线?参数字符串

    * 

    * @param request

    * @return

    */

    private String getUrl(HttpServletRequest request) {

        // 获取项目名

        String contextPath = request.getContextPath();

 

        // 获取ServletPath,即/CustomerServlet

        String servletPath = request.getServletPath();

 

        // 获取问号之后的参数部分

        String queryString = request.getQueryString();

 

        /*

        * 判定queryString中是还是不是带有pageCode
若是含有,需求截取掉pageCode

        */

        if (queryString.contains(“&pageCode=”)) {

            int index = queryString.lastIndexOf(“&pageCode=”);

            queryString = queryString.substring(0, index);

        }

 

        return contextPath + servletPath + “?” + queryString;

    }

}

===============================================================================

c3p0-config.xml

<?xml version=”1.0″ encoding=”UTF-8″?>

<c3p0-config>

    <!– 暗中认可配置 –>

    <default-config>

        <!– 连接四大参数配置 –>

        <property
name=”jdbcUrl”>jdbc:mysql://localhost:3306/customers</property>

        <property
name=”driverClass”>com.mysql.jdbc.Driver</property>

        <property name=”user”>root</property>

        <property name=”password”>Admin123</property>

        <!– 连接池参数配置 –>

        <property name=”acquireIncrement”>3</property>

        <property name=”initialPoolSize”>10</property>

        <prpperty name=”minPoolSize”>2</prpperty>

        <property name=”maxPoolSize”>10</property>

    </default-config>

</c3p0-config>

 

 

 

===============================================================================

frame.jsp

<%@ page language=”java” import=”java.util.*”
pageEncoding=”UTF-8″%>

<%@ taglib prefix=”c” uri=”” %>

<%

    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”>

 

</head>

    <frameset rows=”20%, *” >

        <frame src=”<c:url%20value=’/top.jsp’/>” name=”top”>

        <frame src=”<c:url%20value=’/welcome.jsp’/>”
name=”main”>

    </frameset>

</html>

 

===============================================================================

list.jsp

<%@ page language=”java” import=”java.util.*”
pageEncoding=”UTF-8″%>

<%@ taglib prefix=”c” uri=””%>

 

<%

    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>My JSP ‘list.jsp’ starting page</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”>

 

</head>

 

<body>

    <table align=”center” border=”1px”>

        <tr>

            <th>姓名</th>

            <th>性别</th>

            <th>生日</th>

            <th>手提式有线电话机号码</th>

            <th>邮箱</th>

            <th>描述</th>

        </tr>

        <%–

            要遍历的是pageBean对象的beanList集合

        –%>

        <c:forEach items=”${requestScope.pageBean.beanList }”
var=”cstm”>

            <tr>

                <td>${cstm.cname }</td>

                <td>${cstm.gender }</td>

                <td>${cstm.birthday }</td>

                <td>${cstm.cellphone }</td>

                <td>${cstm.email }</td>

                <td>${cstm.description }</td>

                <td>

                    <a href=”<c:url
value=’/CustomerServlet?method=preEdit&cid=${cstm.cid
}’/>”>编辑</a>

                    <a href=”<c:url
value=’/CustomerServlet?method=delete&cid=${cstm.cid
}’/>”>删除</a>

                </td>

            </tr>

        </c:forEach>

 

    </table>

    <%–

        给出分页相关的链接

 –%>

 <center>

        <%–
因为pageBean中保存了当前页的ULANDL,并且包蕴项目名和Servlet路线与参数,所以能够不用<c:url
/>进行安装了 –%>

        <%– 因为pageBean中的UPRADOL不包蕴pageCode,所以照旧亟待手动增添–%>

        <a href=”${pageBean.url%20}&pageCode=1″ >首页</a>

        <c:if test=”${pageBean.pageCode > 1 }”>

            <a href=”${pageBean.url }&pageCode=${pageBean.pageCode-1
}” >上一页</a>

        </c:if>

 

        <%– 分页页码列表 –%>

 

        <%– 计算begin和end –%>

        <c:choose>

            <%– 当总页数小于十,那么就把具备的页数都显示出来 –%>

            <c:when test=”${pageBean.totalPages <= 10 }”>

                <c:set var=”begin” value=”1″ />

                <c:set var=”end” value=”${pageBean.totalPages }”
/>

            </c:when>

 

            <%– 当总页数 >= 10 –%>

            <c:when test=”${pageBean.totalPages > 10 }”>

                <c:set var=”begin” value=”${pageBean.pageCode – 5 }”
/>

                <c:set var=”end” value=”${pageBean.totalPages + 4 }”
/>
 

                <%– begin溢出 –%>

                <c:if test=”${begin < 1 }”>

                    <c:set var=”begin” value=”1″ />

                    <%– 因为begin已经溢出,所以表示总页数相差拾页
–%>

                    <c:set var=”end” value=”${pageBean.totalPages }”
/>

                </c:if>

 

                <%– end溢出 –%>

                <c:if test=”${end > pageBean.totalPages }”>

                    <%– 因为end溢出,所以begin = end – 9 –%>

                    <c:set var=”begin” value=”${pageBean.totalPages –
9 }” />

                    <c:set var=”end” value=”${pageBean.totalPages }”
/>

                </c:if>

            </c:when>

        </c:choose>

 

        <%– 循环呈现页码列表 –%>

        <c:forEach var=”i” begin=”${begin }” end=”${end }”>

        <c:choose>

            <%– 当前页码与页码列表同样时,输出轻易文本 –%>

            <c:when test=”${pageBean.pageCode eq i }”>

                [${i }]

           </c:when>

            <c:otherwise>

                <a href=”${pageBean.url%20}&pageCode=${i%20}”>${i
}</a>

            </c:otherwise>

        </c:choose>

    </c:forEach>

 

 

    <c:if test=”${pageBean.pageCode < pageBean.totalPages }”>

        <a href=”${pageBean.url%20}&pageCode=${pageBean.pageCode+1%20}”
>下一页</a>

    </c:if>
 

    <a href=”${pageBean.url%20}&pageCode=${pageBean.totalPages%20}”
>尾页</a>

    第${pageBean.pageCode }页/共${pageBean.totalPages }页

 

    </center>

</body>

</html>

 

===============================================================================

query.jsp

<%@ page language=”java” import=”java.util.*”
pageEncoding=”UTF-8″%>

<%@ taglib prefix=”c” uri=””%>

 

<%

    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>My JSP ‘query.jsp’ starting page</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”>

 

</head>

 

<body>

    <h三 align=”center”>高档搜索</h三>

    <form action=”<c:url value=’/CustomerServlet’ />”
method=”get”>

        <input type=”hidden” name=”method” value=”query” />

        <table border=”0″ align=”center” width=”40%”
style=”margin-left: 100px;”>

            <tr>

                <td width=”100px”>客户名称</td>

                <td>

                    <input type=”text” name=”cname” />

                </td>

            </tr>

            <tr>

                <td>客户性别</td>

                <td>

                    <select name=”gender”>

                        <option value=””>请选择</option>

                        <option value=”男”>男</option>

                        <option value=”女”>女</option>

                    </select>

                </td>

            </tr>

            <tr>

                <td>手机</td>

                <td>

                    <input type=”text” name=”cellphone” />

                </td>

            </tr>

            <tr>

                <td>邮箱</td>

                <td>

                    <input type=”text” name=”email” />

                </td>

            </tr>

            <tr>

                <td> </td>

                <td>

                    <input type=”submit” value=”搜索”/>

                    <input type=”reset” value=”重置” />

                </td>

             </tr>

        </table>

    </form>

</body>

</html>

 

===============================================================================

top.jsp

<%@ page language=”java” import=”java.util.*”
pageEncoding=”UTF-8″%>

<%@ taglib prefix=”c” uri=””%>

 

<%

    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中写targe,相当于在本页面中的所有a标签中增加target –>

<base target=”main”>

 

<title>My JSP ‘top.jsp’ starting page</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”>

 

</head>

 

<body style=”text-align: center”>

    <h一>客户关系管理系列</h壹>

    <a href=”<c:url%20value=’/add.jsp’/>”>增加客户</a>
 |

    <a href=”<c:url value=’/CustomerServlet?method=findAll’
/>”>查询客户</a> |

    <a href=”<c:url%20value=’/query.jsp’/>”>高档寻觅</a>
|

    <a href=”<c:url value=’/welcome.jsp’
/>”>重返首页</a>

</body>

</html>

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图
Copyright @ 2010-2019 澳门新葡亰官网app 版权所有