displaytag TableProperties扩展

早就在项目中使用了displaytag1.1作为列表展现的页面组件。displaytag相对于ext不同,是服务器端的ui组件, 能很好的支持分页和排序,也能通过扩展行列装潢器来自定义行和列,通过更换css方便替表格风格。在一般项目中都很合适, 所以我从第一个项目开始就一直用到了现在。
一直以来有一个地方总觉得别扭, 就是displaytag.properties这个配置文件, 首先它是支持国际化的,于是就会有displaytag_xx.properties。但还有一个builtin的,就是displaytag.properties,存在于org/displaytag/properties/下,跟TableProperties.class在一起。
看看这些版本的displaytag.properties是如何加载的,打开TableProperties看看:

private TableProperties(Locale myLocale) throws TablePropertiesLoadException
{
this.locale = myLocale;
// default properties will not change unless this class is reloaded
Properties defaultProperties = loadBuiltInProperties();//这里是只能加载TableProperties.class同路径下的displaytag.properties

properties = new Properties(defaultProperties);
addProperties(myLocale);

// Now copy in the user properties (properties file set by calling setUserProperties()).
// note setUserProperties() MUST BE CALLED before the first TableProperties instantation
Enumeration keys = userProperties.keys();
while (keys.hasMoreElements())
{
String key = (String) keys.nextElement();
if (key != null)
{
properties.setProperty(key, (String) userProperties.get(key));
}
}
}

可以看出,是先加载displaytag.properties,再根据locale记载国际化版本的。
好了,既然如此,为了合理分配这些文件的职责。我想做一个调整,就是让displaytag.properties保存公用的核心配置,比如:
pagination.sort.param=sort
pagination.sortdirection.param=dir
pagination.pagenumber.param=page
pagination.searchid.param=searchid
pagination.sort.asc.value=asc
pagination.sort.desc.value=desc
pagination.sort.skippagenumber=true
为了能自己配置displaytag.properties,我将org/displaytag/properties/中的该文件删除了,然后再classpath的根目录下添加了一个displaytag.properties,并填入配置信息。

国际化版本的displaytag_xx.properties就只保存那些语言相关的信息,比如
basic.msg.empty_list=没有记录被找到

如此对displaytag.properties做了修改之后, 会发现有一个错误,说displaytag.properties找不到了。
看看TableProperties.loadBuiltInProperties():

private static Properties loadBuiltInProperties() throws TablePropertiesLoadException
{
Properties defaultProperties = new Properties();

try
{
InputStream is = TableProperties.class..getResourceAsStream(DEFAULT_FILENAME); //查找TableProperties.class相同路径之下的displaytag.properties

if (is == null)
{
throw new TablePropertiesLoadException(TableProperties.class, DEFAULT_FILENAME, null);
}
defaultProperties.load(is);
}
catch (IOException e)
{
throw new TablePropertiesLoadException(TableProperties.class, DEFAULT_FILENAME, e);
}

return defaultProperties;
}

因为TableProperties.class所在目录下的displaytag.properties已经被删除了, 所以要更改这里的获取方法,改成:
InputStream is = TableProperties.class.getClassLoader().getResourceAsStream(DEFAULT_FILENAME);
这样能找到整个classloader体系之下的classpath中的resource

这样就能正常看到分页了, 不过displaytag.properties的中文显示为乱码。继续修改TableProperties:

private String getProperty(String key)
{
String val = this.properties.getProperty(key);
if (val != null){
try {
val = new String(val.getBytes("ISO8859-1"), "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("UnsupportedEncodingException occured");
}
}

return val;
}

经过如此改造,且让displaytag.properties各类版本文件都保持utf-8编码, 就能够看到正确中文了。

其实displaytag不爽的地方不止以上几个, 还有一个一直想改, 有空想下能否实现。就是当外部分页时,也就是采取动态分页方式,实现PaginatedList接口來控制分页和排序, 不能通过在页面tag中的那个pageSize来设定每页的记录个数, 搞得每个列表的pageSize都是相同的。估计能够通过修改那个TableTag来实现。(针对TableTag做改造,本来想实现PaginatedList根据TableTag的pagesize进行分页, 后来才发现TableTag是页面的内嵌tag,运行在PaginatedList执行之后,因此肯定是拿不到里面的pagesize的,除非将PaginatedList的执行也放到tag中,这样就成了一个真正的tag组件了)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值