SSH一个分页功能,从bean容器注入值的时候在action中接受到的service层报空值,但是自己觉得配置方面没有问题,求大神解释

qq397709884 武汉软件工程职业学院 java软件工程师  2015-08-22 06:50:59
package com.test.bean;

import java.util.List;
/*
bean容器

*/
public class PageBean {

private List list; //要返回的某一页的记录列表

private int allRow; //总记录数
private int totalPage; //总页数
private int currentPage; //当前页
private static int pageSize; //每页记录数

private boolean isFirstPage; //是否为第一页
private boolean isLastPage; //是否为最后一页
private boolean hasPreviousPage; //是否有前一页
private boolean hasNextPage; //是否有下一页
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
public int getAllRow() {
return allRow;
}
public void setAllRow(int allRow) {
this.allRow = allRow;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}




/** *//**
* 初始化分页信息
*/
public void init(){
this.isFirstPage=isFirstPage;
this.isLastPage=isLastPage;
this.hasNextPage=hasNextPage;
this.hasPreviousPage=hasPreviousPage;
}

/** *//**
* 以下判断页的信息,只需getter方法(is方法)即可
* @return
*/

public boolean isFirstPage() {
return currentPage == 1; // 如是当前页是第1页
}
public boolean isLastPage() {
return currentPage == totalPage; //如果当前页是最后一页
}
public boolean isHasPreviousPage() {
return currentPage != 1; //只要当前页不是第1页
}
public boolean isHasNextPage() {
return currentPage != totalPage; //只要当前页不是最后1页
}

/** *//**
* 计算总页数,静态方法,供外部直接通过类名调用
* @param pageSize 每页记录数
* @param allRow 总记录数
* @return 总页数
* @param totalPage 总页数
*/

public static int countTotalPage(final int PageSize,final int allRow){
int totalPage = allRow % pageSize == 0 ? allRow/pageSize : allRow/pageSize+1;
return totalPage;
}

/** *//**
* 计算当前页开始记录
* @param pageSize 每页记录数
* @param currentPage 当前第几页
* @return offset 当前页开始记录号
*/

public static int countOffset(final int pageSize,final int currentPage){
final int offset = pageSize*(currentPage-1);
return offset;
}

/** *//**
* 计算当前页,若为0或者请求的URL中没有"?page=",则用1代替
* @param page 传入的参数(可能为空,即0,则返回1)
* @return 当前页
*/

public static int countCurrentPage(int page){
final int curPage=(page==0?1:page);
return curPage;
}


}



/*
dao层
*/

package com.test.dao;

import java.util.List;

public interface MemberDao {

//省略了其他的代码

/** *//**
* 分页查询
* @param hql 查询的条件
* @param offset 开始记录
* @param length 一次查询几条记录
* @return
*/

public List queryForPage(final String hql,final int offset, int length);



/** *//**
* 查询所有记录数
* @param hql 查询的条件
* @return 总记录数
*/
public int getAllRowCount(String hql);
}


/*
道的实现类
*/

package com.test.dao.impl;


import java.util.List;



import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;



import com.test.dao.MemberDao;

public class MemberDaoImpl implements MemberDao {

/** *//**
* 分页查询
* @param hql 查询的条件
* @param offset 开始记录
* @param length 一次查询几条记录
* @return
*/
private SessionFactory sessionFactory;

public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}


public List queryForPage(final String hql, final int offset, final int length) {
// TODO Auto-generated method stub
Session session=sessionFactory.openSession();
Query q =session.createQuery(hql);
q.setFirstResult(offset);
q.setMaxResults(length);
List list=q.list();
System.out.println(list.size());
session.close();
return list;
}


public int getAllRowCount(String hql) {
// TODO Auto-generated method stub
Session session=sessionFactory.openSession();

return session.createQuery(hql).list().size();
}

}


/*
service 层
*/

package com.test.service;

import com.test.bean.PageBean;

public interface MemberService {
/** *//**
* 分页查询
* @param currentPage 当前第几页
* @param pageSize 每页大小
* @return 封闭了分页信息(包括记录集list)的Bean
*/

public PageBean queryForPage(int pageSize,int currentPage);
}


