【index.html】
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta http-equiv="Content-Type" content="text/html" charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="css/index.css">
<script charset="GBK" language="JavaScript" type="text/javascript" src="js/index.js" >
</script>
</head>
<body>
<div id="div_container">
<div id="div_fruit_list">
<p class="center f30">欢迎使用水果库存后台管理系统</p>
<div style="border:0px solid red;width:60%;margin-left:20%;text-align: right;">
<!--关键字查询功能-->
<form th:action="@{/index}" method="post" style="float: left;">
<input type="hidden" name="operate" value="search"/>
请输入查询关键字:<input type="text" name="keyword" th:value="${session.keyword}">
<input type="submit" value="查询" class="btn">
</form>
<a th:href="@{/add.html}" style="border:0px solid blue;margin-bottom:4px;">添加库存记录</a>
</div>
<!--列表显示功能-->
<table id="tbl_fruit">
<tr>
<th class="w20">名称</th>
<th class="w20">单价</th>
<th class="w20">库存</th>
<th>操作</th>
</tr>
<tr th:if="${#lists.isEmpty(session.fruitList)}">
<td colspan="4">对不起,库存为空!</td>
</tr>
<tr th:unless="${#lists.isEmpty(session.fruitList)}" th:each="fruit : ${session.fruitList}">
<!--<td><a th:text="${fruit.fname}" th:href="@{'/edit.do?fid=' + ${fruit.fid}}"> 苹果 </a></td>
-->
<td><a th:text="${fruit.fname}" th:href="@{/edit.do(fid=${fruit.fid})}"> 苹果 </a></td>
<td th:text="${fruit.price}">5</td>
<td th:text="${fruit.fcount}">20</td>
<!--<td><img src="imgs/deleteIcon.jpg" class="deleteImg" th:οnclick="'delFruit('+${fruit.fid}+')'"/></td>
-->
<td><img src="imgs/deleteIcon.jpg" class="deleteImg" th:onclick="|delFruit(${fruit.fid})|"/></td>
</tr>
</table>
<!--分页功能-->
<div style="width:60%;margin-left:20%;padding-top: 4px;" class="center">
<input type="button" value="首 页" class="btn" th:onclick="|page(1)|" th:disabled="${session.pageNum==1}">
<input type="button" value="上一页" class="btn" th:onclick="|page(${session.pageNum-1})|" th:disabled="${session.pageNum==1}">
<input type="button" value="下一页" class="btn" th:onclick="|page(${session.pageNum+1})|" th:disabled="${session.pageNum==session.pageCount}">
<input type="button" value="尾 页" class="btn" th:onclick="|page(${session.pageCount})|" th:disabled="${session.pageNum==session.pageCount}">
</div>
</div>
</div>
</body>
</html>
【indexServlet.java】
package com.fruit.servlets;
import com.fruit.dao.FruitDAO;
import com.fruit.dao.impl.FruitDAOImpl;
import com.fruit.myssm.myspingmvc.ViewBaseServlet;
import com.fruit.pojo.Fruit;
import com.fruit.util.StringUtil;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
//servlet从3.0版本开始支持注解方式的注册
@WebServlet("/index")
public class indexServlet extends ViewBaseServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
HttpSession session = request.getSession();
//设置当前页:默认值为1
int pageNum = 1;
//
String operate = request.getParameter("operate");
//如果operate!=null,说明:通过表单的查询按钮点击过来的
//如果operate==null,说明:不是通过表单的查询按钮点击过来的
String keyword = null;
if (StringUtil.isNotEmpty(operate) && "search".equals(operate)) {
//说明是点击表单查询发送过来的请求
//此时,pageNum应该还原为1,keyword应该从请求参数中获取
pageNum = 1;
keyword = request.getParameter("keyword");
//如果keyword为null,需要设置为"",否则查询时会拼接成 %null%,我们期望的是 %%
if (StringUtil.isEmpty(keyword)) {
keyword = "";
}
//将keyword把偶才能(覆盖)到session中
session.setAttribute("keyword",keyword);
}else {
//说明此处不是是点击表单查询发送过来的请求,比如点击上一页,下一页或者直接在地址栏输入网址
//此时keyword 应该从session的作用域获取
String pageNumStr = request.getParameter("pageNum");
if(StringUtil.isNotEmpty(pageNumStr)) {
//如果从请求中读取到pageNum,则类型转换,否则默认为1
pageNum = Integer.parseInt(pageNumStr);
}
//如果不是点击的查询按钮,那么查询是基于session中保存的现有keyword进行查询
Object keywordObj = session.getAttribute("keyword");
if (keywordObj != null) {
keyword = (String) keywordObj;
}else {
keyword = "";
}
}
//重新更新当前页的值
session.setAttribute("pageNum",pageNum);
FruitDAO fruitDAO = new FruitDAOImpl();
//List<Fruit> fruitList = fruitDAO.getFruitList();
List<Fruit> fruitList = fruitDAO.getFruitList(keyword,pageNum);
// System.out.println(fruitList);
//保存到session作用域
session.setAttribute("fruitList", fruitList);
int count = fruitDAO.getFruitCount(keyword);
int pageCount = (count + 5 - 1) / 5;
session.setAttribute("pageCount", pageCount);
super.processTemplate("index",request,response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request,response);
}
}
【sql语句】
String sql = "select * from fruit where fname like ? or remark like ? limit ?,5";
String sql = "select count(*) from fruit where fname like ? or remark like ?";