基于Struts+Spring+Hibernate的分页管理

struts-config.xml的配置
[code]<action
attribute="helloForm"
input="/form/hello.jsp"
name="helloForm"
path="/hello"
scope="request"
type="com.test.struts.action.HelloAction">
<forward name="success" path="/page/success.jsp" />
</action>
<!--注意配置 value="classpath:applicationContext.xml" 也就是Spring配置文件的路径如果配置路径错误,会报404错误. -->
<plug-in
className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation"
value="classpath:applicationContext.xml" />
</plug-in>
[/code]

applicationContext.xml的一些配置
[code]<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation"
value="classpath:hibernate.cfg.xml">
</property>
</bean>

<bean id="user" name="user_name" class="com.test.pojo.User">
<property name="id" value="12"/>
<property name="loginName" value="zj"></property>
<property name="password" value="zhangjie"></property>
</bean>

<!--事务管理器的定义 相当于拦截器的作用 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>

<!--
Use bean inheriting to trim down the transaction declaration.
First,define a abstractTxDefinition as the parent of all the beans that needs to
事务管理类型 用于在事务管理类中定义,事务管理类型
-->
<bean id="transactionAttributeSource"
class="org.springframework.transaction.interceptor.NameMatchTransactionAttributeSource">
<property name="properties">
<props>
<prop key="insert*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="delete*">PROPAGATION_REQUIRED</prop>
<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>

<!-- 抽象的事务管理代理类 由Spring框架提供 我们在设计事务管理的时候使用的事务代理类 只要基础这个类就可以-->
<bean id="abstractTxDefinition"
class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
lazy-init="true">
<property name="transactionManager">
<ref bean="transactionManager" />
</property>

<property name="transactionAttributeSource">
<ref bean="transactionAttributeSource" />
</property>
</bean>

<!-- 具体的Dao类 -->
<bean id="userDao" class="com.test.springdao.UserDao">
<property name="sessionFactory">
<ref local="sessionFactory"/>
</property>
</bean>

<!-- 具体的事务管理代理类 是Dao的代理类
在多层架构的系统中,例如在服务层中 我们也可以用服务层的方法继承此类,实现事务管理-->
<!-- method1 dao代理类-->
<bean id="userDaoProxy" parent="abstractTxDefinition">
<property name="proxyTargetClass">
<value>true</value>
</property>
<property name="target">
<ref bean="userDao"/>
</property>
</bean>
<!-- 我们将dao的代理类注入到Service中实现 用此代理类做事务处理和Dao层的数据持久化 -->
<bean id="userService" class="com.test.service.UserService">
<property name="dao">
<ref local="userDaoProxy"/>
</property>
</bean>

<!-- method2 将Service作为dao的代理类 我们用Service做事务处理,并且使用注入的Dao做数据持久化-->
<bean id="userServiceImpl" parent="abstractTxDefinition">
<property name="target">
<bean class="com.test.service.UserService">
<property name="dao">
<ref bean="userDao" />
</property>
</bean>
</property>
</bean>[/code]

页面信息类 Page
[code]package common.util.page;

public class Page {

private static final int NUMPAGE=30;
private int totlePages;
private int numPage=NUMPAGE;
private int currentPage=1;

private int startIndex;
// private int endIndex;

public Page(int currentPage,int totlePages){
this(currentPage,totlePages,NUMPAGE);
}

public Page(int currentPage,int totlePages,int numPage){
this.currentPage=currentPage;
this.totlePages=totlePages;
this.numPage=numPage;
this.startIndex=(currentPage-1)*numPage;
// this.endIndex=startIndex+numPage-1;
}

public int getCurrentPage() {
return currentPage;
}

public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}

// public int getEndIndex() {
// return endIndex;
// }
//
// public void setEndIndex(int endIndex) {
// this.endIndex = endIndex;
// }

public int getNumPage() {
return numPage;
}

public void setNumPage(int numPage) {
this.numPage = numPage;
}

public int getStartIndex() {
return startIndex;
}

public void setStartIndex(int startIndex) {
this.startIndex = startIndex;
}

public int getTotlePages() {
return totlePages;
}

public void setTotlePages(int totlePages) {
this.totlePages = totlePages;
}
}[/code]

返回的结果类:
[code]package common.util.page;

import java.util.List;

public class Result {

private Page page;
private List listItem;

public List getListItem() {
return listItem;
}
public void setListItem(List listItem) {
this.listItem = listItem;
}
public Page getPage() {
return page;
}
public void setPage(Page page) {
this.page = page;
}
}[/code]

Dao的查询工具类:
[code]package common.util.page;

import java.util.List;

import org.hibernate.Query;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

public abstract class DaoImpl extends HibernateDaoSupport{

public Result query(String hql,int numPage){
return query(hql,1,numPage);
}

public Result query(String hql,int currentPage,int numPage){

int numRecord=countRecord(hql);
int totelPages;
if(0==numRecord%numPage)
totelPages=numRecord/numPage;
else
totelPages=numRecord/numPage+1;

Page page=new Page(currentPage,totelPages,numPage);

Query query=this.getSession().createQuery(hql);
query.setFirstResult(page.getStartIndex());
query.setMaxResults(numPage);
List list=query.list();
Result result=new Result();
result.setListItem(list);
result.setPage(page);
return result;
}


public int countRecord(String hql){
int counter=0;
Query query=null;
String hsq=countquery(hql);

query=this.getSession().createQuery(hsq);
List list=query.list();
if (list != null) {
for (int i = 0; i < list.size(); i++) {
counter += (Integer) list.get(i);
}
}
return counter;
}

public String countquery(String query) {
int index = query.indexOf("from");
query = "select count(*) "+query.substring(index);
return query;
}

}[/code]

[code]Dao的实现类:
package com.test.springdao;

import java.util.List;

import common.util.page.DaoImpl;
import common.util.page.Result;

public class UserDao extends DaoImpl implements IUserDao{

public List findAllUsers(){
return this.getHibernateTemplate().find("from User");
}

public Result queryUsers(int currentPage,int numPage){
String hql="from User ";
return this.query(hql, currentPage, numPage);
}
}[/code]

Service层的类:
[code]package com.test.service;

import java.util.List;

import com.test.springdao.IUserDao;
import common.util.page.Result;

public class UserService {

private IUserDao dao;

public IUserDao getDao() {
return dao;
}

public void setDao(IUserDao dao) {
this.dao = dao;
}

public List findAllUsers(){
return dao.findAllUsers();
}

public Result queryUsers(int currentPage,int numPage){
return dao.queryUsers(currentPage, numPage);
}
}[/code]

POJO类 User
[code]package com.test.pojo;

public class User {

private long id;
private String loginName;
private String password;

public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getLoginName() {
return loginName;
}
public void setLoginName(String loginName) {
this.loginName = loginName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}

public String toString(){
return id+" "+loginName+" "+password;
}
}[/code]

pojo的mapping配置文件user.hbm.xml:
[code]<hibernate-mapping package="com.test.pojo">
<class name="User" table="USERS" schema="ZJ">
<id name="id" type="java.lang.Long">
<column name="ID" precision="22" scale="0" />
<generator class="sequence">
<param name="sequence">USERS_SEQ</param>
</generator>
</id>
<property name="loginName" type="java.lang.String">
<column name="LOGIN_NAME" length="20" not-null="true" unique="true" />
</property>
<property name="password" type="java.lang.String">
<column name="LOGIN_PASSWORD" length="20" not-null="true" />
</property>
</class>
</hibernate-mapping>[/code]


Action类:
[code]package com.test.struts.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.web.struts.DispatchActionSupport;

import com.test.springdao.IUserDao;
import com.test.struts.form.HelloForm;
import common.util.page.Result;

public class HelloAction extends DispatchActionSupport {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
IUserDao dao=(IUserDao)this.getWebApplicationContext().getBean("userDaoProxy");
HelloForm helloForm = (HelloForm) form;
String hello=helloForm.getHello();
Result users=dao.queryUsers(helloForm.getCurrentPage(), helloForm.getNumPage());
request.setAttribute("hello", hello);
request.setAttribute("users", users);
return mapping.findForward("success");
}
}[/code]

ActionForm类
[code]package com.test.struts.form;

import org.apache.struts.action.ActionForm;

public class BaseForm extends ActionForm{

private static final long serialVersionUID = 1L;
private int currentPage=1;
private int numPage=10;
private int totelPages;

public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getNumPage() {
return numPage;
}
public void setNumPage(int numPage) {
this.numPage = numPage;
}
public int getTotelPages() {
return totelPages;
}
public void setTotelPages(int totelPages) {
this.totelPages = totelPages;
}
}

public class HelloForm extends BaseForm{


}
[/code]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值