一、這是SSH框架下的demo:
二、例子界面
三、然後建立一JavaBean類用來封裝查詢條件
package util;
/**
* 客戶信息關係表的查詢屬性
* @author miao
*
*/
public class SearchProperties {
private int custNo;
private String custName;
private String custRegion;
private String custManagerName;
private String custLevelLabel;
public SearchProperties() {
super();
}
public SearchProperties(String custName, String custRegion,
String custManagerName, String custLevelLabel) {
super();
this.custName = custName;
this.custRegion = custRegion;
this.custManagerName = custManagerName;
this.custLevelLabel = custLevelLabel;
}
public SearchProperties(int custNo, String custName, String custRegion,
String custManagerName, String custLevelLabel) {
super();
this.custNo = custNo;
this.custName = custName;
this.custRegion = custRegion;
this.custManagerName = custManagerName;
this.custLevelLabel = custLevelLabel;
}
public int getCustNo() {
return custNo;
}
public void setCustNo(int custNo) {
this.custNo = custNo;
}
public String getCustName() {
return custName;
}
public void setCustName(String custName) {
this.custName = custName;
}
public String getCustRegion() {
return custRegion;
}
public void setCustRegion(String custRegion) {
this.custRegion = custRegion;
}
public String getCustManagerName() {
return custManagerName;
}
public void setCustManagerName(String custManagerName) {
this.custManagerName = custManagerName;
}
public String getCustLevelLabel() {
return custLevelLabel;
}
public void setCustLevelLabel(String custLevelLabel) {
this.custLevelLabel = custLevelLabel;
}
}
四、dao接口
package dao;
import java.util.List;
import util.SearchProperties;
import entity.CstCustomer;
/**
* 客戶信息關係接口
*
* @author Ali
*
*/
public interface CstCustomerDao {
/**
* 查詢總記錄數
*/
public Integer findCount();
/**
* 查詢有條件的總記錄數
*/
public Integer countCustomerByProperties(SearchProperties condition);
/**
* 得到某條件的記錄
*/
public List<CstCustomer> getCustomerByProperties(SearchProperties condition, int first, int max);
}
五、daoImpl類
package dao.impl;
import java.util.List;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import util.SearchProperties;
import dao.CstCustomerDao;
import entity.CstCustomer;
/**
* 客戶信息關係實現類
*
* @author Ali
*
*/
public class CstCustomerDaoImpl extends HibernateDaoSupport implements CstCustomerDao {
/**
* 根據條件建立DetachedCriteria對象
*
* @param condition
* @return
*/
public DetachedCriteria createCriterionByProperties(SearchProperties condition) {
DetachedCriteria criteria = DetachedCriteria.forClass(CstCustomer.class);
// 客戶編號
if (condition.getCustNo() != 0) {
criteria.add(Restrictions.eq("custNo", condition.getCustNo()));
}
// 客戶名字
if (null != condition.getCustName() && !"".equals(condition.getCustName())) {
criteria.add(Restrictions.ilike("custName", condition.getCustName(),
MatchMode.ANYWHERE));
}
// 地區
if (null != condition.getCustRegion() && !"".equals(condition.getCustRegion())) {
criteria.add(Restrictions.ilike("custRegion", condition.getCustRegion(),
MatchMode.ANYWHERE));
}
// 客戶經理
if (null != condition.getCustManagerName() && !"".equals(condition.getCustManagerName())) {
criteria.add(Restrictions.ilike("custManagerName", condition.getCustManagerName(),
MatchMode.ANYWHERE));
}
// 客戶等級
if (null != condition.getCustLevelLabel() && !"".equals(condition.getCustLevelLabel())) {
criteria.add(Restrictions.eq("custLevelLabel", condition.getCustLevelLabel()));
}
return criteria;
}
/**
* 查詢總記錄數
*/
public Integer findCount() {
return super.getHibernateTemplate().loadAll(CstCustomer.class).size();
}
/**
* 得到某條件的記錄
*
* @param condition
* @param first
* @param max
* @return
*/
@SuppressWarnings("unchecked")
public List<CstCustomer> getCustomerByProperties(SearchProperties condition, int first, int max) {
DetachedCriteria detachedCriteria = createCriterionByProperties(condition);
return super.getHibernateTemplate().findByCriteria(detachedCriteria, first, max);
}
/**
* 查詢有條件的總記錄數
*/
public Integer countCustomerByProperties(SearchProperties condition) {
DetachedCriteria detachedCriteria = createCriterionByProperties(condition);
return super.getHibernateTemplate().findByCriteria(detachedCriteria).size();
}
}
六、biz接口
package biz;
import java.util.List;
import util.SearchProperties;
import entity.CstCustomer;
/**
* 客戶信息關係業務接口
*
* @author miao
*
*/
public interface CstCustomerBiz {
/**
* 查詢總記錄數
*/
public Integer findCount();
/**
* 查詢有條件的總記錄數
*/
public Integer countCustomerByProperties(SearchProperties condition);
/**
* 得到某條件的記錄
*/
public List<CstCustomer> getCustomerByProperties(SearchProperties condition, int first, int max);
}
七、bizImpl類
package biz.impl;
import java.util.List;
import util.SearchProperties;
import biz.CstCustomerBiz;
import dao.CstCustomerDao;
import entity.CstCustomer;
/**
* 客戶信息關係業務實現類
*
* @author Ali
*
*/
public class CstCustomerBizImpl implements CstCustomerBiz {
// 調用dao
private CstCustomerDao cstCustomerDao;
// set注入
public void setCstCustomerDao(CstCustomerDao cstCustomerDao) {
this.cstCustomerDao = cstCustomerDao;
}
/**
* 查詢總記錄數
*/
public Integer findCount() {
return cstCustomerDao.findCount();
}
public Integer countCustomerByProperties(SearchProperties condition) {
return cstCustomerDao.countCustomerByProperties(condition);
}
public List<CstCustomer> getCustomerByProperties(SearchProperties condition, int number, int size) {
int first = (number - 1) * size;
return cstCustomerDao.getCustomerByProperties(condition, first, size);
}
}
八、第一個頁面。用來顯示查詢條件和轉跳至action
<%@ page language="java" contentType="text/html;charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib prefix="sx" uri="/struts-dojo-tags"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<base href="<%=basePath%>"/>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>客戶信息管理-查看全部客戶信息</title>
<link type="text/css" rel="stylesheet" href="css/style.css" />
<sx:head />
</head>
<body>
<!-- 頭部,查詢條件 -->
<div class="page_title">客戶信息管理</div>
<s:form method="post" action="search" id='sform' namespace="/" theme="simple">
<div class="button_bar">
<input type="reset" value="重置" />
<sx:submit cssClass="common_button" type="button" name="search" value="查詢" targets="divCustomer" />
</div>
<table class="query_form_table">
<tr>
<th>客戶編號</th>
<td>
<s:textfield name="custNo" id="custNo" />
</td>
<th>名稱</th>
<td>
<s:textfield name="custName" id="custName"/>
</td>
<th>地區</th>
<td>
<!-- key是值,value是顯示 -->
<s:select list="#{'':'全部','北京':'北京','華北':'華北','中南':'中南','東北':'東北','西部':'西部','上海':'上海' }" id="custRegion" name="custRegion"></s:select>
</td>
</tr>
<tr>
<th>客戶經理</th>
<td>
<s:textfield name="custManagerName" id="custManagerName" />
</td>
<th>客戶等級</th>
<td>
<s:select list="#{'':'全部','戰略合作伙伴':'戰略合作伙伴','合作伙伴':'合作伙伴','大客戶':'大客戶','普通客戶':'普通客戶' }" id="custLevelLabel" name="custLevelLabel"></s:select>
</td>
<th></th>
<td></td>
</tr>
</table>
</s:form>
<!-- struts2 的??? -->
<div class="data_list_table">
<!-- 顯示分頁數據 -->
<sx:div id="divCustomer" href="search.action">正在加載,請稍候...</sx:div>
</div>
</body>
</html>
九、第二個頁面,用來顯示數據和實現分頁
<%@ page language="java" contentType="text/html;charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib prefix="sx" uri="/struts-dojo-tags"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<base href="<%=basePath%>"/>
<title>客戶信息管理-查看全部客戶信息</title>
<sx:head />
<script src="script/common.js"></script>
<link href="css/style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<table class="data_list_table">
<tr>
<th>序號</th>
<th>客戶編號</th>
<th>名稱</th>
<th>地區</th>
<th>客戶經理</th>
<th>客戶等級</th>
<th>操作</th>
</tr>
<c:if test="${customerList ne null}">
<c:forEach items="${customerList}" var="customer" varStatus="row">
<tr>
<td class="list_data_number">${row.index+1 }</td>
<td class="list_data_text">${customer.custNo }</td>
<td class="list_data_ltext">${customer.custName }</td>
<td class="list_data_text">${customer.custRegion }</td>
<td class="list_data_text">${customer.custManagerName }</td>
<td class="list_data_text">${customer.custLevelLabel }</td>
<td class="list_data_op">
<img οnclick="location.href='edit.action?id=${customer.custNo }';" title="編輯" src="images/bt_edit.gif" class="op_button" />
<img οnclick="location.href='linkman.action?id=${customer.custNo}';" title="聯繫人" src="images/bt_linkman.gif" class="op_button" />
<img οnclick="location.href='activity.action?id=${customer.custNo}';" title="交往記錄" src="images/bt_acti.gif" class="op_button" />
<img οnclick="location.href='orders.action?id=${customer.custNo }&name=${customer.custName}';" title="歷史訂單" src="images/bt_orders.gif" class="op_button" />
<img οnclick="location.href='delete.action?id=${customer.custNo}';" title="刪除" src="images/bt_del.gif" class="op_button" />
</td>
</tr>
</c:forEach>
</c:if>
<c:if test="${empty customerList}">
<tr>
<td colspan="7" align="center">
<c:out value="暫無信息"></c:out>
</td>
</tr>
</c:if>
<!-- 分頁 -->
<tr>
<th colspan="7" class="pager">
<div class="pager">
<!-- 不作顯示 -->
<s:url var="urlFirst" value="search.action">
<s:param name="number" value="1" />
</s:url>
<s:url var="urlPrev" value="search.action">
<s:param name="number" value="prev" />
</s:url>
<s:url var="urlNext" value="search.action">
<s:param name="number" value="next" />
</s:url>
<s:url var="urlLast" value="search.action">
<s:param name="number" value="total" />
</s:url>
<!-- 以上也是 -->
共<s:property value="count" />條記錄 每頁3條
第<s:property value="number" />頁/共<s:property value="total" />頁
<sx:a href="%{urlFirst}" targets="divCustomer" formId="sform">第一頁</sx:a>
<sx:a href="%{urlPrev}" targets="divCustomer" formId="sform">上一頁</sx:a>
<sx:a href="%{urlNext}" targets="divCustomer" formId="sform">下一頁</sx:a>
<sx:a href="%{urlLast}" targets="divCustomer" formId="sform">最後一頁</sx:a>
</div>
</th>
</tr>
</table>
</body>
</html>
十、action的編寫
package action;
import java.io.UnsupportedEncodingException;
import java.util.List;
import util.SearchProperties;
import biz.CstCustomerBiz;
import com.opensymphony.xwork2.ActionSupport;
import entity.CstCustomer;
/**
* 客戶管理的Action
*
* @author miao
*
*/
public class CustomerManagerAction extends ActionSupport {
/*
* 調用業務類
*/
private CstCustomerBiz cstCustomerBiz;// 客戶信息關係
/*
* 封裝數據
*/
private List<CstCustomer> customerList;// 客戶信息關係
/*
* 分頁需要的代碼
*/
final static int PAGE_SIZE = 3;// 每頁的記錄數
private int number;// 當前頁
private int total;// 總頁面
private int count;// 總記錄數
private int prev;// 上一頁
private int next;// 下一頁
/*
* 組合查詢的條件
*/
private int custNo;
private String custName;
private String custRegion;
private String custManagerName;
private String custLevelLabel;
/*
* 從頁面傳來的值
*/
private int id;// 根據id查詢數據
private String name;// 根據名字查詢數據
// set注入
public void setCstCustomerBiz(CstCustomerBiz cstCustomerBiz) {
this.cstCustomerBiz = cstCustomerBiz;
}
// getter/setter方法
public static int getPageSize() {
return PAGE_SIZE;
}
public int getCustNo() {
return custNo;
}
public void setCustNo(int custNo) {
this.custNo = custNo;
}
public String getCustName() {
return custName;
}
public void setCustName(String custName) {
this.custName = custName;
}
public String getCustRegion() {
return custRegion;
}
public void setCustRegion(String custRegion) {
this.custRegion = custRegion;
}
public String getCustManagerName() {
return custManagerName;
}
public void setCustManagerName(String custManagerName) {
this.custManagerName = custManagerName;
}
public String getCustLevelLabel() {
return custLevelLabel;
}
public void setCustLevelLabel(String custLevelLabel) {
this.custLevelLabel = custLevelLabel;
}
public String getName() {
return name;
}
public void setName(String name) throws UnsupportedEncodingException {
this.name = new String(name.getBytes("ISO-8859-1"), "utf-8");
}
public List<CstCustomer> getCustomerList() {
return customerList;
}
public void setCustomerList(List<CstCustomer> customerList) {
this.customerList = customerList;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public long getTotal() {
return total;
}
public void setTotal(int total) {
this.total = total;
}
public int getPrev() {
return prev;
}
public void setPrev(int prev) {
this.prev = prev;
}
public int getNext() {
return next;
}
public void setNext(int next) {
this.next = next;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
/**
* 計算總頁數
*
* @param count 數據總記錄數
* @param size 頁面數據條數
*/
private int calcTotalPage(int count, int size) {
return count % size == 0 ? count / size : count / size + 1;
}
/**
* 組合查詢,帶分頁
*/
public String findByProperties() {
SearchProperties condition = new SearchProperties();
if ("".equals(custName) && "".equals(custRegion) && "".equals(custManagerName)
&& "".endsWith(custLevelLabel)) {
// 查詢所有的記錄條數
count = cstCustomerBiz.findCount();
} else {
// 封裝查詢條件
condition = new SearchProperties(custName, custRegion, custManagerName, custLevelLabel);
// 查詢有條件的總記錄數
count = cstCustomerBiz.countCustomerByProperties(condition);
}
// 計算總頁數
total = calcTotalPage(count, PAGE_SIZE);
// 頁面的處理
if (number == 0) {
number = 1;
}
prev = number - 1;
next = number + 1;
if (prev < 1) {
prev = 1;
}
if (next > total) {
next = total;
}
customerList = cstCustomerBiz.getCustomerByProperties(condition, number, PAGE_SIZE);
return SUCCESS;
}
}
十一、spring的配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <!-- 會話工廠 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="configLocation" value="classpath:hibernate.cfg.xml" /> </bean> <!--注入dao類 --> <bean id="cstCustomerDao" class="dao.impl.CstCustomerDaoImpl"> <description>客戶信息關係接口</description> <property name="sessionFactory" ref="sessionFactory" /> </bean> <!-- 注入biz --> <bean id="cstCustomerBiz" class="biz.impl.CstCustomerBizImpl"> <description>客戶信息關係業務</description> <property name="cstCustomerDao" ref="cstCustomerDao" /> </bean> <!-- 注入action --> <bean id="customerManagerAction" class="action.CustomerManagerAction"> <description>客戶管理Action</description> <property name="cstCustomerBiz" ref="cstCustomerBiz" /> </bean> </beans>
十二、struts的配置文件
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"> <struts> <!-- 由spring管理 --> <constant name="stuts.objectFactory" value="spring" /> <!-- 打開開發模式 --> <constant name="struts.devMode" value="true" /> <package name="action" extends="struts-default" namespace="/"> <action name="search" class="customerManagerAction" method="findByProperties"> <result name="success">/listTrue.jsp</result> <result name="input">/listShow.jsp</result> </action> </package> </struts>