您的位置:首页 > 其它

实现了hibernate的简单的分页了。(二)

2006-09-11 12:47 337 查看
6 分页的两个类放在Util包中:
package com.oa.util;

public class Pager {

private int totalRows; //总行数
private int pageSize = 10; //每页显示的行数
private int currentPage; //当前页号
private int totalPages; //总页数
private int startRow; //当前页在数据库中的起始行

public Pager() {
}

public Pager(int _totalRows) {
totalRows = _totalRows;
totalPages=totalRows/pageSize;
int mod=totalRows%pageSize;
if(mod>0){
totalPages++;
}
currentPage = 1;
startRow = 0;
}

public int getStartRow() {
return startRow;
}

public int getTotalPages() {
return totalPages;
}

public int getCurrentPage() {
return currentPage;
}

public int getPageSize() {
return pageSize;
}

public void setTotalRows(int totalRows) {
this.totalRows = totalRows;
}

public void setStartRow(int startRow) {
this.startRow = startRow;
}

public void setTotalPages(int totalPages) {
this.totalPages = totalPages;
}

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

public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}

public int getTotalRows() {
return totalRows;
}

public void first() {
currentPage = 1;
startRow = 0;
}

public void previous() {
if (currentPage == 1) {
return;
}
currentPage--;
startRow = (currentPage - 1) * pageSize;
}

public void next() {
if (currentPage < totalPages) {
currentPage++;
}
startRow = (currentPage - 1) * pageSize;
}

public void last() {
currentPage = totalPages;
startRow = (currentPage - 1) * pageSize;
}

public void refresh(int _currentPage) {
currentPage = _currentPage;
if (currentPage > totalPages) {
last();
}
}

}

PagerHelp类:

package com.oa.util;

import javax.servlet.http.HttpServletRequest;

public class PagerHelp {

public static Pager getPager(HttpServletRequest httpServletRequest,int totalRows) {

//定义pager对象,用于传到页面
Pager pager = new Pager(totalRows);

//从Request对象中获取当前页号
String currentPage = httpServletRequest.getParameter("currentPage");
//如果当前页号为空,表示为首次查询该页
//如果不为空,则刷新pager对象,输入当前页号等信息
if (currentPage != null) {
pager.refresh(Integer.parseInt(currentPage));
}

//获取当前执行的方法,首页,前一页,后一页,尾页。
String pagerMethod = httpServletRequest.getParameter("pageMethod");
if (pagerMethod != null) {
if (pagerMethod.equals("first")) {
pager.first();
} else if (pagerMethod.equals("previous")) {
pager.previous();
} else if (pagerMethod.equals("next")) {
pager.next();
} else if (pagerMethod.equals("last")) {
pager.last();
}
}
return pager;
}
}

7 Struts中的Action毫无疑问就是属于C层次了,当然也可以说是V层:

package com.oa.view.struts.action;

import java.util.ArrayList;

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 org.apache.struts.actions.DispatchAction;
import org.apache.struts.validator.DynaValidatorForm;

import com.oa.model.service.IUserService;
import com.oa.util.Pager;
import com.oa.util.PagerHelp;

public class ShowAllUserAction extends Action {

private IUserService userService;

public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
ArrayList list=new ArrayList();
int totalRows;//记录总行数
totalRows=userService.counter();

Pager page=new Pager();
page=PagerHelp.getPager(request,totalRows);
list=(ArrayList) userService.findWithPage(page.getPageSize(),page.getStartRow());
request.setAttribute("list", list);
request.setAttribute("page", page);

return mapping.findForward("show_ok");
}

public IUserService getUserService() {
return userService;
}
public void setUserService(IUserService userService) {
this.userService = userService;
}

}

8 Spring 的核心配置文件当然就不用说了:如下

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName">
<value>com.mysql.jdbc.Driver</value>
</property>
<property name="url">
<value>jdbc:mysql://localhost:3307/test</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 bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>

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

<bean id="UserDAO" class="com.oa.data.dao.impl.UserDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
</bean>

<!-- 创建事务管理类 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>

<!-- 创建用户事务代理类 -->
<bean id="userDaoProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<property name="transactionManager">
<ref bean="transactionManager" />
</property>
<property name="target">
<ref local="UserDAO" />
</property>
<property name="transactionAttributes">
<props>
<prop key="insert*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED,readOnly</prop>
<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>

</beans>

9 服务上下文也写了个XML文件,serviceContext.xml ,当然也可以写在application.xml文件中

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="UserService" class="com.oa.model.service.impl.UserService">
<property name="userDAO">
<ref bean="userDaoProxy" />
</property>
</bean>
</beans>

10 viewContext.xml文件把所有的Action都做修配置,因为每个Action中都有个服务类的对象来处理业务,

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean name="/login" class="com.oa.view.struts.action.UserAction" singleton="false">
<property name="userService">
<ref bean="UserService" />
</property>
</bean>
<bean name="/login_test" class="com.oa.view.struts.action.Login2Action" singleton="false">
<property name="userService">
<ref bean="UserService" />
</property>
</bean>
<bean name="/test" class="com.oa.view.struts.action.TestAction" singleton="false">
<property name="userService">
<ref bean="UserService" />
</property>
</bean>

