我的项目中要用到分页功能,发现struts2居然没有相应的标签
后来在struts2的核心包中发现有Counter类。
org.apache.struts2.util.Counter
查看源码,原来它可以实现循环列出 1 2 3 4 5 6 7 8 9 10等等,源码如下:
public class Counter implements java.util.Iterator, Serializable {
private static final long serialVersionUID = 2796965884308060179L;
boolean wrap = false;
// Attributes ----------------------------------------------------
long first = 1;
long current = first;
long interval = 1;
long last = -1;
public void setAdd(long addition) {
current += addition;
}
public void setCurrent(long current) {
this.current = current;
}
public long getCurrent() {
return current;
}
public void setFirst(long first) {
this.first = first;
current = first;
}
public long getFirst() {
return first;
}
public void setInterval(long interval) {
this.interval = interval;
}
public long getInterval() {
return interval;
}
public void setLast(long last) {
this.last = last;
}
public long getLast() {
return last;
}
// Public --------------------------------------------------------
public long getNext() {
long next = current;
current += interval;
if (wrap && (current > last)) {
current -= ((1 + last) - first);
}
return next;
}
public long getPrevious() {
current -= interval;
if (wrap && (current < first)) {
current += (last - first + 1);
}
return current;
}
public void setWrap(boolean wrap) {
this.wrap = wrap;
}
public boolean isWrap() {
return wrap;
}
public boolean hasNext() {
return ((last == -1) || wrap) ? true : (current <= last);
}
public Object next() {
return new Long(getNext());
}
我的项目中,实现代码如下:
<s:bean name="org.apache.struts2.util.Counter" id="counter">
<s:param name="first" value="#first" />
<s:param name="last" value="#zs" />
<s:iterator>
<s:a href="?dhname=mymes&name=allmes&pageid=%{#counter.current-1 }"><s:property value="#counter.current-1"/></s:a>
</s:iterator>
</s:bean>
first表示起始数,第一个数字显示几
zs表示结束数,显示几个。
比如 2 3 4 5 6 7 8 表示起始为2,显示了7个
爆款云服务器s6 2核4G 低至0.46/天,具体规则查看活动详情