jsp页面部分代码:
头部:
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 4 <%@ taglib prefix="s" uri="/struts-tags"%> 5 <%@ page isELIgnored="false" %> 6 <form id="pagerForm" method="post" action="infor/Name.do"> 7 <input type="hidden" name="pageNum" value="1" /> 8 <input type="hidden" name="numPerPage" value="${numPerPage}" /> 9 <input type="hidden" name="orderField" value="${orderField}" /> 10 <input type="hidden" name="orderDirection" value="${orderDirection}" /> 11 </form>
表格部分:
1 <tbody> 2 <s:iterator value = "list"> 3 <tr target="sid_user" rel="1"> 4 <td><input type="checkbox" name="ids" value="${id}"/></td> 5 <td>${nameID}</td> 6 <td>${name}</td> 7 <td>${moddate}</td> 8 <td>${modmanID}</td> 9 <td>${modmanName}</td> 10 <td> 11 <a title="编辑" target="dialog" href="infor/EditorName.do?flag=${id}" class="btnEdit" rel="EditorName">编辑</a> 12 </td> 13 </tr> 14 </s:iterator> 15 16 </tbody>
底边分页部分:
1 <div class="panelBar"> 2 <div class="pages"> 3 <span>每页</span> 4 <select class="combox" name="numPerPage" onchange="navTabPageBreak({numPerPage:this.value})"> 5 <option <s:if test="numPerPage==1">selected="selected"</s:if> value="1" >1</option> 6 7 <option <s:if test="numPerPage==20">selected="selected"</s:if> value="20" >20</option> 8 <option <s:if test="numPerPage==50">selected="selected"</s:if> value="50">50</option> 9 <option <s:if test="numPerPage==100">selected="selected"</s:if> value="100">100</option> 10 <option <s:if test="numPerPage==200">selected="selected"</s:if> value="200">200</option> 11 </select> 12 <span>条,共${totalCount}条</span> 13 </div> 14 <div class="pagination" targetType="${targetType}" totalCount="${totalCount}" numPerPage="${numPerPage}" pageNumShown="10" currentPage="${pageNum}"></div> 15 </div>
XML配置跳转(这个没必要参考):
<action name="Name" class="com.threegrand.www.information.action.Name" method="Name"> <result name="SUCCESS">/content/information/name.jsp</result> </action>
action层:
1 import java.util.List; 2 import java.util.Map; 3 4 import javax.servlet.http.HttpServletRequest; 5 6 import org.apache.struts2.ServletActionContext; 7 8 import com.opensymphony.xwork2.ActionSupport; 9 import com.threegrand.www.information.service.NameService; 10 11 public class Name extends ActionSupport { 12 NameService se = new NameService(); 13 private List<Map<String, Object>> list; 14 private String name; 15 16 private String specimen; 17 private String nameID; 18 private String moddate; 19 private String modmanID; 20 private String modmanName; 21 public NameService getSe() { 22 return se; 23 } 24 public void setSe(NameService se) { 25 this.se = se; 26 } 27 public String getName() { 28 return name; 29 } 30 public void setName(String name) { 31 this.name = name; 32 } 33 public List<Map<String, Object>> getList() { 34 return list; 35 } 36 public void setList(List<Map<String, Object>> list) { 37 this.list = list; 38 } 39 public String getSpecimen() { 40 return specimen; 41 } 42 public void setSpecimen(String specimen) { 43 this.specimen = specimen; 44 } 45 public String getNameID() { 46 return nameID; 47 } 48 public void setNameID(String nameID) { 49 this.nameID = nameID; 50 } 51 public String getModdate() { 52 return moddate; 53 } 54 public void setModdate(String moddate) { 55 this.moddate = moddate; 56 } 57 public String getModmanID() { 58 return modmanID; 59 } 60 public void setModmanID(String modmanID) { 61 this.modmanID = modmanID; 62 } 63 public String getModmanName() { 64 return modmanName; 65 } 66 public void setModmanName(String modmanName) { 67 this.modmanName = modmanName; 68 } 69 70 /* 71 * 分页刷新 72 * 73 */ 74 75 private String loginName = ""; 76 private Integer pageNum=1; 77 private Integer currentPage = 1; // 当前是第几页 78 private String targetType="navTab"; // navTab或dialog,用来标记是navTab上的分页还是dialog上的分页 79 private Integer totalCount=0; // 总条数 80 private Integer numPerPage=20; // 每页显示多少条 81 private Integer pageNumShown=10; // 页标数字多少个 82 private Integer pageCount = 0; // 总页数 83 private String orderField; 84 private String orderDirection; 85 public String getLoginName() { 86 return loginName; 87 } 88 public void setLoginName(String loginName) { 89 this.loginName = loginName; 90 } 91 public Integer getPageNum() { 92 return pageNum; 93 } 94 public void setPageNum(Integer pageNum) { 95 this.pageNum = pageNum; 96 } 97 public Integer getCurrentPage() { 98 return currentPage; 99 } 100 public void setCurrentPage(Integer currentPage) { 101 this.currentPage = currentPage; 102 } 103 public String getTargetType() { 104 return targetType; 105 } 106 public void setTargetType(String targetType) { 107 this.targetType = targetType; 108 } 109 public Integer getTotalCount() { 110 return totalCount; 111 } 112 public void setTotalCount(Integer totalCount) { 113 this.totalCount = totalCount; 114 } 115 public Integer getNumPerPage() { 116 return numPerPage; 117 } 118 public void setNumPerPage(Integer numPerPage) { 119 this.numPerPage = numPerPage; 120 } 121 public Integer getPageNumShown() { 122 return pageNumShown; 123 } 124 public void setPageNumShown(Integer pageNumShown) { 125 this.pageNumShown = pageNumShown; 126 } 127 public Integer getPageCount() { 128 return pageCount; 129 } 130 public void setPageCount(Integer pageCount) { 131 this.pageCount = pageCount; 132 } 133 public String getOrderField() { 134 return orderField; 135 } 136 public void setOrderField(String orderField) { 137 this.orderField = orderField; 138 } 139 public String getOrderDirection() { 140 return orderDirection; 141 } 142 public void setOrderDirection(String orderDirection) { 143 this.orderDirection = orderDirection; 144 } 145 146 public String Name(){ 147 page(); 148 list= se.pagination(loginName,pageNum,numPerPage,orderField,orderDirection); 149 return "SUCCESS"; 150 } 151 152 public void page() { 153 totalCount = se.getCout(loginName); 154 pageCount = totalCount % numPerPage == 0 ? totalCount / numPerPage: totalCount / numPerPage + 1; 155 } 156 157
service层:
1 import java.sql.Connection; 2 import java.sql.PreparedStatement; 3 import java.sql.ResultSet; 4 import java.sql.SQLException; 5 import java.util.ArrayList; 6 import java.util.List; 7 import java.util.Map; 8 9 import javax.servlet.http.HttpServletRequest; 10 import org.apache.struts2.ServletActionContext; 11 import com.threegrand.www.utils.DBUtil; 12 13 public class NameService { 14 DBUtil db = new DBUtil(); 15 List<Map<String,Object>> list; 16 /* 17 * 分页刷新 18 * 19 */ 20 21 private Connection conn=null; 22 private PreparedStatement ps=null; 23 private ResultSet rs=null; 24 25 public Integer getCout(String name) { 26 int count=0; 27 try { 28 String sql="select count(1) from name where 1=1 and name like ?"; 29 conn=db.getConnection(); 30 ps=conn.prepareStatement(sql); 31 ps.setString(1,"%"+name+"%"); 32 rs=ps.executeQuery(); 33 if(rs.next()){ 34 count=rs.getInt(1); 35 } 36 37 } catch (SQLException e) { 38 e.printStackTrace(); 39 }finally{ 40 try { 41 if (rs != null) { 42 rs.close(); 43 } 44 if (ps != null) { 45 ps.close(); 46 } 47 if (conn != null) { 48 conn.close(); 49 } 50 } catch (SQLException e) { 51 e.printStackTrace(); 52 } 53 } 54 return count; 55 } 56 public List<Map<String, Object>> pagination(String name,Integer currentPage,Integer numPerPage,String orderField,String orderDirection){ 57 58 String sql="SELECT id,specimen,nameID,name,moddate,modmanID,modmanName FROM name WHERE name LIKE ‘%%‘ limit "+(currentPage-1)*numPerPage +","+numPerPage; 59 if(orderField!=null&&!"".equals(orderField)){ 60 sql="SELECT id,specimen,nameID,name,moddate,modmanID,modmanName FROM name WHERE name LIKE ‘%%‘ order by "+orderField+" "+orderDirection+" limit "+(currentPage-1)*numPerPage +","+numPerPage; 61 62 } 63 64 list=db.queryToList(sql); 65 66 return list; 67 68 69 } 70
以上内容也是本人参考别人供源的代码作为参考写的,可能不是完全理解,但对比人的代码做了简化,去掉了dao层和model层。
附数据库内容:
下面是效果图:
时间: 2025-01-02 14:18:21