一、在menu_search.jsp中编写搜索相关代码
<script type="text/javascript">
/**
* my_click和my_blur均是用于前台页面搜索框的函数
*/
//鼠标点击搜索框时执行
function my_click(obj, myid){
//点击时,如果取得的值和搜索框默认value值相同,则将搜索框清空
if (document.getElementById(myid).value == document.getElementById(myid).defaultValue){
document.getElementById(myid).value = '';
obj.style.color='#000';
}
}
//鼠标不聚焦在搜索框时执行
function my_blur(obj, myid){
//鼠标失焦时,如果搜索框没有输入值,则用搜索框的默认value值填充
if (document.getElementById(myid).value == ''){
document.getElementById(myid).value = document.getElementById(myid).defaultValue;
obj.style.color='#999';
}
}
/**
* 点击搜索按钮执行的函数
*/
function search(){
document.getElementById("searchform").submit();
}
</script>
<form action="${pageContext.request.contextPath }/MenuSearchServlet" id="searchform">
<table width="100%" border="0" cellspacing="0">
<tr>
<td style="text-align:right; padding-right:220px">
Search
<input type="text" name="textfield" class="inputtable" id="textfield" value="请输入书名"
onmouseover="this.focus();"
onclick="my_click(this, 'textfield');"
onBlur="my_blur(this, 'textfield');"/>
<a href="#">
<img src="${pageContext.request.contextPath}/client/images/serchbutton.gif" border="0" style="margin-bottom:-4px" onclick="search()"/>
</a>
</td>
</tr>
</table>
</form>
二、控制层
/**
* 前台页面,用于菜单栏下面搜索功能的servlet
*/
public class MenuSearchServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
this.doPost(req, resp);
}
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// 1.定义当前页码,默认为1
int currentPage = 1;
String _currentPage = req.getParameter("currentPage");
if (_currentPage != null) {
currentPage = Integer.parseInt(_currentPage);
}
// 2.定义每页显示条数,默认为4
int currentCount = 4;
//获取前台页面搜索框输入的值
String searchfield = req.getParameter("textfield");
//如果搜索框中没有输入值,则表单传递的为默认值,此时默认查询全部商品目录
if("请输入书名".equals(searchfield)){
req.getRequestDispatcher("/showProductByPage").forward(req, resp);
return;
}
//调用service层的方法,通过书名模糊查询,查找相应的图书
ProductService service = new ProductService();
PageBean bean = service.findBookByName(currentPage,currentCount,searchfield);
// 将数据存储到request范围,跳转到product_search_list.jsp页面展示
req.setAttribute("bean", bean);
req.getRequestDispatcher("/client/product_search_list.jsp").forward(req, resp);
}
}
三、视图层product_search_list.jsp,显示搜索结果
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>bookStore列表</title>
<%--导入css --%>
<link rel="stylesheet" href="client/css/main.css" type="text/css" />
</head>
<body class="main">
<jsp:include page="head.jsp" />
<jsp:include page="menu_search.jsp" />
<div id="divpagecontent">
<table width="100%" border="0" cellspacing="0">
<tr>
<td><div style="text-align:right; margin:5px 10px 5px 0px">
<a href="index.jsp">首页</a> > 全部商品 > 图书列表
</div>
<table cellspacing="0" class="listcontent">
<tr>
<td>
<h1>商品目录</h1>
<hr />
<h1>全部商品</h1> 共${bean.totalCount}种商品
<hr />
<div style="margin-top:20px; margin-bottom:5px">
<img src="client/images/productlist.gif" width="100%" height="38" />
</div>
<table cellspacing="0" class="booklist">
<tr>
<c:forEach items="${bean.ps}" var="p" varStatus="vs">
<td>
<div class="divbookpic">
<p>
<a href="${pageContext.request.contextPath}/findProductById?id=${p.id}"><img
src="${pageContext.request.contextPath}${p.imgurl}"
width="115" height="129" border="0" /> </a>
</p>
</div>
<div class="divlisttitle">
<a href="${pageContext.request.contextPath}/findProductById?id=${p.id}">书名: ${p.name}<br />售价:¥${p.price} </a>
</div>
</td>
<%-- <c:if test="${vs.count%4==0}">
</c:if> --%>
</c:forEach>
</tr>
</table>
<!-- <table cellspacing="0" class="booklist">
<tr>
</tr>
</table> -->
<div class="pagination">
<ul>
<c:if test="${bean.currentPage!=1}">
<li class="nextPage">
<a href="${pageContext.request.contextPath}/MenuSearchSerlvet?currentPage=${bean.currentPage-1}&textfield=${bean.searchfield}"><<上一页</a>
</li>
</c:if>
<c:if test="${bean.currentPage==1}">
<li class="disablepage"><<上一页</li>
</c:if>
<c:forEach begin="1" end="${bean.totalPage}" var="pageNum">
<c:if test="${pageNum==bean.currentPage}">
<li class="currentpage">${pageNum }</li>
</c:if>
<c:if test="${pageNum!=bean.currentPage}">
<li><a href="${pageContext.request.contextPath}/MenuSearchSerlvet?currentPage=${pageNum}&textfield=${bean.searchfield}">${pageNum}</a>
</li>
</c:if>
</c:forEach>
<c:if test="${bean.currentPage==bean.totalPage||bean.totalPage==0}">
<li class="disablepage">下一页 >></li>
</c:if>
<c:if test="${bean.currentPage!=bean.totalPage&&bean.totalPage!=0}">
<li class="nextpage">
<a href="${pageContext.request.contextPath}/MenuSearchSerlvet?currentPage=${bean.currentPage+1}&textfield=${bean.searchfield}">下一页>></a>
</li>
</c:if>
</ul>
</div></td>
</tr>
</table>
</td>
</tr>
</table>
</div>
<jsp:include page="foot.jsp" />
</body>
</html>