`

displaytag 外部排序

阅读更多

  大家都知道,Display标签处理大数据量的时候,排序会反应迟顿。因此标签的设计者提供了外部排序功能,也就是由用户自己来处理排序逻辑,我参照字面及实际的意思,将external译为外部排序,好象不太准确,谁有更好的译法,请告诉我。
  这是Display官方文档的译文,第一次翻译E文,不妥之处见谅。特意以中英文对照的形式,大家对照着来理解。有两句话没有译出来,谁知道怎么译请告诉我。
  

The default behavior of displaytag is working with a full list, handling sorting and paging trasparently. Sometimes, however, you could need to give displaytag only the records displayed, handling sorting and paging by yourself (this is what we call "partial list support").
displaytag标签的默认行为是使用全部的数据列表,透明的进行排序与分页。可是有时,你可能需要displaytag标签仅显示需要显示的数据,自己来处理排序与分页(这我们称之为“部分列表支持”(这个译法可能不太贴切,大家明白就行,也就是仅使用全部数据列表中的))。

Displaytag 1.1 offers two alternative ways for working with partial lists:
Displaytag 1.1版提供两种可选的方式来处理部分列表支持:

displaytag

the first one uses the valuelist pattern, and requires that the object that you give to displaytag implements the org.displaytag.pagination.PaginatedList interface. You can pass this object to displaytag as an usual list, and it will extract paging and sorting information from it. This way is more recommended if you have to build your backend layer and you can easily follow this pattern.
第一种使用valuelist模式,要求传递给Displaytag标签的对象实现org.displaytag.pagination.PaginatedList接口。这个对象可以象一个一般的数据列表一样传递给Displaytag标签,Displaytag标签将从中提取分页信息及排序信息。如果自己要构建服务端,这种模式是易于使用的,推荐采用。
a second way, recommended if you only have to use partial list for few tables that show a performance problem using full lists, is passing all the needed parameters as separate tag attributes (recors to be shown, page number, total number of records...)
第二种方式,在由于性能问题,仅需使用部分列表显示为数不多的几个表情况下,推荐采用。传递所有需要的参数作为分开的标签属性(如:页码、记录总数)。


Using the Valuelist pattern
Valuelist模式
If the object passed to the displaytag as a list is in fact an instance of org.displaytag.pagination.PaginatedList (and not a collection), displaytag will assume that the list is externally sorted and paginated. Implementing this interface is trivial, and an adapter over existing classes can easily be written. See the org.displaytag.pagination.PaginatedList javadocs for more details.
如果传递给displaytag标签的列表实际上是org.displaytag.pagination.PaginatedList的实例(也就是不是集合),标签会认为这个列表是外部排好序、分好页的。如何实现org.displaytag.pagination.PaginatedList接口就没有必要介绍了,可以很容易的根据已经存在的类改写。可以查看javadocs的相关部分了解更多细节。

In order for the generated pagination and sort links to be "understandable" by the application, they are not generated the same way as for a traditional DisplayTag table. Indeed, since the table is externally sorted and paginated, the application must be able to easily get the sort and pagination parameters, without having to decode the DisplayTag encoded parameters.
这种模式下,为了产生可被应用“理解”的分页及排序链接,使用与传统DisplayTag表格不同的方法。实际上,既然由外部排序与分页,应用必须能够容易地得到排序与分页参数,而不是去解码由标签编码的参数。

These sort and pagination links are generated based on additional properties. These properties can be overridden using, in order to be able to have different sort of page parameters in the application, or to have multiple tables in a single HTML page.
排序及分页的链接是基于额外的属性。这些参数可以使用<display:setProperty/>改写,以便能在一个HTML页面中处理不同的排序参数,或者处理多个表

If the default values of these properties are used, the href of the sort links will look like this:
如果这些属性能缺省值,排序链接地址形式如下所示:


http://foo.bar.com/context/requestUri?sort=name&dir=asc&originalParameters=originalValues.
The href of the pagination links will look like this:
分页链接地址形式如下:
http://foo.bar.com/context/requestUri?sort=name&dir=asc&page=5&originalParameters=originalValues

Without implementing the PaginatedList interface
不实现PaginatedList接口的解决办法


External sort
外部排序

<display:table name="testList" sort="external" defaultsort="1" id="element">
<display:column property="id" title="ID" sortable="true" sortName="id" />
<display:column property="firstName" sortable="true" sortName="firstName" title="First Name" />
<display:column property="lastName" sortable="true" sortName="lastName" title="Last Name" />
<display:column property="address" sortable="true" sortName="address" title="Email Address"/>
</display:table>
When using the sort="external" property you are telling DisplayTag that your list is already sorted according to some specific column before the tag gets it. This is a must when used with partial lists as we will see below, however you can use it by itself. A new property named sortName has been addeed to the column tag. The purpose of this property is for convenience when writing the backend code that actually does the sorting of the data. For example the header link for the column with property id in the sample code above will render a link containing d-(encoded table id)-s=id. If you remove the sortName property from the column the link will instead contain d-(encoded table id)-s=0, which is the index of the column rather than the name. Once again it is solely for convenience.
使用sort="external"属性,告诉DisplayTag标签在它获取数据列表前,列表已经根据特定的列表排好序了。通过下面的例子可以看到,当使用部分列表的时候这是必须的,但是......(这句话没有搞懂怎么翻译,原文为:however you can use it by itself)。列标签添加了一个新的属性:sortName。这是为了便于后台数据排序代码进行处理。例如,上例中id列的排序链接会包含d-(encoded table id)-s=id,如果不使用sortName属性,排序链接相应的会变为包含d-(encoded table id)-s=0,注意,使用的是列的序号而不是列名。再重申一次,这只是为了方便后台代码的编写。

The first time you access a jsp with a DisplayTag in it, there are two ways for the tag to know how your data is sorted. Firstly it will check for a request parameter d-(encoded table id)-s. If that parameter exists, and its value matches the sortName of one of the declared column tags, then when it is rendered to html it will set the matching column as being sorted. If that parameter does not exist it will examine the defaultsort property of the table tag, if it exists then it will render the corresponding column as sorted. Otherwise no column will be rendered as being sorted. So really you have two options. Set your backend to sort your data based on a set column when there is no request parameter, and set your defaultsort parameter to match that column. Or ensure that all links pointing to your page that houses the DisplayTag have a d-(encoded table id)-s=(column index or sortName) parameter in them.
第一次在页面中使用DisplayTag标签的时候,标签有两种方式知道数据是如何排序的。第一种方式为:检测 d-(encoded table id)-s参数,如果参数存在,并且与某一列的sortName属性一致辞,那么标签就以匹配列为排序列来生成HTML代码。如果参数不存在,就检查缺省的排序列,如果存在就以生成以缺省列进行排序的HTML代码。不满足以上情况,就生成的HTML代码是不以任何列进行排序的。所以你有两个选择,当无request参数的时候,后台代码使用特定的列时行排序,然后defaultsor使用该列为默认排序列;或者使所有构筑DisplayTag标签的链接都包含参数:d-(encoded table id)-s=(column index or sortName)。

To determine the request parameter name for column sorting you can do:
可以通过下面的方法获取request参数的名字:


new ParamEncoder(tableId).encodeParameterName(TableTagParameters.PARAMETER_SORT))
To get the order (ASC/DESC):
获取排序request参数名字的方法为:


new ParamEncoder(tableId).encodeParameterName(TableTagParameters.PARAMETER_ORDER))
Descending is 2, Ascending is 1.
降序为2,升序为1。

 

External Sorting And Paging (Partial Lists)
外部排序与分页(部分列表)

<display:table name="testList" sort="external" defaultsort="1" pagesize="20" id="element" partialList="true" size="resultSize">
<display:column property="id" title="ID" sortable="true" sortName="id" />
<display:column property="firstName" sortable="true" sortName="firstName" title="First Name" />
<display:column property="lastName" sortable="true" sortName="lastName" title="Last Name" />
<display:column property="address" sortable="true" sortName="address" title="Email Address"/>
</display:table>
All of the above information for external sorting applies to partial lists. Additionally there are two new properties on the table tag: partialList and size. To enable partial lists simply set partialList="true" on the table tag. Next you must tell Display Tag the total size of your list since you are only making a specific subset of it available to the tag. You do this by storing the total list size in a java.lang.Integer object and putting it into some scope available to DisplayTag. Then you set size=(name the Integer was stored under) on the table tag. Very easy. It is also important that you specify a pagesize on the table tag. It is also a good idea to set your backend to load a default amount of your list when there are no request parameters, probably the first page worth of data.
上列展示了所有与部分列表外部排序有关的参数。注意,出现了两个新的标签属性:partialList及size。使用部分列表只需简单的设定partialList="true"。接下来,既然你仅使用全部数据的子集,那么必须告诉标签数据列表的中数据的数量。告诉的方法就是,在DisplayTag可以访问到范围(sessionScope、request等),使用java.lang.Integer类型的对象来存储整个数据列表的数量,然后在标签中设定size等于上面存储数量时用的名字。非常容易。表格标签设定pagesize参数也是很重要的。当没有request参数的时候,后台代码装载一个默认的列表数量是个好主意,可能......(这句没有搞懂是什么意思,原文为:probably the first page worth of data)。

To determine the starting record you need to return based on the request parameters provided via Display Tag you can do:
使用Display标签提供的request参数可以得到开始的记录,方法如下:


(Integer.parseInt(request.getParameter((new ParamEncoder(tableId).encodeParameterName(TableTagParameters.PARAMETER_PAGE)))) - 1) * pageSize.
To get the ending record just add pageSize to the above value.
将上面的值加上pageSize就可以得到结束记录。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics