日期:2014-05-16  浏览次数:20343 次

使用JS提交可以翻页的列表

Action:

public class JavaScriptArrayAction extends DispatchAction{

    public ActionForward search(ActionMapping mapping, ActionForm form, HttpServletRequest request,
     HttpServletResponse response) {
 HttpSession session = request.getSession(true);
 JavaScriptArrayForm javaScriptArrayForm = (JavaScriptArrayForm)form;
 List<String> data = (List<String>)session.getAttribute("data");

 int current = javaScriptArrayForm.getCurrent() - 1;
 if(current > -1 && current < 10){
     List<String> l = data.subList(current++ * 10,current * 10);
     javaScriptArrayForm.setData(l);
     String allJSON = javaScriptArrayForm.getAllJSON().trim();

     List<String> names = new ArrayList<String>(l.size());

     if(null != allJSON && allJSON.length() > 0){
  List<String> allList = Arrays.asList(allJSON.substring(1,allJSON.length() - 1).split(","));
  for(int i = 0; i < l.size(); i++){
      names.add(i,allList.contains(l.get(i)) ? l.get(i) : "");
  }
  javaScriptArrayForm.setCurJSON(listToJSON(names));
     }else{
  javaScriptArrayForm.setAllJSON("[]");
  javaScriptArrayForm.setCurJSON("[]");
     }

     String[] ss = new String[names.size()];
     names.toArray(ss);
     javaScriptArrayForm.setName(ss);
 }
 
 return mapping.getInputForward();
    }

    @Override
    protected ActionForward unspecified(ActionMapping mapping, ActionForm form, HttpServletRequest request,
     HttpServletResponse response) throws Exception {
 HttpSession session = request.getSession(true);
 JavaScriptArrayForm javaScriptArrayForm = (JavaScriptArrayForm)form;

 Random rand = new Random();
 Set<String> set = new HashSet<String>();

 do{
     set.add(Integer.toString(rand.nextInt()));
 }while(set.size() <= 100);

 List<String> data = new ArrayList<String>();
 data.addAll(set);
 session.setAttribute("data",data);

 javaScriptArrayForm.setCurrent(1);

 return search(mapping,javaScriptArrayForm,request,response);
    }

    public String listToJSON(List<String> array) {
 Iterator<String> i = array.iterator();
 if(!i.hasNext())
     return "[]";

 StringBuilder sb = new StringBuilder();
 sb.append('[');
 for(;;){
     String e = i.next();
     if(e.length() == 0){
  if(!i.hasNext()){
      if(sb.indexOf(", ",sb.length() - 2) == -1){
   return sb.append(']').toString();
      }else{
   return sb.replace(sb.length() - 2,sb.length(),"]").toString();
      }
  }else
      continue;
     }

     sb.append(e);
     if(!i.hasNext()){
  if(sb.indexOf(", ",sb.length() - 2) == -1){
      return sb.append(']').toString();
  }else{
      return sb.replace(sb.length() - 2,sb.length(),"]").toString();
  }
     }
     sb.append(", ");
 }
    }
}

?

?

form:?

public class JavaScriptArrayForm extends ActionForm{    
    private String allJSON = "";//JS数组[所有页选中的]
    private String curJSON = "";//JS数组[当前页选中的]

    private String[] name;//当前页选中的值
    private List<String> data = new ArrayList<String>();//当前页所有的值
    
    private int current = 0;

    。。。。。。


 } 

??

JSP:

<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%> 
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%>
<%@ taglib uri="http://struts.apache.org/tags-nested" prefix="nested"%>
<html> 
 <head>
  <title>JSP for JavaScriptArrayForm form</title>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"></script>
  <script type="text/javascript">
   var v_all = trim(<bean:write name="javaScriptArrayForm" property