日期:2014-05-17  浏览次数:21019 次

用JSP页面以for循环的方式输出表格信息,怎么做?
javabean程序:
package task6;

import java.util.ArrayList;
import java.util.List;

@SuppressWarnings("unchecked") 
public class MyList {
   
List list = new ArrayList();
  public MyList(){
list.add("zhangsan");
list.add("张三");
list.add("19811224");
list.add("女");
list.add("lisi");
list.add("李四");
list.add("19770805");
list.add("男");
list.add("wangwu");
list.add("王五");
list.add("19830924");
list.add("女");
  } 
  public List getList(){  
return list;
  }
}

JSP程序:
<%@ page language="java" import="task6.MyList" pageEncoding="UTF-8"%>
<HTML>
<BODY>
<jsp:useBean id="mylist" scope="application" class="task6.MyList" >
</jsp:useBean>
<H3>MyList scope="request" Example</H3>

<table>
<%
  for(int i=0;i<3;i++){//循环3次 每次输出一行
%>
<%
  for(int j=0;j<4;j++){//循环4次 每次输出一格 %>

<tr>
<td> 
<%= mylist.getList() %> 
</td>
<%}%>
</tr>
<%}%>
</table>
</BODY>
</HTML>


应该怎样修改,才能正确输出三行显示每个人的信息,即zhangsan/张三/19811224/女、lisi/李四/19770805/男、wangwu/王五/19830924/女 分三行显示。

请高手指点一下呀~~~~

------解决方案--------------------
Java code

List<Map<String,String>> list = new ArrayList<Map<String,String>>(); 
Map map = new HashMap();
map.put("e_name","zhangsan");
map.put("c_name","张三");
map.put("bth_day","19811224");
map.put("sex","女");
list.add(map);
map = new HashMap();
map.put("e_name","lisi");
map.put("c_name","李四");
map.put("bth_day","19770805");
map.put("sex","男");
list.add(map);
map = new HashMap();
map.put("e_name","wangwu");
map.put("c_name","王五");
map.put("bth_day","19830924");
map.put("sex","女");
list.add(map);

------解决方案--------------------
Java code
<%@ page language="java" import="task6.MyList,java.util.List" pageEncoding="UTF-8"%>
<HTML>
<BODY>
<jsp:useBean id="mylist" scope="application" class="task6.MyList" >
</jsp:useBean>
<H3>MyList scope="request" Example</H3>

<table border=1>
<tr>
    <td> 英文</td>
    <td> 中文</td>
    <td> 生日</td>
    <td> 性别</td>
</tr>
<%
    List list = mylist.getList() ;
    int idx1 = 0;
    int idx2 = 1;
    int idx3 = 2;
    int idx4 = 3;
    int len = list.size()  / 4; 

    for (int i = 0; i < len -1; i++){
        
%>
            <tr>
                <td><%=(String)list.get(idx1)%></td>
                <td><%=(String)list.get(idx2)%></td>
                <td><%=(String)list.get(idx3)%></td>
                <td><%=(String)list.get(idx4)%></td>
            </tr>
<%
        idx1 +=4 ;
        idx2 +=4 ;
        idx3 +=4 ;
        idx4 +=4 ;
        }
%>


</table>
</BODY>
</HTML>

------解决方案--------------------
<body>
<table border="1px&qu