首页 > 基础资料 博客日记
JavaWeb学习-关键字查询
2023-07-24 17:10:58基础资料围观218次
本篇文章分享JavaWeb学习-关键字查询,对你有帮助的话记得收藏一下,看Java资料网收获更多编程知识
1. index.html
<div>
<form th:action="@{/index}" method="post">
<input type="hidden" name="oper" value="search" />
请输入查询关键字:<input type="text" name="keyword" th:value="${session.keyword}"/>
<input type="submit" value="查询"/>
</form>
<a th:href="@{/add.html}">添加信息</a>
</div>
2. IndexServlet
package com.fruits.servelet;
import com.fruits.dao.FruitDAO;
import com.fruits.dao.Impl.FruitDAOImpl;
import com.fruits.pojo.Fruit;
import com.fruits.util.StringUtil;
import com.myssm.myspringmvc.ViewBaseServlet;
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;
@WebServlet("/index")
public class IndexServlet extends ViewBaseServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req,resp);
}
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
HttpSession session=request.getSession();
int pageNo = 1;
String oper = request.getParameter("oper");
//如果oper不是空的,则是通过表单的查询按钮点击过来的,
//如果oper是空的,说明不是通过表单的查询按钮点击过来的,
// 因为index文件可以通过点击分页按钮进入,增删改后也可以进入。
String keyword = null;
if(StringUtil.isNotEmpty(oper)&&"search".equals(oper)){
//是通过表单的查询按钮点击过来的,
//此时,pageNo应该还原为 1 , keyword应该从请求参数中获取
pageNo=1;
keyword = request.getParameter("keyword");
if(StringUtil.isEmpty(keyword)){
keyword = "";
}
session.setAttribute("keyword",keyword);
}else{
//说明不是通过表单的查询按钮点击过来的,(比如点击上一页下一页或者直接在地址栏中输入网址 )
//此时keyword应该从session作用域中获取
String pageNoStr = request.getParameter("pageNo");
if(StringUtil.isNotEmpty(pageNoStr)){
pageNo = Integer.parseInt(pageNoStr);
}
Object keywordobj = session.getAttribute("keyword");
if(keywordobj!=null){
keyword = (String)keywordobj;
}else{
keyword="";
}
}
session.setAttribute("pageNo",pageNo);
FruitDAO fruitDAO=new FruitDAOImpl();
//List<Fruit> fruitList = fruitDAO.getFruitList();
//List<Fruit> fruitList = fruitDAO.getFruitListbypageNo(pageNo);
List<Fruit> fruitList = fruitDAO.getListbypageNo(keyword, pageNo);
session.setAttribute("fruitList", fruitList);
//总记录条数
// int count = fruitDAO.getFruitCount();
int count = fruitDAO.getCount(keyword);
//总页数
int pageCount = count/5+1;
session.setAttribute("pageCount", pageCount);
//此处的视图名称是index,
//那么thymeleaf会将这个 逻辑视图名称 对应到 物理视图 名称上去
//逻辑视图名称:index
//物理视图名称: view-prefix+逻辑视图名称+view-suffix
//所以真是的视图名称为 / index. html
super.processTemplate("index",request,response);
}
3.FruitDAO和FruitDAOImpl
//关键字查询且分页
List<Fruit> getListbypageNo(String keyword,int pageNo);
//获取库存总页数+关键字查询
int getCount(String keyword);
public List<Fruit> getListbypageNo(String keyword, int pageNo) {
List<Fruit> list = new ArrayList<>();
getConn();
String sql = "select * from fruit where fname like ? or remark like ? LIMIT ?,5";
try {
ps = conn.prepareStatement(sql);
ps.setString(1,"%"+keyword+"%");
ps.setString(2,"%"+keyword+"%");
ps.setInt(3,(pageNo-1)*5);
rs = ps.executeQuery();
while (rs.next()){
Fruit fruit = new Fruit();
fruit.setFid(rs.getInt(1));
fruit.setFname(rs.getString(2));
fruit.setPrice(rs.getInt(3));
fruit.setFcount(rs.getInt(4));
fruit.setRemark(rs.getString(5));
list.add(fruit);
}
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
@Override
public int getCount(String keyword) {
int count = 0;
String sql = "select * from fruit where fname like ? or remark like ?";
getConn();
try {
ps = conn.prepareStatement(sql);
ps.setString(1,"%"+keyword+"%");
ps.setString(2,"%"+keyword+"%");
rs = ps.executeQuery();
while (rs.next()){
count++;
}
} catch (SQLException e) {
e.printStackTrace();
}
return count;
}
文章来源:https://www.cnblogs.com/Joyce-mi7/p/16494487.html
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:jacktools123@163.com进行投诉反馈,一经查实,立即删除!
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:jacktools123@163.com进行投诉反馈,一经查实,立即删除!
标签: