JSP页面点击“上移下移”链接排序

来源:互联网 发布:2015年进出口贸易数据 编辑:程序博客网 时间:2024/06/05 16:51



index.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"    pageEncoding="utf-8"%>    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="hoo" %><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>排序</title></head><body><form action="" method=post><table border=1><hoo:forEach items="${requestScope.al }" var="user"><tr><td>${user.id }</td><td>${user.name }</td><td><a href=SortServlet?type=up&id=${user.id }>上移</a></td><td><a href=SortServlet?type=down&id=${user.id }>下移</a></td></tr></hoo:forEach></table></form></body></html>
IndexServlet.java
package com.controller;import java.io.IOException;import java.util.ArrayList;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 com.domain.User;import com.service.HuService;@WebServlet("/IndexServlet")public class IndexServlet extends HttpServlet {protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// TODO Auto-generated method stubthis.doPost(request, response);}protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// TODO Auto-generated method stubHuService hs=new HuService();request.getSession().setAttribute("hs", hs);ArrayList<User> al=(ArrayList<User>) hs.getList();request.setAttribute("al", al);request.getRequestDispatcher("index.jsp").forward(request, response);}}
SortServlet.java
package com.controller;import java.io.IOException;import java.util.ArrayList;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 com.domain.User;import com.service.HuService;@WebServlet("/SortServlet")public class SortServlet extends HttpServlet {private static final long serialVersionUID = 1L;    public SortServlet() {        super();        // TODO Auto-generated constructor stub    }protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// TODO Auto-generated method stubthis.doPost(request, response);}protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {String type=request.getParameter("type");String id=request.getParameter("id");HuService hs=(HuService) request.getSession().getAttribute("hs");hs.sort(type, id);ArrayList<User> al=(ArrayList<User>) hs.getList();request.setAttribute("al", al);request.getRequestDispatcher("index.jsp").forward(request, response);}}


HuService.java

package com.service;import java.util.*;import com.domain.User;public class HuService {private ArrayList<User> list;public HuService(){list=new ArrayList<>();list.add(new User("1","疯狂JAVA"));list.add(new User("2","百度"));list.add(new User("3","谷歌"));list.add(new User("4","腾讯"));list.add(new User("5","新浪"));list.add(new User("6","网易"));list.add(new User("7","搜狐"));}public List<User> getList(){return list;}public void sort(String type,String id){if(type.equals("up")){for(int i=0;i<list.size();i++){User user=list.get(i);if(user.getId().equals(id)){if((i-1)>=0){User u=list.get(i-1);System.out.println(u.getName());user.setId(u.getId());u.setId(id);}break;}}}else if(type.equals("down")){for(int i=0;i<list.size();i++){User user=list.get(i);if(user.getId().equals(id)){if((i+1)<=(list.size()-1)){User u=list.get(i+1);user.setId(u.getId());u.setId(id);}break;}}}Collections.sort(this.getList(), new HuComparator());}}class HuComparator implements Comparator<User>{@Overridepublic int compare(User o1, User o2) {int result = o1.getId().compareTo(o2.getId());return result;}}