Java基礎——編寫自定義標籤

    在開發項目過程中,我們經常有這樣的體會:同一個控件我們可能多處使用,同時我們需要在基礎的樣式上加上自己的樣式和操作的js代碼;遇到這種情況,如果每個地方都copy代碼的話那麼,後期如果要做修改,那麼維護的工作量是巨大的,無疑是個災難。基於這種情況,我們可以考慮使用自定義標籤,實現代碼的複用,後期的易維護。 

            先看一張關係圖:


 

     上圖是我們開發自定義標籤常用的接口,我們使用最快捷簡便的方式,繼承TagSupport類,只需重寫doStartTagdoEndtag方法。

製作步驟:

一、編寫java文件

代碼:

public class DataGridColumnTag extends TagSupport{
	protected String fields = "";// 顯示字段
	protected String searchFields = "";// 查詢字段 添加對區間查詢的支持
	private String width;
	private String height;
	private boolean checkbox = false;// 是否顯示覆選框
	private boolean showPageList = true;// 定義是否顯示頁面列表
	private boolean openFirstNode = false;// 是不是展開第一個節點
	private boolean fit = true;// 是否允許表格自動縮放,以適應父容器
	private boolean fitColumns = true;// 當爲true時,自動展開/合同列的大小,以適應的寬度,防止橫向滾動.
	private String sortName;// 定義的列進行排序
	private String sortOrder = "asc";// 定義列的排序順序,只能是"遞增"或"降序".
	private boolean showRefresh = true;// 定義是否顯示刷新按鈕
	private boolean showText = true;// 定義是否顯示刷新按鈕
	private String style = "easyui";// 列表樣式easyui,datatables
	private String onLoadSuccess;// 數據加載完成調用方法
	private String onClick;// 單擊事件調用方法
	private String onDblClick;// 雙擊事件調用方法
	private String queryMode = "single";// 查詢模式
	private String entityName;// 對應的實體對象
	private String rowStyler;// rowStyler函數
	private boolean autoLoadData = true; // 列表是否自動加載數據
	protected static Map<String, String> syscode = new HashMap<String, String>();
	static {
		syscode.put("class", "clazz");
	}

	public void setOnLoadSuccess(String onLoadSuccess) {
		this.onLoadSuccess = onLoadSuccess;
	}

	public void setOnClick(String onClick) {
		this.onClick = onClick;
	}

	public void setOnDblClick(String onDblClick) {
		this.onDblClick = onDblClick;
	}

	public void setShowText(boolean showText) {
		this.showText = showText;
	}

	public void setPagination(boolean pagination) {
		this.pagination = pagination;
	}

