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

JSP中动态合并table
关于动态合并table!首先从网上看到一个例子:如下
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ page import="test.*"%>

<%
	//定义listpojo用于存放pojo类
	List<Pojoceshi> listpojo = new ArrayList<Pojoceshi>();

	//实例化一个pojo类
	Pojoceshi pojo = new Pojoceshi();
	pojo.setName("济南");

	//定义listfood用于存放食物
	List<String> listfood = new ArrayList<String>();
	String food = null;
	food = "香蕉";
	listfood.add(food);
	food = null;
	food = "橘子";
	listfood.add(food);
	food = null;

	pojo.setList_food(listfood);

	listpojo.add(pojo);
	pojo = null;

	//实例化一个pojo类
	Pojoceshi pojo2 = new Pojoceshi();
	pojo2.setName("青岛");

	//定义listfood用于存放食物
	List<String> listfood2 = new ArrayList<String>();
	String food2 = null;
	food2 = "香蕉";
	listfood2.add(food2);
	food2 = null;
	food2 = "橘子";
	listfood2.add(food2);
	food2 = null;

	pojo2.setList_food(listfood2);

	listpojo.add(pojo2);
	pojo2 = null;

	//实例化一个pojo类
	Pojoceshi pojo3 = new Pojoceshi();
	pojo3.setName("马鞍山");

	//定义listfood用于存放食物
	List<String> listfood3 = new ArrayList<String>();
	String food3 = null;
	food3 = "香蕉";
	listfood3.add(food3);
	food3 = null;

	pojo3.setList_food(listfood3);

	listpojo.add(pojo3);
	pojo3 = null;
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>

<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
</head>
<body>
	<TABLE border=2>
		<tr>
			<td>名称</td>
			<td>食物</td>
		</tr>
		<%
			Pojoceshi pojo888 = null;
			for (int i = 0; i < listpojo.size(); i++) {
				pojo888 = listpojo.get(i);
				int j = pojo888.getList_food().size();
		%>

		<tr>
			<td ROWSPAN='<%=j%>'><%=pojo888.getName()%></td>

			<%
				for (int ii = 0; ii < j; ii++) {
						if (ii == 0) {
			%>

			<td><%=pojo888.getList_food().get(ii)%></td>
		</tr>
		<%
			} else {
						System.out.print("shuliang" + ii);
		%>
		<tr>
			<td><%=pojo888.getList_food().get(ii)%></td>
		</tr>

		<%
			}
				}
				pojo888 = null;
			}
		%>
	</TABLE>
</body>


</html>



DTO类

package test;

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

public class Pojoceshi {
	private String name;
	private List<String> list_food;
	//=new ArrayList<String>();
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public List<String> getList_food() {
		return list_food;
	}
	public void setList_food(List<String> list_food) {
		this.list_food = list_food;
	}
}



以上只是简单的合并一列! 假如有如下业务
要显示的字段有公司,部门,子部门,职员,薪水!
以公司,部门,子部门来合计有多少职员,总工资是多少!
其实也很简单!