Jsp +Js + Jquery + EasyUI + Servlet + Lucene,完成分页
package loaderman.fy.action; import java.io.IOException;
import java.io.PrintWriter;
import java.util.LinkedHashMap;
import java.util.Map; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import loaderman.fy.entity.Page;
import loaderman.fy.service.ArticleService;
import net.sf.json.JSONArray; public class ArticleServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
this.doPost(request,response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {
try { request.setCharacterEncoding("UTF-8"); //获取关键字
String keywords = request.getParameter("keywords");//培训
if(keywords == null || keywords.trim().length()==0){
keywords = "培训";//默认值
} //获取当前页号
String temp = request.getParameter("page");//核心
if(temp == null || temp.trim().length()==0){
temp = "1";//默认值
} //调用业务层
ArticleService articleService = new ArticleService();
Page page = articleService.show(keywords,Integer.parseInt(temp)); //构造Map对象
Map<String,Object> map = new LinkedHashMap<String,Object>();
map.put("total",page.getAllRecordNO());
map.put("rows",page.getArticleList()); //第三方工具将Map转成JSON
JSONArray jsonArray = JSONArray.fromObject(map);
String jsonJAVA = jsonArray.toString(); //去掉二边的[]符号
jsonJAVA = jsonJAVA.substring(1,jsonJAVA.length()-1); //以IO的流方式响应到DataGrid组件
response.setContentType("text/html;charset=UTF-8");
PrintWriter pw = response.getWriter();
pw.write(jsonJAVA);
pw.flush();
pw.close();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}
package loaderman.fy.dao; import java.util.ArrayList;
import java.util.List; import loaderman.fy.entity.Article;
import loaderman.util.LuceneUtil;
import org.apache.lucene.document.Document;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs; /**
* 持久层 */
public class ArticleDao {
/**
* 根据关键字,获取总记录数
* @return 总记录数
*/
public int getAllRecord(String keywords) throws Exception{
QueryParser queryParser = new QueryParser(LuceneUtil.getVersion(),"content",LuceneUtil.getAnalyzer());
Query query = queryParser.parse(keywords);
IndexSearcher indexSearcher = new IndexSearcher(LuceneUtil.getDirectory());
TopDocs topDocs = indexSearcher.search(query,2);
//返回符合条件的真实总记录数,不受2的影响
return topDocs.totalHits;
//返回符合条件的总记录数,受2的影响
//return topDocs.scoreDocs.length;
}
/**
* 根据关键字,批量查询记录
* @param start 从第几条记录的索引号开始查询,索引号从0开始
* @param size 最多查询几条记录,不满足最多数目时,以实际为准
* @return 集合
*/
public List<Article> findAll(String keywords,int start,int size) throws Exception{
List<Article> articleList = new ArrayList<Article>(); QueryParser queryParser = new QueryParser(LuceneUtil.getVersion(),"content",LuceneUtil.getAnalyzer());
Query query = queryParser.parse(keywords);
IndexSearcher indexSearcher = new IndexSearcher(LuceneUtil.getDirectory());
TopDocs topDocs = indexSearcher.search(query,100);
//小技巧
int middle = Math.min(start+size,topDocs.totalHits);
for(int i=start;i<middle;i++){
ScoreDoc scoreDoc = topDocs.scoreDocs[i];
int no = scoreDoc.doc;
Document document = indexSearcher.doc(no);
Article article = (Article) LuceneUtil.document2javabean(document,Article.class);
articleList.add(article);
} return articleList;
} public static void main(String[] args) throws Exception{
ArticleDao dao = new ArticleDao();
System.out.println(dao.getAllRecord("学习")); System.out.println("------------------------------"); System.out.println("第一页");
List<Article> list = dao.findAll("学习",0,2);
for(Article a : list){
System.out.println(a);
}
System.out.println("第二页");
list = dao.findAll("学习",2,2);
for(Article a : list){
System.out.println(a);
}
System.out.println("第三页");
list = dao.findAll("学习",4,2);
for(Article a : list){
System.out.println(a);
}
System.out.println("第四页");
list = dao.findAll("学习",6,2);
for(Article a : list){
System.out.println(a);
}
}
}
package loaderman.fy.entity; /**
* 文章
*/
public class Article {
private Integer id;//编号
private String title;//标题
private String content;//内容
public Article(){}
public Article(Integer id, String title, String content) {
this.id = id;
this.title = title;
this.content = content;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
@Override
public String toString() {
return "编号:" + id+"\n标题:" + title + "\n内容:" + content;
}
}
package loaderman.fy.entity; import java.util.ArrayList;
import java.util.List; /**
* 用于Article的分页类
*/
public class Page {
private Integer currPageNO;//当前页号OK
private Integer perPageSize = 2;//每页显示记录数,默认为2条OK
private Integer allRecordNO;//总记录数OK
private Integer allPageNO;//总页数OK
private List<Article> articleList = new ArrayList<Article>();//内容OK
public Page(){}
public Integer getCurrPageNO() {
return currPageNO;
}
public void setCurrPageNO(Integer currPageNO) {
this.currPageNO = currPageNO;
}
public Integer getPerPageSize() {
return perPageSize;
}
public void setPerPageSize(Integer perPageSize) {
this.perPageSize = perPageSize;
}
public Integer getAllRecordNO() {
return allRecordNO;
}
public void setAllRecordNO(Integer allRecordNO) {
this.allRecordNO = allRecordNO;
}
public Integer getAllPageNO() {
return allPageNO;
}
public void setAllPageNO(Integer allPageNO) {
this.allPageNO = allPageNO;
}
public List<Article> getArticleList() {
return articleList;
}
public void setArticleList(List<Article> articleList) {
this.articleList = articleList;
}
}
package loaderman.fy.service; import loaderman.fy.dao.ArticleDao;
import loaderman.fy.entity.Article;
import loaderman.fy.entity.Page; import java.util.List; /**
* 业务层
*/
public class ArticleService {
//持久层
private ArticleDao articleDao = new ArticleDao();
/**
* 根据关键字和页号,查询内容
*/
public Page show(String keywords,int currPageNO) throws Exception{
Page page = new Page(); //封装当前页号
page.setCurrPageNO(currPageNO); //封装总记录数
int allRecordNO = articleDao.getAllRecord(keywords);
page.setAllRecordNO(allRecordNO); //封装总页数
int allPageNO = 0;
if(page.getAllRecordNO() % page.getPerPageSize() == 0){
allPageNO = page.getAllRecordNO() / page.getPerPageSize();
}else{
allPageNO = page.getAllRecordNO() / page.getPerPageSize() + 1;
}
page.setAllPageNO(allPageNO); //封装内容
int size = page.getPerPageSize();
int start = (page.getCurrPageNO()-1) * size;
List<Article> articleList = articleDao.findAll(keywords,start,size);
page.setArticleList(articleList); return page;
} //测试
public static void main(String[] args) throws Exception{
ArticleService test = new ArticleService();
Page page = test.show("学习",4); System.out.println(page.getCurrPageNO());
System.out.println(page.getPerPageSize());
System.out.println(page.getAllRecordNO());
System.out.println(page.getAllPageNO());
for(Article a : page.getArticleList()){
System.out.println(a);
}
}
}
<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>使用Jsp +Js + Jquery + EasyUI + Servlet + Lucene,完成分页</title>
<!-- 引入css文件,无顺序 -->
<link rel="stylesheet" href="themes/icon.css" type="text/css"></link>
<link rel="stylesheet" href="themes/default/easyui.css" type="text/css"></link>
<!-- 引入js文件,有顺序 -->
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/jquery.easyui.min.js"></script>
<script type="text/javascript" src="js/easyui-lang-zh_CN.js"></script>
</head>
<body> <!-- 输入区 -->
<form id="myformID">
输入关键字:<input type="text" value="" id="keywordID"/>
<input type="button" value="站内搜索" id="findID"/>
</form>
<script type="text/javascript">
//定位"站内搜索"按钮
$("#findID").click(function(){
//获取关键字
var keyword = $("#keywordID").val();
//去空格
keyword = $.trim(keyword);
//判断
if(keyword.length == 0){
//提示
alert("请输入关键字!!!");
//清空文本框的内容
$("#keywordID").val("");
//定位于输入关键字文本框
$("#keywordID").focus();
}else{
//异步发送请求到服务器
//load表示方法名
//"keywords"表示需要发送的的参数名,后台收:request.getParameter("keywords")
//keyword表示参数值
$("#dg").datagrid("load",{
"keywords" : keyword
});
}
});
</script> <!-- 显示区 -->
<table id="dg"></table>
<script type="text/javascript">
$("#dg").datagrid({
url : "${pageContext.request.contextPath}/ArticleServlet?time="+new Date().getTime(),
columns : [[
{field:'id',title:'编号',width:100},
{field:'title',title:'标题',width:100},
{field:'content',title:'内容',width:100}
]],
fitColumns : true,
singleSelect : true,
pagination : true,
pageSize : 2,
pageList : [2]
});
</script> </body>
</html>
Jsp +Js + Jquery + EasyUI + Servlet + Lucene,完成分页的更多相关文章
- 使用Jsp +Js + Jquery + EasyUI + Servlet + Lucene,完成分页
步一:创建ArticleDao.java类 public class ArticleDao { public Integer getAllObjectNum(String keywords) thro ...
- jQuery EasyUI datagrid实现本地分页的方法
http://www.codeweblog.com/jquery-easyui-datagrid%e5%ae%9e%e7%8e%b0%e6%9c%ac%e5%9c%b0%e5%88%86%e9%a1% ...
- 雷林鹏分享:jQuery EasyUI 数据网格 - 自定义分页
jQuery EasyUI 数据网格 - 自定义分页 数据网格(datagrid)内置一个很好特性的分页功能,自定义也相当简单.在本教程中,我们将创建一个数据网格(datagrid),并在分页工具栏上 ...
- 雷林鹏分享:jQuery EasyUI 数据网格 - 添加分页组件
jQuery EasyUI 数据网格 - 添加分页组件 本实例演示如何从服务器端加载数据,如何添加分页组件(pagination)到数据网格(datagrid). 创建数据网格(DataGrid) 为 ...
- 套用JQuery EasyUI列表显示数据、分页、查询
声明,本博客从csdn搬到cnblogs博客园了,以前的csdn不再更新,朋友们可以到这儿来找我的文章,更多的文章会发表,谢谢关注! 有时候闲的无聊,看到extjs那么肥大,真想把自己的项目改了,最近 ...
- ASP.NET网站权限设计实现(三)——套用JQuery EasyUI列表显示数据、分页、查询
一.说明: JQuery EasyUI下载地址:http://jquery-easyui.wikidot.com/download,最新版本1.2.2. 首先预览一下界面: 本例实现的功能: 1.多标 ...
- [js]jQuery EasyUI的linkbutton组件disable方法无法禁用jQuery绑定事件的问题分析
问题由来 linkbutton 是 jQuery EasyUI 中常用的一个控件,可以使用它创建按钮.用法很简单,使用 a 标签给一个easyui-linkbutton 的class就可以了. < ...
- 使用Jsp/Js/Ajax/Json/Jquery/Easyui + Servlet + JDBC + Lucene/Mysql/Oracle完成数据库分页
package loaderman.action; import java.io.IOException; import java.io.PrintWriter; import java.util.L ...
- jsp、jQuery、servlet交互实现登录功能
做一个web项目,往往需要有一个登录模块,验证用户名和密码之后跳转页面.为了实现更好的交互,往往需要用到 jQuery 等实现一些友好提示.比如用户名或者密码输入为空的时候提示不能为空:点击提交的时候 ...
随机推荐
- 对称加密实现重要日志上报Openresty接口服务
记录后端接收日志的流程: 由于记录的是广告数据,单次计费数据都会上报,全国内约10几万终端上报. 终端上报:Android电视端Apk上报 接收终端:Openresty(Nginx+lua)利用ngi ...
- go mod 解决 Go 语言的包依赖问题
转:https://testerhome.com/topics/16980 https://testerhome.com/ -------------------------------------- ...
- C语言编译错误:Variably modified array at file scope
今天在编译一段C源程序时,遇到编译错误提示 error: variably modified 'data' at file scope.原因在于代码头部有这样几行: +; int data[maxsi ...
- Java中的集合List、ArrayList、Vector、Stack(三)
List接口 List集合代表一个有序集合,集合中每一个元素都有其对应的顺序索引.List集合容许使用重复元素,可以通过索引来访问指定位置的集合对象. ArrayList和Vector实现类 Arra ...
- MySql链接url参数的设置
mysql JDBC URL格式如下: jdbc:mysql://[host:port],[host:port].../[database][?参数名1][=参数值1][&参数名2][=参数值 ...
- 设置springboot、mysql、nginx,tomcat文件大小(大集合)
1.springboot设置文件大小 第一种: application.properties中添加 spring.http.multipart.maxFileSize=10MBspring.http. ...
- curl查看http请求消息的时长
1. -X 指定请求方式GET请求curl -X GET http://www.jackyops.com/search?data=123 # -X GET是可选的 POST请求curl -X POS ...
- [Spring Boot] Set Context path for application in application.properties
If you were using Microservice with Spring Boot to build different REST API endpoints, context path ...
- 【leetcode】1288. Remove Covered Intervals
题目如下: Given a list of intervals, remove all intervals that are covered by another interval in the li ...
- JPA规则