Displaytag 全局顺序没问题,但是同页顺序反了
Displaytag global order is Ok but order on the same page is reversed
我正在使用带有外部排序和部分列表的 dsiplaytag。当我使用包含数值(持续时间)的特定列对 table 进行排序时,整个列表在全局范围内是有序的(例如降序),但在每一页上它都是升序的。这是一个例子:
Page 1:
31
32
33
Page 2:
28
29
30
Page 3:
25
26
27
虽然我期望的是:
Page 1:
33
32
31
Page 2:
30
29
28
Page 3:
27
26
25
这是我的 table 的声明:
<display:table name="operations" class="list" requestURI=""
id="operation" pagesize="${applicationScope.pageSize}" partialList="true" size="listSize" sort="external" cellpadding="${paddingValue}"
export="true">
更奇怪的是,在我的 java 代码中,我试图颠倒列表操作的顺序,但无论我以何种顺序发送列表。
关于这种行为有什么想法吗?
经过一个多星期的努力,我找到了解决方案:使用自定义比较器代替 displaytag 默认比较器 (org.displaytag.DefaultComparator
):
这里是比较器的代码:
package com.example.model;
import java.text.Collator;
import java.util.Comparator;
public class CustomComparator implements Comparator
{
private Collator collator;
public CustomComparator()
{
this(Collator.getInstance());
}
public CustomComparator(Collator collatorToUse)
{
this.collator = collatorToUse;
this.collator.setStrength(0);
}
public int compare(Object object1, Object object2)
{
int returnValue;
if ((object1 instanceof String) && (object2 instanceof String))
{
returnValue = this.collator.compare(object1, object2);
}
else
{
if ((object1 instanceof Comparable) && (object2 instanceof Comparable))
{
returnValue = ((Comparable) object1).compareTo(object2);
}
else
{
returnValue = this.collator.compare(object1.toString(), object2.toString());
}
}
return -returnValue;
}
}
与默认比较器的唯一区别是比较方法中的最后一条指令:-returnValue
而不是 returnValue
。
然后,在您的 displaytag.properties 中告诉 displaytag 您想要使用比较器:
comparator.default=com.example.model.CustomComparator
这解决了我的问题。
我正在使用带有外部排序和部分列表的 dsiplaytag。当我使用包含数值(持续时间)的特定列对 table 进行排序时,整个列表在全局范围内是有序的(例如降序),但在每一页上它都是升序的。这是一个例子:
Page 1:
31
32
33
Page 2:
28
29
30
Page 3:
25
26
27
虽然我期望的是:
Page 1:
33
32
31
Page 2:
30
29
28
Page 3:
27
26
25
这是我的 table 的声明:
<display:table name="operations" class="list" requestURI=""
id="operation" pagesize="${applicationScope.pageSize}" partialList="true" size="listSize" sort="external" cellpadding="${paddingValue}"
export="true">
更奇怪的是,在我的 java 代码中,我试图颠倒列表操作的顺序,但无论我以何种顺序发送列表。
关于这种行为有什么想法吗?
经过一个多星期的努力,我找到了解决方案:使用自定义比较器代替 displaytag 默认比较器 (org.displaytag.DefaultComparator
):
这里是比较器的代码:
package com.example.model;
import java.text.Collator;
import java.util.Comparator;
public class CustomComparator implements Comparator
{
private Collator collator;
public CustomComparator()
{
this(Collator.getInstance());
}
public CustomComparator(Collator collatorToUse)
{
this.collator = collatorToUse;
this.collator.setStrength(0);
}
public int compare(Object object1, Object object2)
{
int returnValue;
if ((object1 instanceof String) && (object2 instanceof String))
{
returnValue = this.collator.compare(object1, object2);
}
else
{
if ((object1 instanceof Comparable) && (object2 instanceof Comparable))
{
returnValue = ((Comparable) object1).compareTo(object2);
}
else
{
returnValue = this.collator.compare(object1.toString(), object2.toString());
}
}
return -returnValue;
}
}
与默认比较器的唯一区别是比较方法中的最后一条指令:-returnValue
而不是 returnValue
。
然后,在您的 displaytag.properties 中告诉 displaytag 您想要使用比较器:
comparator.default=com.example.model.CustomComparator
这解决了我的问题。