Struts2中使用<sx: />标签实现组合查询和带分页的例子

一、这是SSH框架下的demo:

 TestCustomer.zip

二、例子界面



 

 三、然后建立一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 onclick="location.href='edit.action?id=${customer.custNo }';" title="编辑" src="images/bt_edit.gif" class="op_button" />
						<img onclick="location.href='linkman.action?id=${customer.custNo}';" title="联系人" src="images/bt_linkman.gif" class="op_button" />
						<img onclick="location.href='activity.action?id=${customer.custNo}';" title="交往记录" src="images/bt_acti.gif" class="op_button" />
						<img onclick="location.href='orders.action?id=${customer.custNo }&name=${customer.custName}';" title="历史订单" src="images/bt_orders.gif" class="op_button" />
						<img onclick="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>    

猜你喜欢

转载自z18022893621.iteye.com/blog/1956098
今日推荐