关于使用logic:iterate 整合struts 和hiberante分页的问题

海蓝树 2008-06-30 10:03:39
我有个项目,在使用logic:iterate分页的时候屏幕就一片空白。


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");
}
}






JSP页面:
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>


...全文
206 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
Landor2004 2008-06-30
  • 打赏
  • 举报
回复
先确定是不是list的问题,你把jsp中的
<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>
去掉,看看页面能不能出来
海蓝树 2008-06-30
  • 打赏
  • 举报
回复
还是不行,应该是这样吗?<logic:iterate name="users" id="user" type="com.mine.hib.T3">下面是我DAO类和实体类T3.java

T3.java
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);
}

}

T3DAO.java

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;
}
}
}
Landor2004 2008-06-30
  • 打赏
  • 举报
回复
加上type,如下

<logic:iterate id="user" name="users" scope="request" type="com.mine.hib.你的实体类">

海蓝树 2008-06-30
  • 打赏
  • 举报
回复
users不是空的,加了之后也是一片空白,使用<bean:write>输出了这个:[com.mine.hib.T3@6c5356, com.mine.hib.T3@97d3f0, com.mine.hib.T3@1db6942]
Landor2004 2008-06-30
  • 打赏
  • 举报
回复
如果users是空的话,就会空白,你说的不清楚,users是空吗,加上这句
<logic:present name="users">
<logic:iterate name="users" id="user">
......
</logic:iterate>
</logic:present>
海蓝树 2008-06-30
  • 打赏
  • 举报
回复
我知道错误了,是<a href="edit.html?id=${user.id}">修改 </a>的原因,谢谢了!~~
海蓝树 2008-06-30
  • 打赏
  • 举报
回复
去掉
<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>
之后出现就出来了,应该怎么改呢
水瘦山寒 2008-06-30
  • 打赏
  • 举报
回复
页面空白,应该是后台出现异常了哦。。
捕捉后台异常,看看控制台,再贴出来

67,511

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