通過TAG的疊代自定義自己的TABLE清單标簽,友善開發
調用方式:
<t:Table items="${returnBean}" id="tab" headerCss="headerCss" var="item" cssName="table">
<t:Row>
<t:Cell property="checkbox" title="選擇">
<input type="checkbox" id="a${item.id}" value="${item.id}">
</t:Cell>
<t:Cell property="account" title="賬号"/>
<t:Cell property="email" value="${item.email }" title="E_Mail"/>
<t:Cell property="id" title="id" >
<a href="../index.jsp?id=${item.id}" target="_blank" rel="external nofollow" mce_href="index.jsp?id=${item.id}" target="_blank" rel="external nofollow" >Go Test</a>
</t:Cell>
</t:Row>
<t:Toolbar position="top" css="toolbar">
<t:Button title="新增" id="add1" disabled="disabled"/>
</t:Toolbar>
</t:Table>
1.Table.java
package com.system.tags;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.BodyTagSupport;
import org.apache.commons.beanutils.BeanUtilsBean;
public class Table extends BodyTagSupport{
/**
*
*/
private static final long serialVersionUID = 1L;
private static BeanUtilsBean util = BeanUtilsBean.getInstance();
private String action;
private String cssName;
private String var;
private Object item;
private Collection<?> items;
private Iterator<?> result;
private StringBuffer loop ;
private String id;
private String headerCss;
//子标簽
private Row row;
//子标簽
private Toolbar toolbar;
public Table(){
}
public String getAction() {
return action;
}
public void setAction(String action) {
this.action = action;
}
public String getCssName() {
return cssName;
}
public void setCssName(String cssName) {
this.cssName = cssName;
}
public Row getRow() {
return row;
}
public void setRow(Row row) {
this.row = row;
}
public Collection<?> getItems() {
return items;
}
public void setItems( Collection<?> items) {
this.items = items ;
this.result = this.items.iterator();
}
public String getVar() {
return var;
}
public void setVar(String var) {
this.var = var;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getHeaderCss() {
return headerCss;
}
public void setHeaderCss(String headerCss) {
this.headerCss = headerCss;
}
public Toolbar getToolbar() {
return toolbar;
}
public void setToolbar(Toolbar toolbar) {
this.toolbar = toolbar;
}
/**
* 設定Request.Attribute
*/
private void setHttpAttribute(){
if(this.item != null){
pageContext.setAttribute(var, item);
}else{
pageContext.removeAttribute(var,PageContext.PAGE_SCOPE);
}
}
/**
* 疊代TR開始
*/
public int doStartTag() throws JspException{
if(result.hasNext()){
item = result.next();
}else{
return SKIP_BODY;
}
setHttpAttribute();
return EVAL_BODY_INCLUDE;
}
/**
* 疊代TR
*/
public int doAfterBody() throws JspException{
if(loop == null){
loop = new StringBuffer();
}
loop.append(parse());
if(result.hasNext()){
item = result.next();
setHttpAttribute();
}else{
return SKIP_BODY;
}
return EVAL_BODY_AGAIN;
}
/**
* 輸出最終HTML
*/
public int doEndTag() throws JspException{
JspWriter jw = pageContext.getOut();
StringBuffer sb = new StringBuffer();
String tb = buildToolbar();
sb.append("<table " + (id == null ? "":"id='" + id + "'") + " class='" + cssName + "'>/n");
if(Toolbar.BOTH.equals(toolbar.getPosition()) || Toolbar.TOP.equals(toolbar.getPosition())){
sb.append(tb);
}
if(this.getRow() != null){
List<Cell> list = this.getRow().getList();
int size = list.size();
sb.append("/t<tr " + (headerCss == null ?"":"class='" + headerCss + "'") + ">/n");
for(int i = 0; i < size; i++){
Cell cel = list.get(i);
sb.append("/t/t<td>" + cel.getTitle() + "</td>/n");
}
sb.append("/t</tr>/n");
sb.append(loop);
}
//toolbar
if(Toolbar.BOTH.equals(toolbar.getPosition())
|| Toolbar.BOTTOM.equals(toolbar.getPosition())
|| TagTools.isEmpty(toolbar.getPosition())){
sb.append(tb);
}
sb.append("</table>/n");
try {
jw.write(sb.toString());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
items = null;
result = null;
sb = null;
loop = null;
}
return super.doEndTag();
}
/**
* 構造TOOLBAR
* @return
*/
private String buildToolbar(){
StringBuffer sb = new StringBuffer();
int colspan = this.getRow().getList().size();
Toolbar toolbar = this.getToolbar();
sb.append("/t<tr>/n");
sb.append("/t/t<td colspan='" + colspan + "' "
+ (TagTools.isEmpty(toolbar.getCss()) ? "" : "class='" + toolbar.getCss() + "'") + ">");
List<Button> bts = toolbar.getButtons();
int size = bts.size();
for(int i = 0; i < size; i++){
Button button = bts.get(i);
sb.append("<input type='button' value='" + button.getTitle() + "'" +
(!TagTools.isEmpty(button.getDisabled()) ? " disabled" : "") +
(!TagTools.isEmpty(button.getCss()) ? " class='" + button.getCss() + "'" : "") +
(!TagTools.isEmpty(button.getEvent()) ? " οnclick='" + button.getEvent() + "'" : "") +
" id='" + button.getId() + "'>");
}
sb.append("</td>/n");
sb.append("/t</tr>/n");
return sb.toString();
}
@Override
public void release() {
items = null;
result = null;
}
/**
*
* @return
*/
private String parse(){
StringBuffer sb = new StringBuffer();
sb.append("/t<tr>/n");
if(this.getRow() == null){
return "";
}
List<Cell> cells = this.getRow().getList();
int size = cells.size();
for(int i = 0; i < size; i++){
Cell tmp = cells.get(i);
if(tmp.getBodyValue() != null && !"".equalsIgnoreCase(tmp.getBodyValue())){
sb.append("/t/t<td>" + tmp.getBodyValue() + "</td>/n");
}else{
if(tmp.getValue() != null){
sb.append("/t/t<td>" + tmp.getValue() +"</td>/n");
}else{
//以PROPERTY為
sb.append("/t/t<td>" + getPropertyValue(tmp.getProperty()) + "</td>/n");
}
}
}
sb.append("/t</tr>/n");
return sb.toString();
}
private String getPropertyValue(String property){
try {
return util.getProperty(item, property);
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "";
}
}
2.Row.java
package com.system.tags;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
import javax.servlet.jsp.tagext.Tag;
public class Row extends BodyTagSupport{
/**
*
*/
private static final long serialVersionUID = 1L;
private List<Cell> list;
public Row(){
super();
this.list = new ArrayList<Cell>();
}
public void addCell(Cell cell){
this.list.add(cell);
}
public List<Cell> getList() {
return list;
}
public void setList(List<Cell> list) {
this.list = list;
}
public int doStartTag() throws JspException{
this.list.clear();
Tag tag = findAncestorWithClass(this, Table.class);
Table table = (Table) tag;
table.setRow(this);
return EVAL_BODY_INCLUDE;
}
public int doEndTag() throws JspException{
return EVAL_PAGE;
}
}
3.Cell.java
package com.system.tags;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyContent;
import javax.servlet.jsp.tagext.BodyTagSupport;
import javax.servlet.jsp.tagext.Tag;
public class Cell extends BodyTagSupport {
/**
*
*/
private static final long serialVersionUID = 1L;
private String property;
private String title;
private String value;
private String bodyValue;
public Cell() {
super();
}
public Cell(String property, String title, String value,String bodyValue) {
this.property = property;
this.title = title;
this.value = value;
this.bodyValue = bodyValue;
}
public String getProperty() {
return property;
}
public void setProperty(String property) {
this.property = property;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getBodyValue() {
return bodyValue;
}
public void setBodyValue(String bodyValue) {
this.bodyValue = bodyValue;
}
public int doAfterBody() throws JspException {
BodyContent bc = this.getBodyContent();
this.bodyValue = bc.getString();
return SKIP_BODY;
}
public int doEndTag() throws JspException {
Tag tag = findAncestorWithClass(this, Row.class);
Row row = (Row) tag;
//不能用addCell(this),這個TAG是單例的,如果用this的話,永遠指向最後一個TAG
row.addCell(new Cell(this.property, this.title,
this.value,this.bodyValue));
return super.doEndTag();
}
public int doStartTag() throws JspException {
return EVAL_BODY_BUFFERED;
}
}
4.Toolbar.java
package com.system.tags;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
import javax.servlet.jsp.tagext.Tag;
public class Toolbar extends BodyTagSupport{
/**
*
*/
private static final long serialVersionUID = 1L;
public static final String BOTTOM = "bottom";
public static final String TOP = "top";
public static final String BOTH = "both";
private String css;
private String position;
private PageBar pageBar;
private List<Button> buttons;
public Toolbar(){
super();
this.buttons = new ArrayList<Button>();
}
public String getCss() {
return css;
}
public void setCss(String css) {
this.css = css;
}
public String getPosition() {
return position;
}
public void setPosition(String position) {
this.position = position;
}
public PageBar getPageBar() {
return pageBar;
}
public void setPageBar(PageBar pageBar) {
this.pageBar = pageBar;
}
public List<Button> getButtons() {
return buttons;
}
public void setButtons(List<Button> buttons) {
this.buttons = buttons;
}
public int doStartTag() throws JspException{
this.buttons.clear();
Tag tag = findAncestorWithClass(this, Table.class);
Table table = (Table)tag;
table.setToolbar(this);
return EVAL_BODY_INCLUDE;
}
public int doEndTag() throws JspException{
return EVAL_PAGE;
}
}
5.Button.java
package com.system.tags;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
import javax.servlet.jsp.tagext.Tag;
public class Button extends BodyTagSupport{
/**
*
*/
private static final long serialVersionUID = 1L;
private String id;
private String css;
private String event;
private String title;
private String disabled;
public Button(){
super();
}
public Button(String id,String css,String event,String title,String disabled){
this.id = id;
this.css = css;
this.event = event;
this.title = title;
this.disabled = disabled;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getCss() {
return css;
}
public void setCss(String css) {
this.css = css;
}
public String getEvent() {
return event;
}
public void setEvent(String event) {
this.event = event;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDisabled() {
return disabled;
}
public void setDisabled(String disabled) {
this.disabled = disabled;
}
public int doStartTag() throws JspException{
Tag tag = findAncestorWithClass(this, Toolbar.class);
Toolbar tool = (Toolbar) tag;
tool.getButtons().add(new Button(id,css,event,title,disabled));
return EVAL_BODY_INCLUDE;
}
public int doEndTag() throws JspException{
return EVAL_PAGE;
}
}
6.Pagebar.java
package com.system.tags;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
/**
* 擴充 ,未實作
*
*
*/
public class PageBar extends BodyTagSupport{
/**
*
*/
private static final long serialVersionUID = 1L;
private String current;
private String total;
private String pageSize;
public String getCurrent() {
return current;
}
public void setCurrent(String current) {
this.current = current;
}
public String getTotal() {
return total;
}
public void setTotal(String total) {
this.total = total;
}
public String getPageSize() {
return pageSize;
}
public void setPageSize(String pageSize) {
this.pageSize = pageSize;
}
public int doStartTag() throws JspException{
return EVAL_BODY_INCLUDE;
}
public int doEndTag() throws JspException{
return EVAL_PAGE;
}
}
7.Tagtools.java
package com.system.tags;
public class TagTools {
public static boolean isEmpty(String s){
return "".equals(s) || null == s;
}
}
8.tag.tld
<taglib>
<tlibversion>1.0</tlibversion>
<jspversion>1.2</jspversion>
<shortname>tags</shortname>
<uri>/WEB-INF/tags</uri>
<tag>
<name>Table</name>
<tagclass>com.system.tags.Table</tagclass>
<bodycontent>jsp</bodycontent>
<attribute>
<name>items</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>var</name>
<required>true</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>id</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>headerCss</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>cssName</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
<tag>
<name>Row</name>
<tagclass>com.system.tags.Row</tagclass>
<bodycontent>jsp</bodycontent>
</tag>
<tag>
<name>Cell</name>
<tagclass>com.system.tags.Cell</tagclass>
<bodycontent>jsp</bodycontent>
<attribute>
<name>property</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>title</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>value</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
<tag>
<name>Toolbar</name>
<tagclass>com.system.tags.Toolbar</tagclass>
<bodycontent>jsp</bodycontent>
<attribute>
<name>css</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>position</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
<tag>
<name>Button</name>
<tagclass>com.system.tags.Button</tagclass>
<bodycontent>jsp</bodycontent>
<attribute>
<name>id</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>css</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>event</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>title</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>disabled</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
<tag>
<name>PageBar</name>
<tagclass>com.system.tags.PageBar</tagclass>
<bodycontent>jsp</bodycontent>
</tag>
</taglib>
最終效果:

之是以不是标準的是因為在疊代的時候,理論上應該朝頁面輸出内容,但在這裡是不會輸出的,統一在TABLE 的ENDTAG中輸出。如果檢視頁面源代碼的話,就會發現一塊很大的空白。
不過可以将就用了,分頁的沒有放進來,這個跟各自系統有關系了。有興趣的朋友可以自己把它加進去。