<bean name="/showAllUser" class="com.oa.view.struts.action.ShowAllUserAction" singleton="false">
<property name="userService">
<ref bean="UserService" />
</property>
</bean>
</beans>

11 struts-config文件可以说是使用了Spring之后变化比较大的地方了:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">

<struts-config>
<data-sources />
<form-beans>
<form-bean name="loginForm" type="org.apache.struts.validator.DynaValidatorForm">
<form-property name="password" type="java.lang.String" />
<form-property name="username" type="java.lang.String" />
<form-property name="password1" type="java.lang.String" />
<form-property name="username1" type="java.lang.String" />
</form-bean>
<form-bean name="testForm" type="org.apache.struts.validator.DynaValidatorForm">
<form-property name="username2" type="java.lang.String" />
<form-property name="password2" type="java.lang.String" />
</form-bean>
</form-beans>

<global-exceptions />
<global-forwards>
<forward name="goOk" path="/ok.jsp" />
</global-forwards>
<action-mappings>
<action attribute="loginForm"
input="/login.jsp"
name="loginForm"
parameter="method"
path="/login"
validate="true"
scope="request"
type="org.springframework.web.struts.DelegatingActionProxy" />
<action
attribute="loginForm"
input="/login.jsp"
name="loginForm"
parameter="method"
path="/login_test"
scope="request"
validate="true"
type="org.springframework.web.struts.DelegatingActionProxy" />
<action
attribute="testForm"
input="/login.jsp"
name="testForm"
path="/test"
scope="request"
type="com.oa.view.struts.action.TestAction" />
<action
path="/showAllUser"
scope="request"
type="org.springframework.web.struts.DelegatingActionProxy">
<forward name="show_ok" path="/ok.jsp" />
</action>
<action path="/showalluser2" type="com.oa.view.struts.action.Showalluser2Action">
<forward name="ok" path="/ok.jsp" />
</action>
</action-mappings>

<message-resources parameter="com.oa.view.struts.ApplicationResources" />
<plug-in className="org.apache.struts.tiles.TilesPlugin">
<set-property property="definitions-parser-validate" value="true" />
<set-property property="definitions-config" value="/WEB-INF/tiles-config.xml" />
</plug-in>

<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property property="pathnames" value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml" />
</plug-in>

<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation" value="/WEB-INF/applicationContext.xml,/WEB-INF/serviceContext.xml,/WEB-INF/viewContext.xml" />
</plug-in>

</struts-config>

12 最后当然是页面jsp文件了:

Login.jsp文件:

<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>

<html>
<head>
<title>Login</title>
</head>
<body>

<html:errors/><br>
<html:form action="/login_test">
<bean:message key="user.name"/> : <html:text property="username"/><br/>
<bean:message key="user.password"/> : <html:password property="password"/><br/>
<input type="submit" value="<bean:message key="page.command.submit"/>" onclick="document.forms[0].action='login.do?method=loginAction';document.forms[0].submit();">
</html:form>

<hr>
<a name="a" href="/showAllUser.do?pageMethod=first¤tPage=0">show _fcksavedurl=""/showAllUser.do?pageMethod=first¤tPage=0">show" _fcksavedurl=""/showAllUser.do?pageMethod=first¤tPage=0">show" all user</a>
<a name="a" href="/showalluser2.do">show all user</a>
<br>

</body>
</html>

ok.jsp 主要是显示了分页的显示:

<%@ page language="java" contentType="text/html; charset=UTF-8"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean"%>
<%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html"%>
<%@page import="java.util.*"%>
<%@page import="com.oa.data.bo.User"%>
<%@page import="com.oa.util.Pager"%>
<html>
<head>
<title>OK!!!</title>
</head>
<body>
<bean:message key="user.login.text2"/> 
<bean:message key="user.login.text4"/>
<hr>

<%
ArrayList list=new ArrayList();
User user=new User();
if(request.getAttribute("list")!=null)
{
list=(ArrayList)request.getAttribute("list");
for(int i=0;i<list.size();i++)
{
user=(User)list.get(i);
%>
<table width="100%" border="1" cellspacing="0" cellpadding="0">
<tr>
<td height="25"><%=user.getUserName()%></td>
<td height="25"><%=user.getUserPwd()%></td>
</tr>

</table>
<%
}
}
%>
<%
if(request.getAttribute("page")!=null)
{
Pager p=(Pager)request.getAttribute("page");
%>
<table width="100%" border="1" cellspacing="0" cellpadding="0">
<tr>
<td height="25">
第<%=p.getCurrentPage()%>页 共<%=p.getTotalPages()%>页
<a href="/showAllUser.do?pageMethod=first¤tPage=1%>">首页</a>
<a href="/showAllUser.do?pageMethod=next¤tPage=<%=p.getCurrentPage()%>">下页</a>
<a href="/showAllUser.do?pageMethod=previous¤tPage=<%=p.getCurrentPage()%>">上页</a>
<a href="/showAllUser.do?pageMethod=last¤tPage=<%=p.getCurrentPage()%>">末页</a>
</td>
</tr>

</table>
<%
}
%>
</body>

</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: