欢迎光临
我们一直在努力

复杂类型javabean(数组成员)在jsp中的运用:在jsp页面间传递数组-JSP教程,Java技巧及代码

建站超值云服务器,限时71元/月

在论坛上常看见有人问如何在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的游戏规则呵呵,但愿对你有帮助

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 复杂类型javabean(数组成员)在jsp中的运用:在jsp页面间传递数组-JSP教程,Java技巧及代码
分享到: 更多 (0)