WEB开发网
开发学院软件开发Java 分页标签 阅读

分页标签

 2009-09-26 00:00:00 来源:WEB开发网   
核心提示: 2.Pages.java1packagecn.com.sunrise.home.util;23importjava.io.IOException;4importjava.io.Writer;5importjava.util.Map;6importjava.util.logging.Level;7

2.Pages.java

  1package cn.com.sunrise.home.util;   
  2  
  3import java.io.IOException;   
  4import java.io.Writer;   
  5import java.util.Map;   
  6import java.util.logging.Level;   
  7import java.util.logging.Logger;   
  8  
  9import javax.servlet.http.HttpServletRequest;   
 10  
 11import org.apache.struts2.StrutsStatics;   
 12import org.apache.struts2.components.Component;   
 13import org.apache.struts2.dispatcher.StrutsRequestWrapper;   
 14  
 15import com.opensymphony.xwork2.util.ValueStack;   
 16  
 17/**  
 18 * 分页逻辑Bean  
 19 * @author xiongwei
 20 */  
 21public class Pages extends Component {   
 22       
 23    private HttpServletRequest request;   
 24       
 25    private String pageNo;   
 26    private String total;   
 27    private String styleClass;   
 28    private String theme;   
 29    private String includes;   
 30       
 31    public String getTheme() {   
 32        return theme;   
 33    }   
 34    public void setTheme(String theme) {   
 35        this.theme = theme;   
 36    }       
 37    public String getIncludes() {   
 38        return includes;   
 39    }   
 40    public void setIncludes(String includes) {   
 41        this.includes = includes;   
 42    }   
 43    public String getStyleClass() {   
 44        return styleClass;   
 45    }   
 46    public void setStyleClass(String styleClass) {   
 47        this.styleClass = styleClass;   
 48    }       
 49    public String getPageNo() {   
 50        return pageNo;   
 51    }   
 52    public void setPageNo(String pageNo) {   
 53        this.pageNo = pageNo;   
 54    }   
 55    public String getTotal() {   
 56        return total;   
 57    }   
 58    public void setTotal(String total) {   
 59        this.total = total;   
 60    }   
 61       
 62       
 63    public Pages(ValueStack arg0, HttpServletRequest request) {   
 64        super(arg0);   
 65        this.request = request;   
 66    }   
 67  
 68    @Override  
 69    public boolean start(Writer writer) {   
 70           
 71        boolean result = super.start(writer);   
 72        try {   
 73            StringBuilder str = new StringBuilder();   
 74            Map cont=this.getStack().getContext();   
 75            StrutsRequestWrapper req=(StrutsRequestWrapper)cont.get(StrutsStatics.HTTP_REQUEST);   
 76               
 77            String url=(String)req.getAttribute("javax.servlet.forward.request_uri");   
 78                          
 79            //从ValueStack中取出数值   
 80            Object obj=this.getStack().findValue(pageNo);               
 81            pageNo = String.valueOf((Integer)obj);     
 82            obj=this.getStack().findValue(total);   
 83            total = String.valueOf((Integer)obj);     
 84               
 85            StringBuffer perUrl=new StringBuffer("");   
 86            if(includes!=null){   
 87                String[] perm=includes.split(",");   
 88                for(int i=0;i<perm.length;i++){   
 89                    String permName=perm[i];   
 90                    Object obje=this.getStack().findValue(permName);   
 91                     String vType=obje.getClass().toString();   
 92                       
 93                    vType=vType.substring(vType.lastIndexOf(".")+1,vType.length());                        
 94                    perUrl.append("&"+permName);   
 95                    if(vType.equals("String")){   
 96                        String tmp= (String)this.getStack().findValue(permName);   
 97                        perUrl.append("="+tmp);   
 98                    }else if(vType.equals("Long")){   
 99                        Long tmp= (Long)this.getStack().findValue(permName);   
100                        perUrl.append("="+tmp);   
101                    }else if(vType.equals("Float")){   
102                        Float tmp= (Float)this.getStack().findValue(permName);   
103                        perUrl.append("="+tmp);   
104                    }else if(vType.equals("Integer")){   
105                        Integer tmp= (Integer)this.getStack().findValue(permName);   
106                        perUrl.append("="+tmp);   
107                    }else if(vType.equals("Boolean")){   
108                        Boolean tmp= (Boolean)this.getStack().findValue(permName);   
109                        perUrl.append("="+tmp);   
110                    }   
111                }                  
112            }   
113               
114               
115            Integer cpageInt = Integer.valueOf(pageNo);   
116            str.append("<span ");   
117            if (styleClass != null) {   
118                str.append(" class='"+styleClass+"'>");   
119            } else {   
120                str.append(">");   
121            }   
122            //文本样式   
123            if (theme == null || "text".equals(theme)) {   
124                //当前页与总页数相等   
125                if (pageNo.equals(total)) {   
126                    //如果total = 1,则无需分页,显示“[第1页] [共1页]”   
127                    if ("1".equals(total)) {   
128                         
129                    } else {   
130                        //到达最后一页,显示“[首页] [上一页] [末页]” 
131                        str.append("<td>   <br></td>");
132                        str.append("<td><a href='"+url+"?pageNo=1"+perUrl+"'><font class='luntanbmy'>首页</font></a></td>");
133                        str.append("<td>   <br></td>");  
134                        str.append("<td><a href='"+url+"?pageNo=" + (cpageInt - 1)+perUrl+"'><font class='luntanbmy'>上一页</font></a></td>"); 
135                        str.append("<td>   <br></td>");
136                        str.append("<td><a href='#'><font class='luntanbmy'>下一页</font></a></td>");
137                        str.append("<td>   <br></td>");
138                        str.append("<td><a href='"+url+"?pageNo=" + total+perUrl+"'><font class='luntanbmy'>最后一页</font></a></td>");
139                        str.append("<td>   <br></td>");
140                        str.append("<td class='lqbz'>共<span style='color: red'>" + total + "</span>页</td>"); 
141                        str.append("<td>   <br></td>");
142                        str.append("<td class='lqbz'>当前第<span style='color: red'>" + pageNo + "</span>页</td>"); 
143                    }   
144                } else {   
145                    //当前页与总页数不相同   
146                    if ("1".equals(pageNo)) {   
147                        //第一页,显示“[首页] [下一页] [末页]”   
148                        str.append("<td>   <br></td>");
149                        str.append("<td><a href='"+url+"?pageNo=1"+perUrl+"'><font class='luntanbmy'>首页</font></a></td>");
150                        str.append("<td>   <br></td>");  
151                        str.append("<td><a href='#'><font class='luntanbmy'>上一页</font></a></td>"); 
152                        str.append("<td>   <br></td>");
153                        str.append("<td><a href='"+url+"?pageNo=" + (cpageInt + 1) +perUrl+"'><font class='luntanbmy'>下一页</font></a></td>");
154                        str.append("<td>   <br></td>");
155                        str.append("<td><a href='"+url+"?pageNo=" + total+perUrl+"'><font class='luntanbmy'>最后一页</font></a></td>");
156                        str.append("<td>   <br></td>");
157                        str.append("<td class='lqbz'>共<span style='color: red'>" + total + "</span>页</td>"); 
158                        str.append("<td>   <br></td>");
159                        str.append("<td class='lqbz'>当前第<span style='color: red'>" + pageNo + "</span>页</td>"); 
160                    } else {   
161                        //不是第一页,显示“[首页] [上一页] [下一页] [末页]”   
162                        str.append("<td>   <br></td>");
163                        str.append("<td><a href='"+url+"?pageNo=1"+perUrl+"'><font class='luntanbmy'>首页</font></a></td>");
164                        str.append("<td>   <br></td>");  
165                        str.append("<td><a href='"+url+"?pageNo=" + (cpageInt - 1)+perUrl+"'><font class='luntanbmy'>上一页</font></a></td>"); 
166                        str.append("<td>   <br></td>");
167                        str.append("<td><a href='"+url+"?pageNo=" + (cpageInt + 1) +perUrl+"'><font class='luntanbmy'>下一页</font></a></td>");
168                        str.append("<td>   <br></td>");
169                        str.append("<td><a href='"+url+"?pageNo=" + total+perUrl+"'><font class='luntanbmy'>最后一页</font></a></td>");
170                        str.append("<td>   <br></td>");
171                        str.append("<td class='lqbz'>共<span style='color: red'>" + total + "</span>页</td>"); 
172                        str.append("<td>   <br></td>");
173                        str.append("<td class='lqbz'>当前第<span style='color: red'>" + pageNo + "</span>页</td>"); 
174                    }   
175                }   
176            } 
177            str.append("</span>");   
178               
179            writer.write(str.toString());   
180               
181        } catch (IOException ex) {   
182            Logger.getLogger(Pages.class.getName()).log(Level.SEVERE, null, ex);   
183        }   
184        return result;   
185    }   
186} 

上一页  1 2 3 4  下一页

Tags:标签

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接