springMVC中通用的分頁配置
阿新 • • 發佈:2019-01-09
在前面的文章中,我們一步步完成了springmvc的maven工程的搭建,整合mybatis,日誌,等操作,本文在此基礎上繼續新增通用的分頁操作,並貼出原始碼供給參考。
下面先講解本文通用的分頁配置,其實通用目前僅支援oracle和mysql的兩大主流資料庫分頁
一,此前的專案下建立新的實體類Page,並新增util包新增幾個工具類,最後修改我們原先的mybatis-config.xml配置檔案即可
page.java:用於封裝分頁的實體類
package com.test.entity; import com.test.util.PageData; import java.io.Serializable; import java.util.ArrayList; import java.util.List; /** * 分頁類 * @author yufeng * 建立時間:2017-10-12 */ public class Page implements Serializable { /** * */ private static final long serialVersionUID = 1L; private int showCount = 5; //每頁顯示記錄數,預設為5條 private int totalPage; //總頁數 private int totalResult; //總記錄數 private int currentPage; //當前頁 private int currentResult; //當前記錄起始索引 private boolean entityOrField; //true:需要分頁的地方,傳入的引數就是Page實體;false:需要分頁的地方,傳入的引數所代表的實體擁有Page屬性 private String pageStr; //最終頁面顯示的底部翻頁導航,詳細見:getPageStr(); private String formName;//分頁時所要提交的form的名字 private PageData pd = new PageData(); private List<PageData> list = new ArrayList<PageData>(); public int getTotalPage() { if (totalResult % showCount == 0) totalPage = totalResult / showCount; else totalPage = totalResult / showCount + 1; return totalPage; } public void setTotalPage(int totalPage) { this.totalPage = totalPage; } public int getTotalResult() { return totalResult; } public void setTotalResult(int totalResult) { this.totalResult = totalResult; } public int getCurrentPage() { if (currentPage <= 0) currentPage = 1; if (currentPage > getTotalPage()) currentPage = getTotalPage(); return currentPage; } public void setCurrentPage(int currentPage) { this.currentPage = currentPage; } //拼接分頁 頁面及JS函式 public String getPageStr() { StringBuffer sb = new StringBuffer(); if(totalResult>0){ sb.append(" <ul class=\"fanyecontrol\">\n"); if(currentPage==1){ sb.append(" <li class=\"fyclicked\"><a>首頁</a></li>\n"); sb.append(" <li class=\"fyclicked\"><a>上一頁</a></li>\n"); }else{ sb.append(" <li style=\"cursor:pointer;\" class=\"fyactive\"><a onclick=\"nextPage(1)\">首頁</a></li>\n"); sb.append(" <li style=\"cursor:pointer;\" class=\"fyactive\"><a onclick=\"nextPage("+(currentPage-1)+")\">上一頁</a></li>\n"); } int showTag = 5;//分頁標籤顯示數量 int startTag = 1; if(currentPage>showTag){ startTag = currentPage-1; } int endTag = startTag+showTag-1; for(int i=startTag; i<=totalPage && i<=endTag; i++){ if(currentPage==i){ sb.append("<li class=\"fyonclick\"><a>"+i+"</a></li>\n"); }else{ sb.append(" <li style=\"cursor:pointer;\" class=\"fyactive\"><a onclick=\"nextPage("+i+")\">"+i+"</a></li>\n"); } } if(currentPage==totalPage){ sb.append(" <li class=\"fyclicked\"><a>下一頁</a></li>\n"); sb.append(" <li class=\"fyclicked\"><a>末頁</a></li>\n"); }else{ sb.append(" <li style=\"cursor:pointer;\" class=\"fyactive\"><a onclick=\"nextPage("+(currentPage+1)+")\">下一頁</a></li>\n"); sb.append(" <li style=\"cursor:pointer;\" class=\"fyactive\"><a onclick=\"nextPage("+totalPage+")\">末頁</a></li>\n"); } sb.append(" <li>共<span>"+totalPage+"</span>頁</li>\n"); sb.append(" <li>到第<input type=\"number\" value=\"\" id=\"toGoPage\" class=\"selectpage\"/>頁</li>\n"); sb.append(" <li class=\"surebtn\"><a onclick=\"toTZ();\">確定</a></li>\n"); sb.append("</ul>\n"); sb.append("<script type=\"text/javascript\">\n"); //換頁函式 sb.append("function nextPage(page){"); sb.append(" if(true && document.forms[0]){\n"); sb.append(" var url = document.forms[0].getAttribute(\"action\");\n"); sb.append(" if(url.indexOf('?')>-1){url += \"&"+(entityOrField?"currentPage":"page.currentPage")+"=\";}\n"); sb.append(" else{url += \"?"+(entityOrField?"currentPage":"page.currentPage")+"=\";}\n"); sb.append(" url = url + page + \"&" +(entityOrField?"showCount":"page.showCount")+"="+showCount+"\";\n"); sb.append(" document.forms[0].action = url;\n"); sb.append(" document.forms[0].submit();\n"); sb.append(" }else{\n"); sb.append(" var url = document.location+'';\n"); sb.append(" if(url.indexOf('?')>-1){\n"); sb.append(" if(url.indexOf('currentPage')>-1){\n"); sb.append(" var reg = /currentPage=\\d*/g;\n"); sb.append(" url = url.replace(reg,'currentPage=');\n"); sb.append(" }else{\n"); sb.append(" url += \"&"+(entityOrField?"currentPage":"page.currentPage")+"=\";\n"); sb.append(" }\n"); sb.append(" }else{url += \"?"+(entityOrField?"currentPage":"page.currentPage")+"=\";}\n"); sb.append(" url = url + page + \"&" +(entityOrField?"showCount":"page.showCount")+"="+showCount+"\";\n"); sb.append(" document.location = url;\n"); sb.append(" }\n"); sb.append("}\n"); //跳轉函式 sb.append("function toTZ(){"); sb.append("var toPaggeVlue = document.getElementById(\"toGoPage\").value;"); sb.append("if(toPaggeVlue == ''){document.getElementById(\"toGoPage\").value=1;return;}"); sb.append("if(isNaN(Number(toPaggeVlue))){document.getElementById(\"toGoPage\").value=1;return;}"); sb.append("nextPage(toPaggeVlue);"); sb.append("}\n"); sb.append("</script>\n"); } pageStr = sb.toString(); return pageStr; } /** * ajax-分頁 */ public String getAjaxPageStr() { StringBuffer sb = new StringBuffer(); if (totalResult > 0) { sb.append(" <ul class=\"newpagination\">\n"); if (currentPage == 1) { sb.append(" <li><a><</a></li>\n"); } else { sb.append(" <li style=\"cursor:pointer;\" onclick=\"getPagInfo(" + (currentPage - 1) + ")\"><a><</a></li>\n"); } int showTag = 5;//分頁標籤顯示數量 int startTag = 1; if (currentPage > showTag) { sb.append("<li>...</li>\n"); startTag = currentPage - 1; } int endTag = startTag + showTag - 1; for (int i = startTag; i <= totalPage && i <= endTag; i++ ) { if (currentPage == i) { sb.append("<li class=\"active\"><a><font color='white'>" + i + "</font></a></li>\n"); } else { sb.append(" <li style=\"cursor:pointer;\" onclick=\"getPagInfo(" + i + ")\"><a>" + i + "</a></li>\n"); } } if (endTag < totalPage) { sb.append("<li>...</li>\n"); } if (currentPage == totalPage) { sb.append(" <li><a>></a></li>\n"); } else { sb.append(" <li style=\"cursor:pointer;\" onclick=\"getPagInfo(" + (currentPage + 1) + ")\"><a>></a></li>\n"); } sb.append("</ul>\n"); sb.append("<script type=\"text/javascript\">\n"); sb.append("</script>\n"); } pageStr = sb.toString(); return pageStr; } public void setPageStr(String pageStr) { this.pageStr = pageStr; } public int getShowCount() { return showCount; } public void setShowCount(int showCount) { this.showCount = showCount; } public int getCurrentResult() { currentResult = (getCurrentPage() - 1) * getShowCount(); if (currentResult < 0) currentResult = 0; return currentResult; } public void setCurrentResult(int currentResult) { this.currentResult = currentResult; } public boolean isEntityOrField() { return entityOrField; } public void setEntityOrField(boolean entityOrField) { this.entityOrField = entityOrField; } public String getFormName() { return formName; } public void setFormName(String formName) { this.formName = formName; } public PageData getPd() { return pd; } public void setPd(PageData pd) { this.pd = pd; } public List<PageData> getList() { return list; } public void setList(List<PageData> list) { this.list = list; } }
PageData.java:封裝了map型別(相當於map型別),並封裝了頁面http用於請求的request引數放入此型別中
PagePlugin.java:用於分頁的外掛工具類,根據不同的資料庫,執行不同的查詢分頁方法:package com.test.util; import java.io.Serializable; import java.util.Collection; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Set; import javax.servlet.http.HttpServletRequest; /** * 說明:引數封裝Map * 建立人:yufeng * 修改時間 2017-10-12 */ @SuppressWarnings("rawtypes") public class PageData extends HashMap implements Map, Serializable { private static final long serialVersionUID = 1L; transient Map map = null; HttpServletRequest request; @SuppressWarnings("unchecked") public PageData(HttpServletRequest request) { this.request = request; Map properties = request.getParameterMap(); Map returnMap = new HashMap(); Iterator entries = properties.entrySet().iterator(); Map.Entry entry; String name = ""; String value = ""; while (entries.hasNext()) { entry = (Map.Entry) entries.next(); name = (String) entry.getKey(); Object valueObj = entry.getValue(); if (null == valueObj) { value = ""; } else if (valueObj instanceof String[]) { String[] values = (String[]) valueObj; for (int i = 0; i < values.length; i++) { value = values[i] + ","; } value = value.substring(0, value.length() - 1); } else { value = valueObj.toString(); } returnMap.put(name, value); } map = returnMap; } public PageData() { map = new HashMap(); } @Override public Object get(Object key) { Object obj = null; if (map.get(key) instanceof Object[]) { Object[] arr = (Object[]) map.get(key); obj = request == null ? arr : (request.getParameter((String) key) == null ? arr : arr[0]); } else { obj = map.get(key); } return null != obj ? obj : ""; } public String getString(Object key) { return (String) get(key); } @SuppressWarnings("unchecked") @Override public Object put(Object key, Object value) { return map.put(key, value); } @Override public Object remove(Object key) { return map.remove(key); } public void clear() { map.clear(); } public boolean containsKey(Object key) { return map.containsKey(key); } public boolean containsValue(Object value) { return map.containsValue(value); } public Set entrySet() { return map.entrySet(); } public boolean isEmpty() { return map.isEmpty(); } public Set keySet() { return map.keySet(); } @SuppressWarnings("unchecked") public void putAll(Map t) { map.putAll(t); } public int size() { return map.size(); } public Collection values() { return map.values(); } }
package com.test.util; import java.lang.reflect.Field; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.List; import java.util.Properties; import javax.xml.bind.PropertyException; import org.apache.ibatis.executor.ErrorContext; import org.apache.ibatis.executor.ExecutorException; import org.apache.ibatis.executor.statement.BaseStatementHandler; import org.apache.ibatis.executor.statement.RoutingStatementHandler; import org.apache.ibatis.executor.statement.StatementHandler; import org.apache.ibatis.mapping.BoundSql; import org.apache.ibatis.mapping.MappedStatement; import org.apache.ibatis.mapping.ParameterMapping; import org.apache.ibatis.mapping.ParameterMode; import org.apache.ibatis.plugin.Interceptor; import org.apache.ibatis.plugin.Intercepts; import org.apache.ibatis.plugin.Invocation; import org.apache.ibatis.plugin.Plugin; import org.apache.ibatis.plugin.Signature; import org.apache.ibatis.reflection.MetaObject; import org.apache.ibatis.reflection.property.PropertyTokenizer; import org.apache.ibatis.scripting.xmltags.ForEachSqlNode; import org.apache.ibatis.session.Configuration; import org.apache.ibatis.type.TypeHandler; import org.apache.ibatis.type.TypeHandlerRegistry; import org.apache.log4j.Logger; import org.springframework.util.StringUtils; import com.test.entity.Page; /** * * 類名稱:分頁外掛 * 類描述: * @author yufneg * 作者單位: * 聯絡方式: * 修改時間:2017-10-12 * @version 1.0 */ @Intercepts({@Signature(type=StatementHandler.class,method="prepare",args={Connection.class})}) public class PagePlugin implements Interceptor { private static String dialect = ""; //資料庫方言 private static String pageSqlId = ""; //mapper.xml中需要攔截的ID(正則匹配) private static final Logger logger = Logger.getLogger(PagePlugin.class); public Object intercept(Invocation ivk) throws Throwable { if(ivk.getTarget() instanceof RoutingStatementHandler){ RoutingStatementHandler statementHandler = (RoutingStatementHandler)ivk.getTarget(); BaseStatementHandler delegate = (BaseStatementHandler) ReflectHelper.getValueByFieldName(statementHandler, "delegate"); MappedStatement mappedStatement = (MappedStatement) ReflectHelper.getValueByFieldName(delegate, "mappedStatement"); if(mappedStatement.getId().matches(pageSqlId)){ //攔截需要分頁的SQL BoundSql boundSql = delegate.getBoundSql(); Object parameterObject = boundSql.getParameterObject();//分頁SQL<select>中parameterType屬性對應的實體引數,即Mapper介面中執行分頁方法的引數,該引數不得為空 if(parameterObject==null){ throw new NullPointerException("parameterObject尚未例項化!"); }else{ Connection connection = (Connection) ivk.getArgs()[0]; String sql = boundSql.getSql(); //String countSql = "select count(0) from (" + sql+ ") as tmp_count"; //記錄統計 String fhsql = sql; String countSql = "select count(0) from (" + fhsql+ ") tmp_count"; //記錄統計 == oracle 加 as 報錯(SQL command not properly ended) PreparedStatement countStmt = connection.prepareStatement(countSql); BoundSql countBS = new BoundSql(mappedStatement.getConfiguration(),countSql,boundSql.getParameterMappings(),parameterObject); setParameters(countStmt,mappedStatement,countBS,parameterObject); ResultSet rs = countStmt.executeQuery(); int count = 0; if (rs.next()) { count = rs.getInt(1); } rs.close(); countStmt.close(); Page page = null; if(parameterObject instanceof Page){ //引數就是Page實體 page = (Page) parameterObject; page.setEntityOrField(true); page.setTotalResult(count); }else{ //引數為某個實體,該實體擁有Page屬性 Field pageField = ReflectHelper.getFieldByFieldName(parameterObject,"page"); if(pageField!=null){ page = (Page) ReflectHelper.getValueByFieldName(parameterObject,"page"); if(page==null) page = new Page(); page.setEntityOrField(false); page.setTotalResult(count); ReflectHelper.setValueByFieldName(parameterObject,"page", page); //通過反射,對實體物件設定分頁物件 }else{ throw new NoSuchFieldException(parameterObject.getClass().getName()+"不存在 page 屬性!"); } } String pageSql = generatePageSql(sql,page); ReflectHelper.setValueByFieldName(boundSql, "sql", pageSql); //將分頁sql語句反射回BoundSql. } } } return ivk.proceed(); } /** * 對SQL引數(?)設值,參考org.apache.ibatis.executor.parameter.DefaultParameterHandler * @param ps * @param mappedStatement * @param boundSql * @param parameterObject * @throws SQLException */ @SuppressWarnings({"rawtypes", "unchecked"}) private void setParameters(PreparedStatement ps,MappedStatement mappedStatement,BoundSql boundSql,Object parameterObject) throws SQLException { ErrorContext.instance().activity("setting parameters").object(mappedStatement.getParameterMap().getId()); List<ParameterMapping> parameterMappings = boundSql.getParameterMappings(); if (parameterMappings != null) { Configuration configuration = mappedStatement.getConfiguration(); TypeHandlerRegistry typeHandlerRegistry = configuration.getTypeHandlerRegistry(); MetaObject metaObject = parameterObject == null ? null: configuration.newMetaObject(parameterObject); for (int i = 0; i < parameterMappings.size(); i++) { ParameterMapping parameterMapping = parameterMappings.get(i); if (parameterMapping.getMode() != ParameterMode.OUT) { Object value; String propertyName = parameterMapping.getProperty(); PropertyTokenizer prop = new PropertyTokenizer(propertyName); if (parameterObject == null) { value = null; } else if (typeHandlerRegistry.hasTypeHandler(parameterObject.getClass())) { value = parameterObject; } else if (boundSql.hasAdditionalParameter(propertyName)) { value = boundSql.getAdditionalParameter(propertyName); } else if (propertyName.startsWith(ForEachSqlNode.ITEM_PREFIX)&& boundSql.hasAdditionalParameter(prop.getName())) { value = boundSql.getAdditionalParameter(prop.getName()); if (value != null) { value = configuration.newMetaObject(value).getValue(propertyName.substring(prop.getName().length())); } } else { value = metaObject == null ? null : metaObject.getValue(propertyName); } TypeHandler typeHandler = parameterMapping.getTypeHandler(); if (typeHandler == null) { throw new ExecutorException("There was no TypeHandler found for parameter "+ propertyName + " of statement "+ mappedStatement.getId()); } typeHandler.setParameter(ps, i + 1, value, parameterMapping.getJdbcType()); } } } } /** * 根據資料庫方言,生成特定的分頁sql * @param sql * @param page * @return */ private String generatePageSql(String sql,Page page){ if(page!=null && !StringUtils.isEmpty(dialect)){ StringBuffer pageSql = new StringBuffer(); if("mysql".equals(dialect)){ pageSql.append(sql); pageSql.append(" limit "+page.getCurrentResult()+","+page.getShowCount()); }else if("oracle".equals(dialect)){ String oracleSql="SELECT * FROM (SELECT A.*, ROWNUM RN FROM ("; pageSql.append(oracleSql); pageSql.append(sql); pageSql.append(") A) WHERE RN >"+page.getCurrentResult()+" AND RN <="+(page.getCurrentResult()+page.getShowCount())); } return pageSql.toString(); }else{ return sql; } } public Object plugin(Object arg0) { return Plugin.wrap(arg0, this); } public synchronized void setProperties(Properties p) { dialect = p.getProperty("dialect"); if (StringUtils.isEmpty(dialect)) { try { logger.error("PagePlugin | setProperties:|description:PropertyException",new PropertyException("dialect property is not found!")); throw new PropertyException("dialect property is not found!"); } catch (PropertyException e) { logger.error("PagePlugin | setProperties:|description:dialect PropertyException",e); } } pageSqlId = p.getProperty("pageSqlId"); if (StringUtils.isEmpty(pageSqlId)) { try { logger.error("PagePlugin | setProperties:|description:PropertyException",new PropertyException("pageSqlId property is not found!")); throw new PropertyException("pageSqlId property is not found!"); } catch (PropertyException e) { logger.error("PagePlugin | setProperties:|description:pageSqlId PropertyException",e); } } } }
ReflectHelper.java:反射的工具類
package com.test.util;
import java.lang.reflect.Field;
import org.apache.log4j.Logger;
/**
* 說明:反射工具
* 建立人:yufeng
* 修改時間 2017-10-12
* @version
*/
public class ReflectHelper {
/**日誌*/
public static final Logger logger = Logger.getLogger(ReflectHelper.class);
/**
* 獲取obj物件fieldName的Field
* @param obj
* @param fieldName
* @return
*/
public static Field getFieldByFieldName(Object obj, String fieldName) {
for (Class<?> superClass = obj.getClass(); superClass != Object.class; superClass = superClass
.getSuperclass()) {
try {
return superClass.getDeclaredField(fieldName);
} catch (NoSuchFieldException e) {
logger.debug("ReflectHelper | getFieldByFieldName:|getFieldByFieldName Exception",e);
}
}
return null;
}
/**
* 獲取obj物件fieldName的屬性
* @param obj
* @param fieldName
* @return
* @throws SecurityException
* @throws NoSuchFieldException
* @throws IllegalArgumentException
* @throws IllegalAccessException
*/
public static Object getValueByFieldName(Object obj, String fieldName)
throws SecurityException, NoSuchFieldException,
IllegalArgumentException, IllegalAccessException {
Field field = getFieldByFieldName(obj, fieldName);
Object value = null;
if(field!=null){
if (field.isAccessible()) {
value = field.get(obj);
} else {
field.setAccessible(true);
value = field.get(obj);
field.setAccessible(false);
}
}
return value;
}
/**
* 設定obj物件fieldName的屬性
* @param obj
* @param fieldName
* @param value
* @throws SecurityException
* @throws NoSuchFieldException
* @throws IllegalArgumentException
* @throws IllegalAccessException
*/
public static void setValueByFieldName(Object obj, String fieldName,
Object value) throws SecurityException, NoSuchFieldException,
IllegalArgumentException, IllegalAccessException {
Field field = obj.getClass().getDeclaredField(fieldName);
if (field.isAccessible()) {
field.set(obj, value);
} else {
field.setAccessible(true);
field.set(obj, value);
field.setAccessible(false);
}
}
}
最後修改我們原有的mybatis-config.xml配置檔案
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD SQL Map Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<setting name="cacheEnabled" value="true" /><!-- 全域性對映器啟用快取 -->
</settings>
<typeAliases>
<typeAlias type="com.test.util.PageData" alias="pd"/>
<!-- 分頁 -->
<typeAlias type="com.test.entity.Page" alias="Page"/>
</typeAliases>
<plugins>
<plugin interceptor="com.test.util.PagePlugin">
<property name="dialect" value="oracle"/>
<property name="pageSqlId" value=".*listPage.*"/>
</plugin>
</plugins>
</configuration>
這裡需要注意兩點(下圖紅圈內):1.根據自己的業務選擇自己的資料庫。
2.在mapper.xml中定義的需要分頁查詢方法必須含有listpage這樣的關鍵字,這樣系統會對含listpage關鍵字的sql進行分頁,如下圖
二、完成後我們重新寫一個查詢使用者的方法,分別寫介面,實現類,mapper.xml,和controller呼叫,最後新建一個jsp頁面展示即可
我這裡直接在一起的userService中新增新的方法:
UserService.java:這裡返回值和引數都定義Page型別,(一般我們需要返回list)但它裡面包含了我們需要查詢的list集合。
public Page list(Page page)throws Exception;
UserServiceImpl.java:實現類,主要將獲取到的list據轉化為Page,這裡的PageData和map一致,是鍵值對型別
@SuppressWarnings("unchecked")
@Override
public Page list(Page page) throws Exception {
List<PageData> list = (List<PageData>)dao.findForList("UserMapper.selectUserlistPage", page);
page.setList(list);
return page;
}
userMapper.xml:編寫sql,引數是page型別,返回值是pd即pagedata
<!-- 通過分頁查詢所有user -->
<select id="selectUserlistPage" parameterType="page" resultType="pd">
select user_id,username, password,name,rights,last_login,ip,status,role_id from sys_app_user
</select>
TestController.java 這裡添加了三個方法:getList:獲取集合資料,getPageData()和getRequest()方法目的是獲取request請求的引數放入pagedata中。
@RequestMapping(value="list.do")
public ModelAndView getList(Page page){
ModelAndView mv=new ModelAndView();
PageData pd = this.getPageData();
List<PageData> pdlist=new ArrayList<PageData>();
try {
//查詢人員列表
page.setPd(pd);
page.setShowCount(10);
page = userService.list(page);
//獲取列表
pdlist = page.getList();
logger.info(" [TestController] [getList][success]");
} catch (Exception e) {
logger.error("[TestController] [getList][error]",e);
}
if(pdlist!=null&&pdlist.size()>0){
mv.addObject("list", pdlist);
}
mv.addObject("page", page);
mv.setViewName("listUser");
return mv;
}
/**得到pagedata物件
* @return
*/
public PageData getPageData()
{
return new PageData(this.getRequest());
}
/**得到request物件
* @return
*/
public HttpServletRequest getRequest()
{
HttpServletRequest request = ((ServletRequestAttributes)RequestContextHolder
.getRequestAttributes()).getRequest();
return request;
}
最後listUser.jsp:jsp中需要在展示和遍歷list的外層包一個form表單,action則填寫上面的getList()方法的請求,因為這裡封裝的分頁通過點選的頁面數通過提交表單來實現的。
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>test</title>
<style type="text/css">
ul{
list-style:none; /* 去掉ul前面的符號 */
margin: 0px; /* 與外界元素的距離為0 */
padding: 0px; /* 與內部元素的距離為0 */
width: auto; /* 寬度根據元素內容調整 */
}
ul li{
float:left; /* 向左漂移,將豎排變為橫排 */
padding: 10px; /* 與內部元素的距離為0 */
}
</style>
</head>
<body>
<form action="http://localhost:9795/test/list.do" method="post" name="Form" id="Form">
<div>
<c:forEach items="${list}" var="l" >
<h4>${l.USER_ID} | ${l.USERNAME} | ${l.NAME}</h4>
</c:forEach>
<!--翻頁控制元件-->
${page.pageStr}
</div>
</form>
</body>
</html>
最後結果:(沒有優化css樣式,實現了基本功能即可)點選頁面的時候,注意上面位址列引數的變化
下面貼出原始碼地址,原始碼中沒有資料庫指令碼,自行準備資料庫這塊的資料
原始碼地址:http://download.csdn.net/download/yufeng005/10018398