标签:delete user zed 客户管理 log return prot 项目 substr
一、概述今天自己做了个项目练练,然后有一些分页的功能,自己把分页的功能做了一个简单的总结,然后,为了以后能够方便自己的开发,做了一个baseDao的实现。
2.1、分页的实体类pageBean
package org.sihai.utils;
import java.util.List;
public class PageBean {
//当前页数
private Integer currentPage;
//总记录数
private Integer totalCount;
//每页显示条数
private Integer pageSize;
//总页数
private Integer totalPage;
//分页列表数据
private List list;
public PageBean(Integer currentPage, Integer totalCount, Integer pageSize) {
this.totalCount = totalCount;
this.pageSize = pageSize;
this.currentPage = currentPage;
if(this.currentPage == null){
//如页面没有指定显示那一页.显示第一页.
this.currentPage = 1;
}
if(this.pageSize == null){
//如果每页显示条数没有指定,默认每页显示3条
this.pageSize = 3;
}
//计算总页数
this.totalPage = (this.totalCount+this.pageSize-1)/this.pageSize;
//判断当前页数是否超出范围
//不能小于1
if(this.currentPage < 1){
this.currentPage = 1;
}
//不能大于总页数
if(this.currentPage > this.totalPage){
this.currentPage = this.totalPage;
}
}
//计算起始索引
public int getStart(){
return (this.currentPage-1)*this.pageSize;
}
public Integer getCurrentPage() {
return currentPage;
}
public void setCurrentPage(Integer currentPage) {
this.currentPage = currentPage;
}
public Integer getTotalCount() {
return totalCount;
}
public void setTotalCount(Integer totalCount) {
this.totalCount = totalCount;
}
public Integer getPageSize() {
return pageSize;
}
public void setPageSize(Integer pageSize) {
this.pageSize = pageSize;
}
public Integer getTotalPage() {
return totalPage;
}
public void setTotalPage(Integer totalPage) {
this.totalPage = totalPage;
}
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
}
2.2、Action的实现
package org.sihai.web.action;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import org.sihai.domain.user;
import org.sihai.domain.User;
import org.sihai.service.userService;
import org.sihai.service.UserService;
import org.sihai.utils.PageBean;
public class userAction extends ActionSupport implements ModelDriven<user> {
private user user = new user();
private userService cs;
private Integer currentPage;
private Integer pageSize;
public String list() throws Exception {
//封装离线查询对象
DetachedCriteria dc = DetachedCriteria.forClass(user.class);
//判断并封装参数
if(StringUtils.isNotBlank(user.getuser_name())){
dc.add(Restrictions.like("user_name", "%"+user.getuser_name()+"%"));
}
//1 调用Service查询分页数据(PageBean)
PageBean pb = cs.getPageBean(dc,currentPage,pageSize);
//2 将PageBean放入request域,转发到列表页面显示
ActionContext.getContext().put("pageBean", pb);
return "list";
}
@Override
public user getModel() {
return user;
}
public void setCs(userService cs) {
this.cs = cs;
}
public Integer getCurrentPage() {
return currentPage;
}
public void setCurrentPage(Integer currentPage) {
this.currentPage = currentPage;
}
public Integer getPageSize() {
return pageSize;
}
public void setPageSize(Integer pageSize) {
this.pageSize = pageSize;
}
}
2.3、service的实现
package org.sihai.service.impl;
import java.util.List;
import org.hibernate.criterion.DetachedCriteria;
import org.sihai.dao.userDao;
import org.sihai.domain.user;
import org.sihai.service.userService;
import org.sihai.utils.PageBean;
public class userServiceImpl implements userService {
private userDao cd;
@Override
public PageBean getPageBean(DetachedCriteria dc, Integer currentPage, Integer pageSize) {
//1 调用Dao查询总记录数
Integer totalCount = cd.getTotalCount(dc);
//2 创建PageBean对象
PageBean pb = new PageBean(currentPage, totalCount, pageSize);
//3 调用Dao查询分页列表数据
List<user> list = cd.getPageList(dc,pb.getStart(),pb.getPageSize());
//4 列表数据放入pageBean中.并返回
pb.setList(list);
return pb;
}
public void setCd(userDao cd) {
this.cd = cd;
}
}
2.4、baseDao的具体实现(通用基类)
package org.sihai.dao.impl;
import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
import org.sihai.dao.BaseDao;
import org.sihai.domain.user;
public class BaseDaoImpl<T> extends HibernateDaoSupport implements BaseDao<T> {
private Class clazz;//用于接收运行期泛型类型
public BaseDaoImpl() {
//获得当前类型的带有泛型类型的父类
ParameterizedType ptClass = (ParameterizedType) this.getClass().getGenericSuperclass();
//获得运行期的泛型类型
clazz = (Class) ptClass.getActualTypeArguments()[0];
}
@Override
public void save(T t) {
getHibernateTemplate().save(t);
}
@Override
public void delete(T t) {
getHibernateTemplate().delete(t);
}
@Override
public void delete(Serializable id) {
T t = this.getById(id);//先取,再删
getHibernateTemplate().delete(t);
}
@Override
public void update(T t) {
getHibernateTemplate().update(t);
}
@Override
public T getById(Serializable id) {
return (T) getHibernateTemplate().get(clazz, id);
}
@Override
public Integer getTotalCount(DetachedCriteria dc) {
//设置查询的聚合函数,总记录数
dc.setProjection(Projections.rowCount());
List<Long> list = (List<Long>) getHibernateTemplate().findByCriteria(dc);
//清空之前设置的聚合函数
dc.setProjection(null);
if(list!=null && list.size()>0){
Long count = list.get(0);
return count.intValue();
}else{
return null;
}
}
@Override
public List<T> getPageList(DetachedCriteria dc, Integer start, Integer pageSize) {
List<T> list = (List<T>) getHibernateTemplate().findByCriteria(dc, start, pageSize);
return list;
}
}
2.5、baseService的具体实现
package org.sihai.qualitycontrol.util.base;
import java.io.Serializable;
import java.util.List;
import org.springframework.transaction.annotation.Transactional;
@Transactional
public interface BaseEbi<T> {
public void save(T t);
public void update(T t);
public void delete(T t);
public List<T> getAll();
public T get(Serializable uuid);
public List<T> getAll(BaseQueryModel qm, Integer pageNum,Integer pageCount);
public Integer getCount(BaseQueryModel qm);
}
2.6、baseAction的具体实现
package org.sihai.qualitycontrol.util.base;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts2.ServletActionContext;
import org.sihai.qualitycontrol.auth.emp.vo.EmpModel;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
public class BaseAction extends ActionSupport{
public static final String LIST = "list";
public static final String TO_LIST = "toList";
public static final String INPUT = "input";
public Integer pageNum = 1;
public Integer pageCount = 10;
public Integer maxPageNum;
public Integer dataTotal;
public String getActionName(){
//动态
//DepAction ->dep
//EmpAction ->emp
String actionName = this.getClass().getSimpleName();
//DepAction ->Dep
String temp = actionName.substring(0, actionName.length()-6);
//Dep ->dep OrderDetailAction ->orderDetail orderdetail
return temp.substring(0,1).toLowerCase()+temp.substring(1);
}
protected void setDataTotal(int dataTotal){
this.dataTotal = dataTotal ;
maxPageNum = (dataTotal + pageCount -1) / pageCount;
}
protected void put(String name,Object obj){
ActionContext.getContext().put(name,obj);
}
protected Object get(String name){
return ActionContext.getContext().get(name);
}
protected void putSession(String name,Object obj){
ActionContext.getContext().getSession().put(name,obj);
}
protected Object getSession(String name){
return ActionContext.getContext().getSession().get(name);
}
protected EmpModel getLogin(){
return (EmpModel) getSession(EmpModel.EMP_LOGIN_USER_OBJECT_NAME);
}
protected HttpServletRequest getRequest(){
return ServletActionContext.getRequest();
}
protected HttpServletResponse getResponse(){
return ServletActionContext.getResponse();
}
}
2.7、页面的实现
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<TITLE>客户列表</TITLE>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<LINK href="${pageContext.request.contextPath }/css/Style.css" type=text/css rel=stylesheet>
<LINK href="${pageContext.request.contextPath }/css/Manage.css" type=text/css
rel=stylesheet>
<script type="text/javascript" src="${pageContext.request.contextPath }/js/jquery-1.4.4.min.js"></script>
<SCRIPT language=javascript>
function changePage(pageNum){
//1 将页码的值放入对应表单隐藏域中
$("#currentPageInput").val(pageNum);
//2 提交表单
$("#pageForm").submit();
};
function changePageSize(pageSize){
//1 将页码的值放入对应表单隐藏域中
$("#pageSizeInput").val(pageSize);
//2 提交表单
$("#pageForm").submit();
};
</SCRIPT>
<META content="MSHTML 6.00.2900.3492" name=GENERATOR>
</HEAD>
<BODY>
<TABLE cellSpacing=0 cellPadding=0 width="98%" border=0>
<TBODY>
<TR>
<TD width=15><IMG src="${pageContext.request.contextPath }/images/new_019.jpg"
border=0></TD>
<TD width="100%" background="${pageContext.request.contextPath }/images/new_020.jpg"
height=20></TD>
<TD width=15><IMG src="${pageContext.request.contextPath }/images/new_021.jpg"
border=0></TD>
</TR>
</TBODY>
</TABLE>
<TABLE cellSpacing=0 cellPadding=0 width="98%" border=0>
<TBODY>
<TR>
<TD width=15 background=${pageContext.request.contextPath }/images/new_022.jpg><IMG
src="${pageContext.request.contextPath }/images/new_022.jpg" border=0></TD>
<TD vAlign=top width="100%" bgColor=#ffffff>
<TABLE cellSpacing=0 cellPadding=5 width="100%" border=0>
<TR>
<TD class=manageHead>当前位置:客户管理 > 客户列表</TD>
</TR>
<TR>
<TD height=2></TD>
</TR>
</TABLE>
<TABLE borderColor=#cccccc cellSpacing=0 cellPadding=0
width="100%" align=center border=0>
<TBODY>
<TR>
<TD height=25>
<FORM id="pageForm" name="userForm"
action="${pageContext.request.contextPath }/userAction_list"
method=post>
<!-- 隐藏域.当前页码 -->
<input type="hidden" name="currentPage" id="currentPageInput" value="<s:property value="#pageBean.currentPage" />" />
<!-- 隐藏域.每页显示条数 -->
<input type="hidden" name="pageSize" id="pageSizeInput" value="<s:property value="#pageBean.pageSize" />" />
<TABLE cellSpacing=0 cellPadding=2 border=0>
<TBODY>
<TR>
<TD>客户名称:</TD>
<TD><INPUT class=textbox id=sChannel2
style="WIDTH: 80px" maxLength=50 name="user_name" value="${param.user_name}"></TD>
<TD><INPUT class=button id=sButton2 type=submit
value=" 筛选 " name=sButton2></TD>
</TR>
</TBODY>
</TABLE>
</FORM>
</TD>
</TR>
<TR>
<TD>
<TABLE id=grid
style="BORDER-TOP-WIDTH: 0px; FONT-WEIGHT: normal; BORDER-LEFT-WIDTH: 0px; BORDER-LEFT-COLOR: #cccccc; BORDER-BOTTOM-WIDTH: 0px; BORDER-BOTTOM-COLOR: #cccccc; WIDTH: 100%; BORDER-TOP-COLOR: #cccccc; FONT-STYLE: normal; BACKGROUND-COLOR: #cccccc; BORDER-RIGHT-WIDTH: 0px; TEXT-DECORATION: none; BORDER-RIGHT-COLOR: #cccccc"
cellSpacing=1 cellPadding=2 rules=all border=0>
<TBODY>
<TR
style="FONT-WEIGHT: bold; FONT-STYLE: normal; BACKGROUND-COLOR: #eeeeee; TEXT-DECORATION: none">
<TD>客户名称</TD>
<TD>客户级别</TD>
<TD>客户来源</TD>
<TD>联系人</TD>
<TD>电话</TD>
<TD>手机</TD>
<TD>操作</TD>
</TR>
<s:iterator value="#pageBean.list" var="user" >
<TR
style="FONT-WEIGHT: normal; FONT-STYLE: normal; BACKGROUND-COLOR: white; TEXT-DECORATION: none">
<TD>
<s:property value="#user.user_name" />
</TD>
<TD>
<s:property value="#user.user_level" />
</TD>
<TD>
<s:property value="#user.user_source" />
</TD>
<TD>
<s:property value="#user.user_goods" />
</TD>
<TD>
<s:property value="#user.user_phone" />
</TD>
<TD>
<s:property value="#user.user_mobile" />
</TD>
<TD>
<a href="${pageContext.request.contextPath }/userServlet?method=edit&userId=${user.user_id}">修改</a>
??
<a href="${pageContext.request.contextPath }/userServlet?method=delete&userId=${user.user_id}">删除</a>
</TD>
</TR>
</s:iterator>
</TBODY>
</TABLE>
</TD>
</TR>
<TR>
<TD><SPAN id=pagelink>
<DIV
style="LINE-HEIGHT: 20px; HEIGHT: 20px; TEXT-ALIGN: right">
共[<B><s:property value="#pageBean.totalCount" /> </B>]条记录,[<B><s:property value="#pageBean.totalPage" /></B>]页
,每页显示 <%-- changePageSize($(‘#pageSizeSelect option‘).filter(‘:selected‘).val()) --%>
<select name="pageSize" onchange="changePageSize($(‘#pageSizeSelect option:selected‘).val())" id="pageSizeSelect" >
<option value="3" <s:property value="#pageBean.pageSize==3?‘selected‘:‘‘" /> >3</option>
<option value="5" <s:property value="#pageBean.pageSize==5?‘selected‘:‘‘" /> >5</option>
</select>
条
[<A href="javaScript:void(0)" onclick="changePage(<s:property value=‘#pageBean.currentPage-1‘ />)" >前一页</A>]
<B><s:property value="#pageBean.currentPage" /></B>
[<A href="javaScript:void(0)" onclick="changePage(<s:property value=‘#pageBean.currentPage+1‘ />)" >后一页</A>]
到
<input type="text" size="3" id="page" name="page" value="<s:property value="#pageBean.currentPage" />" />
页
<input type="button" value="Go" onclick="changePage($(‘#page‘).val())"/>
</DIV>
</SPAN></TD>
</TR>
</TBODY>
</TABLE>
</TD>
<TD width=15 background="${pageContext.request.contextPath }/images/new_023.jpg"><IMG
src="${pageContext.request.contextPath }/images/new_023.jpg" border=0></TD>
</TR>
</TBODY>
</TABLE>
<TABLE cellSpacing=0 cellPadding=0 width="98%" border=0>
<TBODY>
<TR>
<TD width=15><IMG src="${pageContext.request.contextPath }/images/new_024.jpg"
border=0></TD>
<TD align=middle width="100%"
background="${pageContext.request.contextPath }/images/new_025.jpg" height=15></TD>
<TD width=15><IMG src="${pageContext.request.contextPath }/images/new_026.jpg"
border=0></TD>
</TR>
</TBODY>
</TABLE>
</BODY>
</HTML>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="51%">?</td>
<td width="13%">共${dataTotal}条记录
<td width="6%">
<a id="fir" class="sye">首??页</a>
</td>
<td width="6%">
<a id="pre" class="sye">上一页</a>
</td>
<td width="6%">
<a id="next" class="sye">下一页</a>
</td>
<td width="6%">
<a id="last" class="sye">末??页</a>
</td>
<td width="12%">当前第<span style="color:red;">${pageNum}</span>/${maxPageNum }页</td>
</tr>
</table>
<s:hidden name="pageNum"/>
<script type="text/javascript">
$(function(){
//最大页=1,都隐藏
//第一页,前2隐藏,后2显示
//最后一页,前2显示,后2隐藏
//中间任意,都显示
var pageNum = ${pageNum};
var maxPageNum = ${maxPageNum};
if(maxPageNum == 1){
$("#fir").css("display","none");
$("#pre").css("display","none");
$("#next").css("display","none");
$("#last").css("display","none");
}else if(pageNum == 1){
$("#fir").css("display","none");
$("#pre").css("display","none");
$("#next").css("display","inline");
$("#last").css("display","inline");
}else if(maxPageNum == pageNum){
$("#fir").css("display","inline");
$("#pre").css("display","inline");
$("#next").css("display","none");
$("#last").css("display","none");
}else {
$("#fir").css("display","inline");
$("#pre").css("display","inline");
$("#next").css("display","inline");
$("#last").css("display","inline");
}
$("#fir").click(function(){
$("[name=pageNum]").val(1);
$("form:first").submit();
});
$("#pre").click(function(){
$("[name=pageNum]").val($("[name=pageNum]").val()-1);
$("form:first").submit();
});
//下一页
$("#next").click(function(){
//收集页码值,将页码值设置为指定值,提交表单
//获取原始页码值,然后+1,设置回去
$("[name=pageNum]").val($("[name=pageNum]").val()*1+1);
$("form:first").submit();
});
$("#last").click(function(){
$("[name=pageNum]").val(maxPageNum);
$("form:first").submit();
});
});
</script>
以前这样的总结还是比较少的,以至于在以后的开发,很多的模块的小的东西还需要重新的来思考,很是浪费时间,希望以后能够坚持。。。
欢迎关注我的微信公众号
好好学java
,在公众号里,回复:java基础、html5、javaEE基础、struts2、spring、redis、luncene、oracle
等,将可获得以上的优质视频教程及源码。
ssh项目实战----分页功能的完整的实现(通用分页、基类实现)
标签:delete user zed 客户管理 log return prot 项目 substr
原文地址:http://blog.51cto.com/sihai/2066709