模糊查询和分页功能
需要注意的是这里前端使用了thymeleaf模板,后端获取前端的参数,从定义方法到实现方法再再servlet中调用,其中servlet中调用了前文所写的一个判断是否为空的函数StringUtil,也可以写在servlet里(只是这里为稍作简化)
index.html
<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="oper" 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>
<div style="width: 60%;margin-left: 20%; padding-top: 6px;" class="center">
<input type="button" value="首 页" class="btn" th:onclick="|page(1)|" th:disabled="${session.pageNo==1}"/>
<input type="button" value="上一页" class="btn" th:onclick="|page(${session.pageNo-1})|" th:disabled="${session.pageNo==1}"/>
<input type="button" value="下一页" class="btn" th:onclick="|page(${session.pageNo+1})|" th:disabled="${session.pageNo==session.pageCount}"/>
<input type="button" value="尾 页" class="btn" th:onclick="|page(${session.pageCount})|" th:disabled="${session.pageNo==session.pageCount}"/>
</div>
FruitDAO
public interface FruitDAO {
//获取指定页码上的所有的库存列表信息,每页显示5条
List<Fruit> getFruitList(String keyword,Integer pageNo);
//查询库存总计录条目
int getFruitCount(String keyword);
}
FruitDAOImpl
public class FruitDAOImpl extends BaseDAO<Fruit> implements FruitDAO {
//获取所有的库存列表信息
@Override
public List<Fruit> getFruitList(String keyword, Integer pageNo) {
return super.executeQuery("select * from t_fruit where fname like ? or remark like ? limit ? , 5","%"+keyword+"%","%"+keyword+"%",(pageNo-1)*5);
}
//查询库存总计录条目
@Override
public int getFruitCount(String keyword) {
return ((Long)super.executeComplexQuery("select count(*) from t_fruit where fname like ? or remark like ?","%"+keyword+"%","%"+keyword+"%")[0]).intValue();
}
}
IndexServlet
//servlet从3.0开始支持注解方式的注册
@WebServlet("/index")
public class IndexServlet extends ViewBaseServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req,resp);
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//设置编码
req.setCharacterEncoding("UTF-8");
//获取当前的会话session
HttpSession session = req.getSession();
//设置当前页默认值为1
Integer pageNo = 1;
//获取oper的值
String oper = req.getParameter("oper");
//若oper!=null则说明通过表单的查询按钮过来的,反之如果oper为空,则不是通过查询过来的
String keyword = null;
//这里的if语句:如果oper不为空,且它的值等于search
if (StringUtil.isNotEmpty(oper) && "search".equals(oper)){
//说明此处是点击表单查询发送过来的信息
pageNo = 1;
keyword = req.getParameter("keyword");
//若keyword为空这里把它设置为空字符串"",否则查询时会拼接成%null%
if (StringUtil.isEmpty(keyword)){
keyword = "";
}
//将keyword保存到session
session.setAttribute("keyword",keyword);
} else {
//说明此处不是点击表单查询发送过来的信息(比如点击上一页,下一页)
//此时keyword应该从session作用域中获得
String pageNoStr = req.getParameter("pageNo");
if(StringUtil.isNotEmpty(pageNoStr)){
pageNo = Integer.parseInt(pageNoStr);
}
//如果不是点击查询按钮,那么查询是基于session中保存的现有keyword进行查询
Object keywordObj = session.getAttribute("keyword");
if (keywordObj != null){
keyword = (String)keywordObj;
}else {
keyword = "";
}
}
//更新当前页的值
session.setAttribute("pageNo",pageNo);
//获取库存列表,保存到session中
FruitDAO fruitDAO = new FruitDAOImpl();
List<Fruit> fruitList = fruitDAO.getFruitList(keyword,pageNo);
//保存到session作用域
session.setAttribute("fruitList",fruitList);
//获取总记录条数
int fruitCount = fruitDAO.getFruitCount(keyword);
//通过记录条数获取页数
int pageCount = (fruitCount+5-1)/5;
session.setAttribute("pageCount",pageCount);
//此处的视图名称是index,那么thymeleaf会讲这个逻辑视图名称对应到物理视图上去
super.processTemplate("index",req,resp);
}
}