	public void setCheckbox(boolean checkbox) {
		this.checkbox = checkbox;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public void setTreegrid(boolean treegrid) {
		this.treegrid = treegrid;
	}

	public void setWidth(String width) {
		this.width = width;
	}

	public void setHeight(String height) {
		this.height = height;
	}

	//。。。。。。。。。
	//其他set方法。。。。
	//。。。。。。。。。
	public int doStartTag() throws JspTagException {
		// 清空資源
		urlList.clear();
		toolBarList.clear();
		columnValueList.clear();
		columnStyleList.clear();
		columnList.clear();
		fields = "";
		searchFields = "";
		return EVAL_PAGE;
	}

	public int doEndTag() throws JspException {
		try {
			JspWriter out = this.pageContext.getOut();
			if (style.equals("easyui")) {
				out.print(end().toString());
			} else {
				out.print(datatables().toString());
				out.flush();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return EVAL_PAGE;
	}
}

因爲繼承TagSupport所以重寫doStartTag(),doEndTag()。

 

二、編寫.tld文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
                        "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
	<tlib-version>1.0</tlib-version>
	<jsp-version>1.2</jsp-version>
	<short-name>t</short-name>
	<uri>/easyui-tags</uri>
	<display-name>"自定義標籤"</display-name>
	<tag>
		<name>datagrid</name>
		<tag-class>itoo.cgform.base.tag.DataGridTag</tag-class>
		<body-content>JSP</body-content>
		<description>數據列表</description>
		<attribute>
			<name>name</name>
			<required>true</required>
			<rtexprvalue>true</rtexprvalue>
			<description>列表TABLE標示</description>
		</attribute>
		<attribute>
			<name>treegrid</name>
			<rtexprvalue>true</rtexprvalue>
			<description>是否是樹形列表 值爲true 或者false</description>
		</attribute>
		<attribute>
			<name>actionUrl</name>
			<required>true</required>
			<rtexprvalue>true</rtexprvalue>
			<description>分頁提交的路徑</description>
		</attribute>
		<attribute>
			<name>pagination</name>
			<rtexprvalue>true</rtexprvalue>
			<description>是否分頁 true,false</description>
		</attribute>
		<attribute>
			<name>title</name>
			<rtexprvalue>true</rtexprvalue>
			<description>表格標題</description>
		</attribute>
		<attribute>
			<name>idField</name>
			<rtexprvalue>true</rtexprvalue>
			<description>主鍵字段</description>
		</attribute>
		<attribute>
			<name>width</name>
			<rtexprvalue>true</rtexprvalue>
			<description>表格寬度</description>
		</attribute>
		<attribute>
			<name>height</name>
			<rtexprvalue>true</rtexprvalue>
			<description>表格高度</description>
		</attribute>
		<attribute>
			<name>checkbox</name>
			<rtexprvalue>true</rtexprvalue>
			<description>是否顯示覆選框</description>
		</attribute>
		<attribute>
			<name>fit</name>
			<rtexprvalue>true</rtexprvalue>
			<description>是否適應父容器,true 默認 ,false</description>
		</attribute>
		<attribute>
			<name>sortName</name>
			<rtexprvalue>true</rtexprvalue>
			<description>定義的列進行排序</description>
		</attribute>
		<attribute>
			<name>sortOrder</name>
			<rtexprvalue>true</rtexprvalue>
			<description>定義列的排序順序,只能是"遞增 asc"或"降序desc"</description>
		</attribute>
		<attribute>
			<name>fitColumns</name>
			<rtexprvalue>true</rtexprvalue>
			<description>當爲true時,自動展開/合同列的大小,以適應的寬度,防止橫向滾動</description>
		</attribute>
		<attribute>
			<name>showPageList</name>
			<rtexprvalue>true</rtexprvalue>
			<description>是否顯示頁面列表</description>
		</attribute>
		<attribute>
			<name>showRefresh</name>
			<rtexprvalue>true</rtexprvalue>
			<description>是否顯示刷新按鈕</description>
		</attribute>
		<attribute>
			<name>showText</name>
			<rtexprvalue>true</rtexprvalue>
			<description>是否顯示分頁信息</description>
		</attribute>
		<attribute>
			<name>style</name>
			<rtexprvalue>true</rtexprvalue>
			<description>表格樣式easyui,datatables</description>
		</attribute>
		<attribute>
			<name>pageSize</name>
			<rtexprvalue>true</rtexprvalue>
			<description>每頁顯示條數</description>
		</attribute>
		<attribute>
			<name>onLoadSuccess</name>
			<rtexprvalue>true</rtexprvalue>
			<description>數據加載成調用方法</description>
		</attribute>
		<attribute>
			<name>onDblClick</name>
			<rtexprvalue>true</rtexprvalue>
			<description>雙擊調用方法</description>
		</attribute>
		<attribute>
			<name>onClick</name>
			<rtexprvalue>true</rtexprvalue>
			<description>單擊調用方法 tree 下function(rowData),dataGrid
				下function(rowIndex,rowData)</description>
		</attribute>
		<attribute>
			<name>queryMode</name>
			<rtexprvalue>true</rtexprvalue>
			<description>查詢模式:single(單條件查詢:默認),group(組合查詢)</description>
		</attribute>
		<attribute>
			<name>autoLoadData</name>
			<rtexprvalue>true</rtexprvalue>
			<description>列表頁面數據加載模式。true自動加載數據,false手動加載,默認爲true</description>
		</attribute>
		<attribute>
			<name>openFirstNode</name>
			<rtexprvalue>true</rtexprvalue>
			<description>是不是展開第一個節點,在樹形情況下,true展開,false不展開默認false</description>
		</attribute>
		<attribute>
			<name>entityName</name>
			<rtexprvalue>true</rtexprvalue>
			<description>對應的實體對象,如果entity和controller都是規則的可以不填,自動補全標籤做關聯</description>
		</attribute>
		<attribute>
			<name>rowStyler</name>
			<rtexprvalue>true</rtexprvalue>
			<description>行 css函數 指定名稱就可以,調用爲 functionName(index,row)這樣調用</description>
		</attribute>
		<attribute>
			<name>extendParams</name>
			<rtexprvalue>true</rtexprvalue>
			<description>datagrid 的擴展字段,如果easyui上面有但是jeecg沒有這個屬性可以自己添加
				添加規則和easyui的field一致</description>
		</attribute>
	</tag>
</taglib>

三、調用代碼

<t:datagrid sortName="createDate" sortOrder="desc" name="tablePropertyList" title="" fitColumns="false"
	actionUrl="" idField="id" fit="true" queryMode="group" checkbox="true">
</t:datagrid>

四、界面效果


 


總結:

       下面的圖描述了容器加載自定義標籤的一個過程:首先調用setPageContent方法,jsp引擎會將jsp頁轉換時隱含創建的pageContext對象作爲參數調用setPageContent方法;然後會調用getParentsetParent方法對父標籤對象進行設置;之後調用所有的setter方法,將變量全部賦值;上面三步完成後就開始調用doStartTag方法,標誌着真正的標籤處理開始了;隨後調用doEndTag意味着標籤處理結束。待所有同類標籤處理結束後調用release方法將該標籤從內存中清除掉。


發佈了141 篇原創文章 · 獲贊 14 · 訪問量 34萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章