下拉自动刷新数据

当有多条信息需要在前端展示的时候,通常的做法是做成翻页列表的形式,下拉自动刷新是翻页显示的一种,但是相较于传统的方式要更加人性化,所以今天就实现了一下下拉自动刷新的翻页形式。

 直接上代码:

<%@page import="java.util.List"%>
<%@page import="java.util.ArrayList"%>
<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>




Insert title here
 
   




后端处理servlet:

package com.js.test;


import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import net.sf.json.JSONObject;




public class NewsList extends HttpServlet implements ServletContextListener{

private static final long serialVersionUID = 1L;

@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}


@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String str=request.getServletPath();
int index=str.lastIndexOf("/");
int end=str.lastIndexOf(".");
String methodname=str.substring(index+1, end);
Class cla=this.getClass();
Method[] method=cla.getMethods();
for(Method m:method)
{
if(m.getName().equals(methodname))
{
try {
m.invoke(this, request,response);
} catch (Exception e) {
System.out.println("调用错误");
}
}
}
}

public void updatemessage(HttpServletRequest request,HttpServletResponse response) throws IOException
{
int currentpage=Integer.parseInt(request.getParameter("currentpage"));
int start=currentpage*100;
List page_list=new ArrayList();
Map> info=new HashMap>();
@SuppressWarnings("unchecked")
List message=(List) request.getServletContext().getAttribute("message");
if(message==null)
{
page_list.add("获取数据失败");
}else if(currentpage==10){
page_list.add("没有更多数据啦");
}else
{
page_list=message.subList(start, start+100);
}
info.put("message", page_list);
JSONObject array=JSONObject.fromMap(info);
response.setContentType("application/json");
response.setCharacterEncoding("utf-8");
PrintWriter out= response.getWriter();
out.print(array);
out.flush();
}

    
public void Ready_message(ServletContext application)
{
List news=new ArrayList();
for(int i=0;i<1000;i++)
{
String str="这是第"+i+"条信息";
news.add(str);
}
application.setAttribute("message", news);
}


@Override
public void contextDestroyed(ServletContextEvent arg0) {
System.out.println("应用关闭");
}


@Override
public void contextInitialized(ServletContextEvent arg0) {
Ready_message(arg0.getServletContext());
System.out.println("应用创建完成");


}






}
由于没有从数据库中查询数据,所以这儿在应用启动的时候,先创建了一下数据,后面每次的翻页请求都从这些数据中获取。

你可能感兴趣的:(java动态,java,分页)