Struts2中实现自定义分页标签 --功能扩充上一篇结合Struts2实现了分页的自定义标签。标签比较简单,3个参数,单一的显示样式。下面对该标签的功能进行进一步的扩充,主要包括: 1.可以为标签指定样式。通过styleClass属性,可以为标签指定一个样式表。 2.增加了分页样式的选择。通过theme属性指定分页样式。 theme="text"的样式:
theme="number"的样式:
修改方案: 1.在tld文件中增加这两个属性的声明:
<attribute> <name>styleClass</name> <required>false</required> <rtexprvalue>true</rtexprvalue> </attribute> <attribute> <name>theme</name> <required>false</required> <rtexprvalue>true</rtexprvalue> </attribute> 2.在自定义标签类中分别增加styleClass和theme属性,并提供setter方法: PageTag.java
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package com.tangs.tag; import com.opensymphony.xwork2.util.ValueStack; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts2.components.Component; import org.apache.struts2.views.jsp.ComponentTagSupport; /** * 分页标签 * @author tangs */ public class PageTag extends ComponentTagSupport { private String cpage; private String total; private String url; private String styleClass; //新增的样式属性 private String theme; //新增的分页样式属性 public void setTheme(String theme) { this.theme = theme; } public void setStyleClass(String styleClass) { this.styleClass = styleClass; } public void setCpage(String cpage) { this.cpage = cpage; } public void setTotal(String total) { this.total = total; } public void setUrl(String url) { this.url = url; } @Override public Component getBean(ValueStack arg0, HttpServletRequest arg1, HttpServletResponse arg2) { return new Pages(arg0, arg1); } protected void populateParams() { super.populateParams(); Pages pages = (Pages)component; pages.setCpage(cpage); pages.setTotal(total); pages.setUrl(url); pages.setStyleClass(styleClass); pages.setTheme(theme); } } Pages.java
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package com.tangs.tag; import com.opensymphony.xwork2.util.ValueStack; import java.io.IOException; import java.io.Writer; import java.util.logging.Level; import java.util.logging.Logger; import javax.servlet.http.HttpServletRequest; import org.apache.struts2.components.Component; /** * 分页逻辑Bean * @author tangs */ public class Pages extends Component { private HttpServletRequest request; private String cpage; private String total; private String url; private String styleClass; private String theme; public String getTheme() { return theme; } public void setTheme(String theme) { this.theme = theme; } public String getStyleClass() { return styleClass; } public void setStyleClass(String styleClass) { this.styleClass = styleClass; } public String getCpage() { return cpage; } public void setCpage(String cpage) { this.cpage = cpage; } public String getTotal() { return total; } public void setTotal(String total) { this.total = total; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public Pages(ValueStack arg0, HttpServletRequest request) { super(arg0); this.request = request; } @Override public boolean start(Writer writer) { boolean result = super.start(writer); try { StringBuilder str = new StringBuilder(); boolean isValid = true; //从ValueStack中取出数值 if (isValid) { if (total.startsWith("%{") && total.endsWith("}")) { total = total.substring(2, total.length() -1); total = (String)this.getStack().findValue(total); isValid = total == null ? false : true; } else { isValid = false; } } if (isValid) { if (cpage.startsWith("%{") && cpage.endsWith("}")) { cpage = cpage.substring(2, cpage.length() - 1); cpage = (String)this.getStack().findValue(cpage); isValid = cpage == null ? false : true; } else { isValid = false; } } if (isValid) { if (url.startsWith("%{") && url.endsWith("}")) { url = url.substring(2, url.length() - 1); url = (String)this.getStack().findValue(url); isValid = url == null ? false : true; } else { isValid = false; } } if (isValid) { Integer cpageInt = Integer.valueOf(cpage); str.append("<span "); if (styleClass != null) { str.append(" class='"+styleClass+"'>"); } else { str.append(">"); } //文本样式 if (theme == null || "text".equals(theme)) { //theme="text"样式 //当前页与总页数相等 if (cpage.equals(total)) { //如果total = 1,则无需分页,显示“[第1页] [共1页]” if ("1".equals(total)) { str.append("[第 " + cpage + " 页]"); str.append(" [共 " + total + " 页]"); } else { //到达最后一页,显示“[首页] [上一页] [末页]” str.append("<a href='"); str.append(url); str.append("?cpage=1&total="+total+"&url="+url); str.append("'>[首页]</a> <a href='"); str.append(url); str.append("?cpage=" + (cpageInt - 1) + "&total=" + total+"&url="+url); str.append("'>[上一页]</a> <a href='"); str.append(url); str.append("?cpage=" + total + "&total=" + total+"&url="+url); str.append("'>[末页]</a>"); } } else { //当前页与总页数不相同 if ("1".equals(cpage)) { //第一页,显示“[首页] [下一页] [末页]” str.append("<a href='"); str.append(url); str.append("?cpage=1&total="+total+"&url="+url); str.append("'>[首页]</a> <a href='"); str.append(url); str.append("?cpage=" + (cpageInt + 1) + "&total=" + total+"&url="+url); str.append("'>[下一页]</a> <a href='"); str.append(url); str.append("?cpage=" + total + "&total=" + total+"&url="+url); str.append("'>[末页]</a>"); } else { //不是第一页,显示“[首页] [上一页] [下一页] [末页]” str.append("<a href='"); str.append(url); str.append("?cpage=1&total="+total+"&url="+url); str.append("'>[首页]</a> <a href='"); str.append(url); str.append("?cpage=" + (cpageInt - 1) + "&total=" + total+"&url="+url); str.append("'>[上一页]</a> <a href='"); str.append(url); str.append("?cpage=" + (cpageInt + 1) + "&total=" + total+"&url="+url); str.append("'>[下一页]</a> <a href='"); str.append(url); str.append("?cpage=" + total + "&total=" + total+"&url="+url); str.append("'>[末页]</a>"); } } } else if ("number".equals(theme)) { //theme="number"的数字样式 [1 2 3 4 5 6 7 8 9 10 > >>] Integer totalInt = Integer.valueOf(total); //如果只有一页,则无需分页 str.append("[ "); if (totalInt == 1) { str.append("<strong>1</strong> "); } else { //计算一共分几组 int group = (totalInt - 1) / 10 + 1; //当前第几组 int cgroup = (cpageInt - 1) / 10 + 1; if (cgroup > 1) { //当前不是第一组,要显示“<< <” //<<:返回前一组第一页 //<:返回前一页 str.append("<a href='"); str.append(url); str.append("?cpage=" + ((cgroup - 2) * 10 + 1 ) + "&total=" + total+"&url="+url); str.append("'>«</a> " ); str.append("<a href='"); str.append(url); str.append("?cpage=" + (cpageInt - 1) + "&total=" + total+"&url="+url); str.append("'>‹</a> " ); } //10个为一组显示 for (int i = (cgroup - 1) * 10 + 1; i <= totalInt && i <= cgroup * 10; i++) { if (cpageInt == i) { //当前页要加粗显示 str.append("<strong>"); } str.append("<a href='"); str.append(url); str.append("?cpage=" + i + "&total=" + total+"&url="+url); str.append("'>" + i + "</a> "); if (cpageInt == i) { str.append("</strong>"); } } //如果多于1组并且不是最后一组,显示“> >>” if (group > 1&& cgroup != group) { //>>:返回下一组最后一页 //>:返回下一页 str.append("<a href='"); str.append(url); str.append("?cpage=" + (cpageInt + 1) + "&total=" + total+"&url="+url); str.append("'>›</a> " ); str.append("<a href='"); str.append(url); str.append("?cpage=" + ((cgroup * 10 + 10) > totalInt ? totalInt : (cgroup * 10 + 10)) + "&total=" + total+"&url="+url); str.append("'>»</a> " ); } } str.append("]"); } str.append("</span>"); } writer.write(str.toString()); } catch (IOException ex) { Logger.getLogger(Pages.class.getName()).log(Level.SEVERE, null, ex); } return result; } } 3.在页面中使用标签
<tangs:pages cpage="%{cpage}" total="%{total}" url="%{url}" styleClass="page" theme="number"/> 指定了styleClass和theme属性,得到的分页样式:
|
|