/*
service实现类
*/

package com.test.service.impl;

import java.util.List;

import org.junit.Test;

import com.test.bean.PageBean;
import com.test.bean.User;
import com.test.dao.MemberDao;
import com.test.service.MemberService;

public class MemberServiceImpl implements MemberService {

//通过applicationContext.xml配置文件注入MemberDao的值
private MemberDao memberDao;

public void setMemberDao(MemberDao memberDao) {
this.memberDao = memberDao;
}
/** *//**
* 分页查询
* @param page 当前第几页
* @param pageSize 每页大小
* @return 封闭了分页信息(包括记录集list)的Bean
*/


public PageBean queryForPage(int pageSize, int page) {
// TODO Auto-generated method stub

final String hql = "from User user order by user.id desc"; //查询语句
int allRow = memberDao.getAllRowCount(hql); //总记录数
int totalPage = PageBean.countTotalPage(pageSize, allRow); //总页数
final int offset = PageBean.countOffset(pageSize, page); //当前页开始记录
final int length = pageSize; //每页记录数
final int currentPage = PageBean.countCurrentPage(page);
List<User> list = memberDao.queryForPage(hql,offset, length); //"一页"的记录
System.out.println(allRow+" "+totalPage+" "+offset+length+" "+currentPage);
PageBean pageBean=new PageBean();
pageBean.setPageSize(pageSize);
pageBean.setCurrentPage(currentPage);
pageBean.setAllRow(allRow);
pageBean.setTotalPage(totalPage);
pageBean.setList(list);
pageBean.init();
return pageBean;
}

}


/*
action 层
*/

package com.test.action.user;

import java.util.List;

import org.junit.Test;

import com.test.bean.PageBean;
import com.test.bean.User;
import com.test.service.MemberService;

public class ListMember {

private MemberService memberService;
private List<User> listUser;
public List<User> getListUser() {
return listUser;
}
public void setListUser(List<User> listUser) {
this.listUser = listUser;
}
public void setMemberService(MemberService memberService) {
this.memberService = memberService;
}

private int page=1;
private PageBean pageBean;

public int getPage() {
return page;
}
public void setPage(int page) {
this.page = page;
}
public PageBean getPageBean() {
return pageBean;
}
public void setPageBean(PageBean pageBean) {
this.pageBean = pageBean;
}

@Test
public String findfy() throws Exception{
//分页的pageBean,参数pageSize表示每页显示记录数,page为当前页
System.out.println(memberService.hashCode());
this.pageBean= memberService.queryForPage(2, page);
this.listUser=this.pageBean.getList();

return "success";
}
}

<action name="pageNum" class="com.test.action.user.ListMember" method="findfy">
<result name="success" type="redirect">/listMember.jsp</result>
</action>

数据库连接池的配置正确
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">

<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>

<property name="url">
<value>jdbc:mysql://localhost:3306/ssh2</value>
</property>

<property name="username">
<value>root</value>
</property>

<property name="password">
<value>root</value>
</property>

</bean>

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

<property name="dataSource">
<ref local="dataSource" />
</property>

<property name="mappingResources">
<list>
<value>com/test/bean/User.hbm.xml</value>
</list>
</property>

<property name="hibernateProperties">

<props>

<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.show_sql">true</prop>

</props>

</property>

</bean>

<!-- 分页注入值 -->
<bean id="MemberDao" class="com.test.dao.impl.MemberDaoImpl" scope="singleton">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="MemberService" class="com.test.service.impl.MemberServiceImpl">
<property name="memberDao" ref="MemberDao"></property>
</bean>
<bean id="pageAction" class="com.test.action.user.ListMember">
<property name="memberService" ref="MemberService"></property>
</bean>
...全文
64 点赞 收藏 1
写回复
1 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
無乄聊 2015-08-26
缺少ssh 框架的整合jar 包
回复
相关推荐
发帖
Web 开发
创建于2007-09-28

8.0w+

社区成员

Java Web 开发
申请成为版主
帖子事件
创建了帖子
2015-08-22 06:50
社区公告
暂无公告