為了替代硬編碼eXtremeTable使用的默認屬性值,我在屬性文件中配置所有用到的屬性。
如果你需要覆蓋任何默認的設置,你可以創建自己的extremecomponents.properties文件 并設置你想改變的值。
為了設置屬性文件,你應該如下例所示在/WEB-INF/web.xml文件中聲明一個context-param,并 指定你的屬性文件的路徑:
<context-param>
<param-name>extremecomponentsPreferencesLocation</param-name>
<param-value>/org/extremesite/resource/extremecomponents.properties</param-value>
</context-param>
你可以認為屬性文件為你提供了一個對所有的eXtremeTables聲明全局設置的一個方法。
創建屬性文件的最大好處就是避免在標簽中復制、粘貼相同的屬性。典型的extremecomponents.properties文件如下所示:
table.imagePath=/extremesite/images/*.gif
table.rowsDisplayed=12
column.parse.date=yyyy-MM-dd
column.format.date=MM/dd/yyyy
column.format.currency=$###,###,##0.00
在屬性文件定義的TableTag使用最多的兩個屬性是:imagePath和rowsDisplayed。如果你不在屬性文件中聲明
這些屬性,你需要在每個eXtremeTable中添加他們。典型的表如下所示:
<ec:table
items="presidents"
action="${pageContext.request.contextPath}/presidents.run"
imagePath="${pageContext.request.contextPath}/images/*.gif"
rowsDisplayed="12"
title="Presidents"
>
...
</ec:table>
如果在屬性文件聲明imagePath和rowsDisplayed,則表如下所示:
<ec:table
items="presidents"
action="${pageContext.request.contextPath}/presidents.run"
title="Presidents"
>
...
</ec:table>
正如你所見,屬性文件避免了重復編碼。
在屬性文件定義的ColumnTag使用最多的兩個屬性是:parse和format。如果你不在屬性文件中聲明
這些屬性,你需要在每個eXtremeTable中添加他們。典型的列使用日期cell如下所示:
<ec:column property="dateOfBirth" cell=”date” parse=”yyyy-MM-dd” format=”MM/dd/yyyy”/>
如果在屬性文件聲明parse和format,則列如下所示:
<ec:column property="dateOfBirth" cell=”date”/>
當然你仍然可以定義parse和format屬性來覆蓋全局設置,但是大多數工程對于日期使用一致的parse
和format。需要注意屬性文件中parse.date和format.date的聲明語法。
下例為使用貨幣cell的典型列:
<ec:column property="salary" cell=”currency” format=”$###,###,##0.00”/>
如果在屬性文件聲明format,則列如下所示:
<ec:column property="salary" cell=”currency”/>
另外,你可以聲明一個定制的format并在列中通過使用列的basis來使用它,我把這想象為named屬性。因此如果你的
extremecomponents.properties文件如下所示:
table.format.myCustomDate=yy-MM-dd
那么列可以如下使用定制的format:
<ec:column property="dateOfBirth" cell="date" format=”myCustomDate”>
10.4. Advanced
Techniques
使用named屬性是我定義其他不同屬性默認值時經常使用的方法。你可能對我
使用cell="date"來指定日期cell、使用cell="currency"來指定貨幣cell或使用view="xls."來指定xls導出感到疑惑。
如果我給你展示extremetable.properties文件的一些片斷,這些就將非常清晰了。 extremetable.properties是eXtremeTable聲明默認設置的屬性文件,你可以通過使用
extremecomponents.properties文件來覆蓋它。
column.cell.date=org.extremecomponents.table.cell.DateCell
column.cell.currency=org.extremecomponents.table.cell.NumberCell
column.filterCell.droplist=org.extremecomponents.table.cell.FilterDroplistCell
table.view.xls=org.extremecomponents.table.view.XlsView
當你在列上定義cell="date"時,eXtremeTable尋找到column.cell. 屬性并將你定義的cell屬性值拼接上。
換句話說cell="date"關聯到column.cell.date=org.extremecomponents.table.cell.DateCell這條屬性。使用屬性文件
真正強大的地方在于你能夠在extremecomponents.properties文件中聲明一個定制的cell,并在ColumnTag中通過
名稱來使用它。
再使用一個實例來闡明這一點,是否記得ColumnTag章Cell節中如何調用一個名為MyCell的定制cell:
<ec:column property="firstName" cell="com.mycompany.cell.MyCell"/>
cell使用的更好方式是在屬性文件中聲明并通過名稱使用它。首先,更新extremecomponents.properties文件:
table.imagePath=/extremesite/images/*.gif
table.rowsDisplayed=12
table.cellspacing=2
column.parse.date=yyyy-MM-dd
column.format.date=MM/dd/yyyy
column.format.currency=$###,###,##0.00
column.cell.myCell=com.mycompany.cell.MyCell
現在可以通過名稱調用MyCell:
<ec:column property="firstName" cell="myCell"/>
正如你所見的這能幫助保持代碼清潔,并且這些都在一個地方定義。如果你的定制cell聲明 需要改變你只需要修改屬性文件。