使用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="allJSON"/>);
   var v_cur = trim(<bean:write name="javaScriptArrayForm" property="curJSON"/>);
   
   function doSearch(page){
    var val = document.forms[0].elements['current'].value + page;
    document.forms[0].elements['current'].value = eval("("+ val +")");

    if(v_cur.length==0){
     $(':checkbox[name=name][checked]').each(function(){
      v_all.push(this.value);
     });
    }else{
     $(':checkbox[name=name][checked]').each(function(index,ele){
      if(!contains(v_cur,ele.value)){
       v_all.push(this.value);//添加本次新选择的项
      }
     });
     
     v_cur = trim(v_cur);
    
     if(v_cur.length>0){
      //删除本次取消的项      
      for(var i=0;i< v_cur.length; i++){
       contains(v_all,v_cur[i]);
      }
     }
    }    
    
    $(':hidden[name=allJSON]').val("["+trim(v_all).join(",")+"]");
    $("form[name='javaScriptArrayForm']").submit();
   } 
   
   //去除空白的元素
   function trim(a){
    if(a.length > 0){
     a = a.sort();       
     while(a[0]=="") a.shift();
    }
    return a;
   }
   
   function contains(a,o){
    for(var i=0; i < a.length; i++){
     if(a[i]==o){
      a[i]="";
      return true;
     }
    }
    return false;
   }
  </script>
 </head>
 <body>
  <html:form action="/jsArray">  
   <html:hidden property="allJSON"/>
   <html:hidden property="method" value="search"/>
   <nested:iterate property="data" indexId="index">
    <html:multibox property="name"><nested:write/></html:multibox><nested:write/><br/>
   </nested:iterate>    
   <a href="javascript:void(0)" onclick="doSearch('-1')">prev</a>
   <html:text property="current" style="width:12px;"/>   
   <a href="javascript:void(0)" onclick="doSearch('+1')">next</a> 
  </html:form>
 </body>
</html>

 

你可能感兴趣的:(JavaScript,jquery,bean,Ajax,struts)