67,512
社区成员
发帖
与我相关
我的任务
分享
package com.mine.struts.action;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.mine.server.Service;
public class User_showAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
// 分析当前页码
String pageString=request.getParameter("page");
if(pageString == null || pageString.length() == 0) {
pageString = "1";
}
int currentPage= 0 ;
try {
currentPage = Integer.parseInt(pageString);// 当前页码
} catch(Exception e) {}
if(currentPage == 0) {
currentPage = 1;
}
int pageSize = 3;//每页显示的数据数
// 读取数据
Service manager = new Service();
List users = manager.findPagedAll(currentPage, pageSize);
request.setAttribute("users",users);// 保存用户列表
request.setAttribute("totalPage",manager.getTotalPage(pageSize));// 保存总页数
request.setAttribute("totalCount", manager.getTotalCount());//
//保存记录总数
request.setAttribute("currentPage", currentPage);// 保存当前页码
return mapping.findForward("display");
}
}
HTML code<%@ page language="java" import="com.mine.server.*,java.util.*"
pageEncoding="GBK"%>
<%@ page contentType="text/html;charset=GBK"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic"%>
<%
String path = request.getContextPath();
String basePath =request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>用户列表页面</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<style>
a:hover {color:red;text-decoration:none}
</style>
</head>
<body><b>用户列表页面</b><br>
<table width="100%" border="0">
</table>
<table width="80%" border="1" cellpadding="0"
style="border-collapse: collapse; " bordercolor="#000000">
<tr>
<td>用户名</td>
<td>密码</td>
<td>地址</td>
<td>电话</td>
<td>邮箱</td>
<td>性别</td>
<td>爱好</td>
<td>操作</td>
</tr>
<logic:iterate name="users" id="user">
<tr>
<td>${user.username}</td>
<td>${user.password}</td>
<td>${user.address}</td>
<td>${user.tel}</td>
<td>${user.email}</td>
<td>${user.sex}</td>
<td>${user.enjoy}</td>
<td>${user.content}</td>
<td><a href="edit.html?id=${user.id}">修改</a></td>
</tr>
</logic:iterate>
</table> 共${totalCount}个用户
第${currentPage}页/共${totalPage}页
<c:if test="${currentPage > 1}">
[ <a
href="${pageContext.request.contextPath}/user_show.html?page=${currentPage-1
}">上一页</a> ]
</c:if>
<c:if test="${currentPage <= 1}">
[ 上一页 ]
</c:if>
<c:if test="${currentPage < totalPage}">
[ <a
href="${pageContext.request.contextPath}/user_show.html?page=${currentPage+1
}">下一页</a> ]
</c:if>
<c:if test="${currentPage >= totalPage}">
[ 下一页 ]
</c:if>
<%-- 输出 JavaScript 跳转代码 --%>
<script>
// 页面跳转函数
// 参数: 包含页码的表单元素,例如输入框,下拉框等
function jumpPage(input) {
// 页码相同就不做跳转
if(input.value == ${currentPage}) {
return;
}
var newUrl = "${pageContext.request.contextPath}/user_show.html?page=" +
input.value;
document.location = newUrl;
}
</script>
转到
<!-- 输出 HTML SELECT 元素, 并选中当前页面编码 -->
<select onchange='jumpPage(this);'>
<c:forEach var="i" begin="1" end="${totalPage}">
<option value="${i}"
<c:if test="${currentPage == i}">
selected
</c:if>
>第${i}页</option>
</c:forEach>
</select>
输入页码:<input type="text" value="${currentPage}" id="jumpPageBox"
size="3">
<input type="button" value="跳转"
onclick="jumpPage(document.getElementById('jumpPageBox'))">
</body>
</html>
package com.mine.hib;
// Generated by MyEclipse Persistence Tools
import java.util.Set;
/**
* T3 generated by MyEclipse Persistence Tools
*/
public class T3 extends AbstractT3 implements java.io.Serializable {
// Constructors
/** default constructor */
public T3() {
}
/** minimal constructor */
public T3(String username) {
super(username);
}
/** full constructor */
public T3(String username, String password, String address, String tel,
String email, String sex, String enjoy, String content) {
super(username, password, address, tel, email, sex, enjoy, content);
}
}
package com.mine.hib;
import java.util.List;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;
/**
* Data access object (DAO) for domain model class T3.
*
* @see com.mine.hib.T3
* @author MyEclipse Persistence Tools
*/
public class T3DAO extends BaseHibernateDAO {
private static final Log log = LogFactory.getLog(T3DAO.class);
// property constants
public static final String PASSWORD = "password";
public static final String ADDRESS = "address";
public static final String TEL = "tel";
public static final String EMAIL = "email";
public static final String SEX = "sex";
public static final String ENJOY = "enjoy";
public static final String CONTENT = "content";
public void save(T3 transientInstance) {
log.debug("saving T3 instance");
try {
getSession().save(transientInstance);
log.debug("save successful");
} catch (RuntimeException re) {
log.error("save failed", re);
throw re;
}
}
public void delete(T3 persistentInstance) {
log.debug("deleting T3 instance");
try {
getSession().delete(persistentInstance);
log.debug("delete successful");
} catch (RuntimeException re) {
log.error("delete failed", re);
throw re;
}
}
public T3 findById(java.lang.String id) {
log.debug("getting T3 instance with id: " + id);
try {
T3 instance = (T3) getSession().get("com.mine.hib.T3", id);
return instance;
} catch (RuntimeException re) {
log.error("get failed", re);
throw re;
}
}
public List findByExample(T3 instance) {
log.debug("finding T3 instance by example");
try {
List results = getSession().createCriteria("com.mine.hib.T3").add(
Example.create(instance)).list();
log.debug("find by example successful, result size: "
+ results.size());
return results;
} catch (RuntimeException re) {
log.error("find by example failed", re);
throw re;
}
}
public List findByProperty(String propertyName, Object value) {
log.debug("finding T3 instance with property: " + propertyName
+ ", value: " + value);
try {
String queryString = "from T3 as model where model." + propertyName
+ "= ?";
Query queryObject = getSession().createQuery(queryString);
queryObject.setParameter(0, value);
return queryObject.list();
} catch (RuntimeException re) {
log.error("find by property name failed", re);
throw re;
}
}
public List findByPassword(Object password) {
return findByProperty(PASSWORD, password);
}
public List findByAddress(Object address) {
return findByProperty(ADDRESS, address);
}
public List findByTel(Object tel) {
return findByProperty(TEL, tel);
}
public List findByEmail(Object email) {
return findByProperty(EMAIL, email);
}
public List findBySex(Object sex) {
return findByProperty(SEX, sex);
}
public List findByEnjoy(Object enjoy) {
return findByProperty(ENJOY, enjoy);
}
public List findByContent(Object content) {
return findByProperty(CONTENT, content);
}
public List findAll() {
log.debug("finding all T3 instances");
try {
String queryString = "from T3";
Query queryObject = getSession().createQuery(queryString);
return queryObject.list();
} catch (RuntimeException re) {
log.error("find all failed", re);
throw re;
}
}
public T3 merge(T3 detachedInstance) {
log.debug("merging T3 instance");
try {
T3 result = (T3) getSession().merge(detachedInstance);
log.debug("merge successful");
return result;
} catch (RuntimeException re) {
log.error("merge failed", re);
throw re;
}
}
public void attachDirty(T3 instance) {
log.debug("attaching dirty T3 instance");
try {
getSession().saveOrUpdate(instance);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}
public void attachClean(T3 instance) {
log.debug("attaching clean T3 instance");
try {
getSession().lock(instance, LockMode.NONE);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}
public int getTotalCount() {
Query q = getSession().createQuery("select count(*) from T3");
List cc = q.list();
Integer a = (Integer) cc.get(0);
return a.intValue();
}
/**
* 分页显示数据.
*
* @param currentPage
* 当前页码, 从 1 开始
* @param pageSize
* 每页显示数据量
* @return 分页后的数据列表 - List<Student>
*/
public List findPagedAll(int currentPage, int pageSize) {
log.debug("分页查找");
try {
if (currentPage == 0) {
currentPage = 1;
}
String queryString = "from T3";
Query queryObject = getSession().createQuery(queryString);
queryObject.setFirstResult((currentPage - 1) * pageSize);
queryObject.setMaxResults(pageSize);
return queryObject.list();
} catch (RuntimeException re) {
log.error("find all failed", re);
throw re;
}
}
}