Java map集合实现上一条(篇),下一条(篇)新闻翻篇功能
程序员文章站
2022-04-16 08:51:49
...
传统实现新闻消息的上一条和下一条功能均使用list集合即可,【即:将数据id保存在list集合中,点击上一条时将id减1,点击下一条时id加1】,此时实现传统的上一条与下一条功能是没有问题的。
但是对于一些特殊的显示功能就无法实现。例如:在数据库中存储了365天每天发生的事情(多件事)如图1(字段分别为id,year,month,day,title),在页面上显示对应于今天发生的事情,此时使用list实现翻篇还是可以的,因为id是连续的,但是如果像数据库中插入一条今天发生的新事件,此时id就会与上边不连续,无法使用list实现翻篇,此时就可以使用map实现。将取出的id作为value,int类型的i作为key,从而实现重新排序。
图1
整体结构如下:
此功能实现如下过程:
通过某一天的时间获取今日事件servlet---GetInfo ,用于index.jsp显示title
response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("UTF-8");
Calendar c = Calendar.getInstance();
int month1 = c.get(Calendar.MONTH); //获取当前服务器时间--月
int day1 = c.get(Calendar.DATE);//获取当前服务器时间--日
int type = 0;
if(month1<12){
month1=month1+1;
}else{
month1=12;
}
String month = String.valueOf(month1);
String day = String.valueOf(day1);
Map<Integer,Object> Scimap = Sci.getHtodayMap(month,day);
request.getSession().setAttribute("Scimap", Scimap);
request.getRequestDispatcher("index.jsp").forward(request, response);
获取数据的sql----getHtodayMap(month,day)
public static Map<Integer, Object> getHtodayMap(String month, String day) {
Map<Integer, Object> m1 = new HashMap<Integer, Object>();
PreparedStatement pstmt = null;
ResultSet rs = null;
Connection conn = null;
String sql = "select * from sci where month='"+month+"' and day='"+day+"'";
int i = 0;
try {
conn = DB.getConn();
pstmt = DB.getPStmt(conn, sql);
rs = pstmt.executeQuery();
while(rs.next()){
i++;
Sci s = new Sci();
s.setId(rs.getInt("id"));
s.setYear(rs.getString("year"));
s.setMonth(rs.getString("month"));
s.setDay(rs.getString("day"));
s.setTitle(rs.getString("title"));
s.setContent(rs.getString("content"));
m1.put(i,s);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DB.closeRs(rs);
DB.closeStmt(pstmt);
DB.closeConn(conn);
}
return m1;
}
显示页面index.jsp <c:forEach items="${sessionScope.Scimap}" var="Sci">
<li><span class="fl" id="data">${Sci.value.year}-${Sci.value.month}-${Sci.value.day}</span>
<a href="GetHtodayInfo?id=${Sci.value.id}&key=${Sci.key}" target="_blank" class="tit1">${Sci.value.title}</a></li>
</c:forEach>
页面效果:
点击index.jsp页面显示部分,跳转网址如下,进入内容显示详情(servlet内容见下):
点击上一条id为,跳转网址为:
获取index.jsp点击显示内容及上一条下一条点击信息的servlet----GetHtodayInfo:
response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("UTF-8");
int id1 = Integer.parseInt(request.getParameter("id"));//用于显示本条信息使用,即由index.jsp跳转到htoday.jsp显示内容页面时,取出本id对应内容
int key = Integer.parseInt(request.getParameter("key"));//用于判断map中存储的今日事件的条数
String flag = request.getParameter("flag");//上一条或者下一条的标志
Calendar c = Calendar.getInstance();//获取系统时间
int month1 = c.get(Calendar.MONTH);
int day1 = c.get(Calendar.DATE);
int id = 0;
if(month1<12){
month1=month1+1;
}else{
month1=12;
}
String month = String.valueOf(month1);
String day = String.valueOf(day1);
Map<Integer,Integer> idmap = Sci.getHtodayIdMap(month, day);//获取时间对应时间,并存入map
if(flag=="1" || "1".equals(flag)){//flag=1表示上一条
key=key-1;
if(idmap.containsKey(key)){//判断减一以后是否还存在于map中,若存在则执行以下内容
id = idmap.get(key);//在map中,根据key获取value,即根据key获取id
Sci s = Sci.getinfo(id);//根据id获取数据库详细内容
request.getSession().setAttribute("s", s);//存入session
request.getSession().setAttribute("key", key);
request.getRequestDispatcher("htoday.jsp").forward(request, response);//请求转发到显示页面
}else{//若key不存在于map中,则:
key=idmap.size();//获取map最大值
id = idmap.get(key);//从id最小跳到id最大,实现循环
Sci s = Sci.getinfo(id);//根据id获取内容
request.getSession().setAttribute("s", s);
request.getSession().setAttribute("key", key);
request.getRequestDispatcher("htoday.jsp").forward(request, response);
}
}else if(flag=="2" || "2".equals(flag)){//flag=2表示下一条
key=key+1;
if(idmap.containsKey(key)){
id = idmap.get(key);
Sci s = Sci.getinfo(id);
request.getSession().setAttribute("s", s);
request.getSession().setAttribute("key", key);
request.getRequestDispatcher("htoday.jsp").forward(request, response);
}else{
key=1;
id=idmap.get(key);
Sci s = Sci.getinfo(id);
request.getSession().setAttribute("s", s);
request.getSession().setAttribute("key", key);
request.getRequestDispatcher("htoday.jsp").forward(request, response);
}
}else{
Sci s = Sci.getinfo(id1);//由index.jsp跳转到htoday.jsp显示内容页面时,取出本id对应内容
request.getSession().setAttribute("s", s);
request.getSession().setAttribute("key", key);
request.getRequestDispatcher("htoday.jsp").forward(request, response);
}
根据id获取内容的方法---getinfp(id)
public static Sci getinfo(int id) {
PreparedStatement pstmt = null;
ResultSet rs = null;
Connection conn = null;
String sql = "select * from sci where id='"+id+"'";
Sci s = new Sci();
try {
conn = DB.getConn();
pstmt = DB.getPStmt(conn, sql);
rs = pstmt.executeQuery();
while(rs.next()){
s.setId(rs.getInt("id"));
s.setYear(rs.getString("year"));
s.setMonth(rs.getString("month"));
s.setDay(rs.getString("day"));
s.setTitle(rs.getString("title"));
s.setContent(rs.getString("content"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DB.closeRs(rs);
DB.closeStmt(pstmt);
DB.closeConn(conn);
}
return s;
}
显示部分htoday.jsp