现在的位置: 首页 > 综合 > 正文

备份

2013年09月12日 ⁄ 综合 ⁄ 共 3683字 ⁄ 字号 评论关闭

一:jsp之间传递数组

在论坛上常看见有人问如何在jsp页面间传递数组,其实用javabean是很容易实现的.

下面给个简单的例子,只要遵循javabean的游戏规则,什么类型的数据结构都可以传递:

1:写一个测试用的javabean:
package com.infoearth;
public class JobBean{
private int[] b;

 /**
  * @return 返回 b。
  */
 public int[] getB() {
  return b;
 }
 /**
  * @param b 要设置的 b。
  */
 public void setB(int[] b) {
  this.b = b;
 }
}

2:在第一个jsp页面中(注意文本框的name都是"b"):
<form action="manageJob.jsp" method="post">
<table>
<tr>
    <td width="50" height="30">&nbsp;</td>
        <td width="50" height="30"><input name="b" type="text" size="3"></td>
    <td width="50" height="30"><input name="b" type="text" size="3"></td>
    <td width="50" height="30"><input name="b" type="text" size="3"></td>
    <td width="50" height="30"><input name="b" type="text" size="3"></td>
    <td width="50" height="30"><input name="b" type="text" size="3"></td>
    <td width="50" height="30"><input name="b" type="text" size="3"></td>
    <td width="50" height="30"><input name="b" type="text" size="3"></td>
    <td width="50" height="30"><input name="b" type="text" size="3"></td>
    <td width="50" height="30"><input name="b" type="text" size="3"></td>
    <td width="50" height="30"><input name="b" type="text" size="3"></td>
    <td width="50" height="30"><input name="b" type="text" size="3"></td>
  </tr>
</table>
<input type="submit" value="提交">
</form>

3:在manageJob.jsp页面中,就可以接受这个数组了,很简单,用get方法就可以得到:
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*, com.infoearth.*"

errorPage="" %>
<jsp:useBean id="jobInfo" class="com.infoearth.JobBean" scope="request">
<jsp:setProperty name="jobInfo" property="*"/>
</jsp:useBean>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>无标题文档</title>
</head>

<body>
hello
<%
int[] temp=jobInfo.getB();
for(int i=0;i<temp.length;i++){
 out.print(i+"---"+temp[i]+"<br>");

}

%>
</body>
</html>

请注意javabean的内部机制:在javabean中命名的数组名为b,那么在第一个jsp中,填写数组b的文本框必须命名为b,否则就得不到!
这是javabean的游戏规则
呵呵,但愿对你有帮助

二:翻页

在java中,翻页比较头疼,下面是我写的一个实现翻页功能的类
请高手指正。
我的体会:
优点:简单好用
缺点:一次就从数据库中读出了所有的信息。不知如何改进,请指教   
public class selectPage implements java.io.Serializable{  //我的翻页类
        private int pageRecord;    //分页单位=10       
        private int currentPage;   //当前页=1
       
        private ArrayList allInfo;  //  记录集
        private int allRow;    //数据表总记录数=0
        private int pages;         //总页数=0
               
        public selectPage(ArrayList list) {
            this.allInfo = list;             
            this.pageRecord = 10;           
            this.currentPage = 1;
            this.allRow = list.size();
            this.pages = allRow/10;
            if(allRow%10!=0)this.pages++;
            }
       
        public void setPageRecord(int pgrecord){
            pageRecord = pgrecord;
            }
           
        public void setPages(){     //根据pageRecord改变总页
            int p = allRow/pageRecord;           
            if(allRow%pageRecord!=0)p++; 
            this.pages = p;
            }
           
        public void setCurrentPage(int curpage){
            currentPage = curpage;
            }
           
        public int getPageRecord(){
            return pageRecord;
            }
           
        public int getPages(){
            return pages;
            }
           
        public int getCurrentPage(){
            return currentPage;
            }
           
        public int getAllRow(){
            return allRow;
            }   
       
        public ArrayList getPage(){
            ArrayList tempList = new ArrayList();
            int endindex = pageRecord*currentPage > allRow? allRow:pageRecord*currentPage;
            for(int i=pageRecord*(currentPage-1);i<endindex;i++){
                tempList.add(allInfo.get(i));
                }
            return tempList;
            }
       
        public String toString(){
            String tempStr = "pageRecord="+this.getPageRecord()+"currentPage="+this.getCurrentPage()+"allRow="+this.getAllRow()+"pages="+this.getPages();
            return tempStr;
            }
        }

抱歉!评论已关闭.