很多时候我们需要在jsp展现数据用的都是table,来看看
用flex3的Grid怎么做。
测试数据:
if object_id('product') is not null
drop table product
create table product
(
id int primary key identity(1,1),
productName varchar(50),
remark varchar(100)
)
declare @i int
set @i = 1
while @i < 100
begin
insert into product values('产品' + convert(varchar(3),@i),'备注' + convert(varchar(3),@i))
set @i = @i + 1
end
select * from product
grid需要的是xml数据源:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="list.send()">
<!--jsp请求-->
<!--<mx:HTTPService id="list" url="../index.jsp" />-->
<!--Servlet请求-->
<mx:HTTPService id="list" url="../productServlet" />
<!--grid数据绑定-->
<mx:DataGrid dataProvider="{list.lastResult.catalog.product}" width="395" height="307" x="223" y="54" color="#F2C50F"></mx:DataGrid>
</mx:Application>
看一提供数据的Servlet:
package com;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.dao.ProductDao;
import com.vo.Product;
@SuppressWarnings("serial")
public class ProductServlet extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html");
response.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
String str = "<?xml version=\"1.0\" encoding=\"utf-8\"?>";
str += "<catalog>";
ProductDao srv = new ProductDao();
List<Product> list = null;
list = srv.getAll();
Product product;
for (int i = 0; i < list.size(); i++)
{
product = (Product) list.get(i);
str += "<product productId=\"" + product.getId() + "\">";
str += "<productName>" + product.getProductName() + "</productName>";
str += "<remark>" + product.getRemark() +"</remark>";
str += "</product>";
}
str += "</catalog>";
out.write(str);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
this.doGet(request, response);
}
}
当然你也可以写成jsp的形式:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="com.dao.ProductDao"%>
<%@page import="com.vo.Product"%>
<?xml version="1.0" encoding="utf-8"?>
<catalog>
<%
ProductDao srv = new ProductDao();
List<Product> list = null;
list = srv.getAll();
Product product;
for (int i = 0; i < list.size(); i++)
{
product = (Product) list.get(i);
%>
<product productId="<%=product.getId()%>">
<productName><%=product.getProductName()%></productName>
<remark><%=product.getRemark()%></remark>
</product>
<%
}
%></catalog>
HTTPService标签实例化一个对象它将会请求jsp和反序列化请求对象。url属性需要指定jsp或Servlet Action并且返回xml数据。其中list是HTTPService标签的id,lastResult属性包含了一个对象叫"catalog",相当于xml的catalog节点。在catalog节点下有节点数组product.这个list.lastResult.catalog.product相当于从HTTPService请求jsp返回的数组。
如图: