Posted on 2007-04-24 15:51
G_G 阅读(1040)
评论(0) 编辑 收藏 所属分类:
Reportform 、
JspTag
<?
xml version="1.0" encoding="UTF-8"
?>
<!
DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"
>
<
web-app
>
<
taglib
>
<
taglib-uri
>
http://www.extremecomponents.org
</
taglib-uri
>
<
taglib-location
>
/WEB-INF/extremecomponents.tld
</
taglib-location
>
</
taglib
>
</
web-app
>
<%
@ taglib prefix
=
"
ec
"
uri
=
"
/WEB-INF/extremecomponents.tld
"
%>
<%
@ taglib prefix
=
"
c
"
uri
=
"
/WEB-INF/c.tld
"
%>
<%
@ page language
=
"
java
"
import
=
"
java.util.*
"
pageEncoding
=
"
UTF-8
"
%>
<%
String
path
=
request.getContextPath();
String
basePath
=
request.getScheme()
+
"
://
"
+
request.getServerName()
+
"
:
"
+
request.getServerPort()
+
path
+
"
/
"
;
%>
<!
DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
>
<
html
>
<
head
>
<
base
href
="<%=basePath%>"
>
<
title
>
My JSP 'MyJsp.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
="<c:url value="
/extremecomponents.css"
/>
">
</
head
>
<
body
>
<%
List goodss
=
new
ArrayList();
for
(
int
i
=
1
; i
<=
10
; i
++
)
{
Map goods
=
new
java.util.HashMap();
goods.put(
"
code
"
,
"
A00
"
+
i);
goods.put(
"
name
"
,
"
面包
"
+
i);
goods.put(
"
status
"
,
"
A:valid
"
);
goods.put(
"
born
"
,
new
Date
());
goodss.add(goods);
}
request.setAttribute(
"
goodss
"
, goodss);
%>
<
ec:table
action
="${pageContext.request.contextPath}/test.jsp"
items
="goodss"
cellpadding
="1"
title
="my bread"
>
<
ec:row
></
ec:row
>
<
ec:column
property
="code"
/>
<
ec:column
property
="name"
/>
<
ec:column
property
="status"
/>
<
ec:column
property
="born"
cell
="date"
format
="yyyy-MM-dd"
/>
</
ec:table
>
</
body
>
</
html
>
{转}
关键字: java,eXtremeComponents
在我开发的一个通用查询项目中想把查询结果集的显示部分采用eXtremeComponents组件来处理,但是碰到个问题,就是组件预先并不知道查询结果的列名,也就是必须解决Column列的动态显示问题。 有一种方法就是通过在jsp页面里罗列一下,但是总感觉不舒服,查文档发现eXtremeComponents有一接口AutoGenerateColumns可实现此功能,以下为具体实现步骤: 一、在应用的servlet(或struts action等)里(如sqlAction.do)实现根据SQL语句的运行结果获取字段名称列表(fieldnames)和查询结果集(results)并将其放入httpRequest的属性中
代码 - List fieldnames = 实现[获取字段名称列表]方法;
- List results = 实现[获取查询结果集]方法;
- httpRequest.setAttribute("fieldnames", fieldnames);
- httpRequest.setAttribute("results", results);
results将作为eXtremeTable组件中属性items的值,fieldnames将用来迭代构造Column对象
二、编写类AutoGenerateColumnsImpl实现org.extremecomponents.table.core.AutoGenerateColumns接口
代码 - package org.boogie.sql.common.ec;
-
- import java.util.Iterator;
- import java.util.List;
-
- import org.extremecomponents.table.bean.Column;
- import org.extremecomponents.table.core.AutoGenerateColumns;
- import org.extremecomponents.table.core.TableModel;
-
- public class AutoGenerateColumnsImpl implements AutoGenerateColumns {
-
- public void addColumns(TableModel model) {
- List fieldnames = (List) model.getContext().getRequestAttribute(
- "fieldnames");
- Iterator iterator = fieldnames.iterator();
- while (iterator.hasNext()) {
- String fieldname = (String) iterator.next();
- Column column = model.getColumnInstance();
- column.setProperty(fieldname);
-
- model.getColumnHandler().addAutoGenerateColumn(column);
- }
- }
- }
AutoGenerateColumns接口只有一个方法addColumns供实现,在此方法中通过传入的TableModel型参数 model可从其Context中获取到httpRequest中的fieldnames属性值,然后根据fieldnames列表迭代构造对应 Column后添加到model中
三、在显示页文件的eXtremeTable中,配置TableTag的属性items值为results,配置ColumnTag的属性autoGenerateColumns值为类AutoGenerateColumnsImpl的全路径
代码 - <ec:table
- items="results"
- var="result"
- action="${pageContext.request.contextPath}/sqlAction.do"
- imagePath="${pageContext.request.contextPath}/images/table/*.gif"
- title="查询结果"
- width="100%"
- rowsDisplayed="5"
- >
- <ec:parameter name="method" value="ec"/>
- <ec:row>
- <ec:columns autoGenerateColumns="org.boogie.sql.common.ec.AutoGenerateColumnsImpl"/>
- </ec:row>
- </ec:table>
|
小结******************************************************************************
<%@ page language="java" pageEncoding="UTF-8"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-logic" prefix="logic"%>
<%@ taglib uri="http://www.extremecomponents.org" prefix="ec" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
<%@ taglib uri="http://www.jjm.cn/tags-security" prefix="jjmtag"%>
<html>
<head>
<LINK href="<%=request.getContextPath()%>/css/extremetable.css" rel="stylesheet" type="text/css">
<script language="JavaScript" src="<%=request.getContextPath()%>/res_others/calendar/calendar.js "> </script> //时间 的 js 插件
<LINK rel="StyleSheet" type="text/css" href="<%=request.getContextPath()%>/css/jjmStyle.css">
</head>
<body>
<CENTER>
<TABLE border="1" class="borderLessTable" width=100%>
<html:form action="/tAT.do">
<TR>
<TD class="tdQueryCaption13">
<CENTER> 时间范围:</CENTER>
</TD>
<TD class="tdQueryCaption13">
<CENTER>
<html:text property="time" size="8" readonly="true"/> //作为 时间 的 javascript 插件
<img alt="弹出日历下拉菜单" height="16" width="16" align="middle"
src="<%=request.getContextPath()%>/res_others/calendar/img/cal.gif"
style="cursor:hand;"
onclick="fPopUpCalendarDlg(time);return false"/>
</CENTER>
</TD>
<tD>
<CENTER><html:image src="/rlzy/images/edit/chaxun.gif"></html:image></CENTER>
</tD>
</html:form>
</TABLE>
</CENTER>
<ec:table
items ="list"
imagePath="${pageContext.request.contextPath}/images/ectable/*.gif"
cellpadding ="1">
<ec:row></ec:row>
<ec:column property ="入段&调离" />
<ec:column cell="com.jjm.extremesite.cell.CorpNameCell" property="段号"/> // (1) 标签的重写
<ec:column cell="com.jjm.extremesite.cell.CorpNameCell" property="车间" />
<ec:column property="姓名" />
<ec:column property="入段时间" cell="date" format="yyyy-MM-dd" /> //时间 的表示 cell="date" format="yyyy-MM-dd"
<ec:column property="调离时间" cell="date" format="yyyy-MM-dd"/>
</ec:table >
</body>
</html>
package com.jjm.extremesite.cell;
import org.extremecomponents.table.bean.Column;
import org.extremecomponents.table.cell.AbstractCell;
import org.extremecomponents.table.core.TableModel;
import com.jjm.bj.dao.CorpBean;
import com.jjm.bj.dao.CorpDao;
public class CorpNameCell extends AbstractCell {
public void destroy() {
}
/** *//**
* @param args
*/
public static void main(String[] args) {
}
private String getCorpName(String id){
if (id!=null && id.length()>0){
CorpBean obj =null;
obj=CorpDao.findByID(id);
if (obj!=null){
return obj.getCorpName();
}
}
return "";
}
protected String getCellValue(TableModel tableModel, Column column) {
CorpNameCell corpName=new CorpNameCell();
return corpName.getCorpName(column.getValueAsString());
}
}