- 浏览: 122008 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
ze_nana:
那楼主去哪个行业了?
怀念曾经的日子 -
誓言无声:
...
我的90后学生的一篇检查 -
lvwenwen:
不错,楼主要是能上传能跑的工程就更好了,楼主加油!
JMS(Java Massage Service)与MDB(Massage Driver Bean) -
lvff1314:
楼主的爬虫是自己用socket写的还是用的开源框架啊,可否跟大 ...
《说说做网站的事》我朋友写的关于自己开发一个网站的经验帖 -
MVC2008MVC:
感觉自己玩玩还行
《说说做网站的事》我朋友写的关于自己开发一个网站的经验帖
(1)jsp页面代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ include file="/taglibs.jsp"%>
<%@ page language="java" pageEncoding="UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>管理产品信息</title>
<link rel="stylesheet" type="text/css" href="../style/main.css"/>
</head>
<script type="text/javascript">
function startLink(productId){
if(confirm("你确信要启用该产品吗?")){
productListForm.action="startProduct.action?proId="+productId;
productListForm.submit();
return true;
}else{
return false;
}
}
function stopLink(productId){
if(confirm("你确信要停用该产品吗?")){
productListForm.action="stopProduct.action?proId="+productId;
productListForm.submit();
return true;
}else{
return false;
}
}
function deleteLink(productId){
if(confirm("你确信要删除该产品吗?")){
productListForm.action="deleteProduct.action?proId="+productId;
productListForm.submit();
return true;
}else{
return false;
}
}
function goPagelink(){
var go = document.getElementById("goPage").value;
document.location="toAppointPage.action?goPage="+go;
return true;
}
</script>
<body>
<div style="color:red">
<s:fielderror></s:fielderror>
</div>
<s:form action="productList" name="productListForm" method="post" theme="simple">
<div class="mainbox list">
<div class="morearea">
<a href="<%=request.getContextPath()%>/product/toAddProduct.action">发布产品信息</a>
</div>
<h1>管理产品信息</h1>
<table border="0" cellspacing="0" cellpadding="0" class="table" bordercolor="#cccccc">
<tbody>
<tr>
<th width="10%">编号</th>
<th width="25%">产品名称</th>
<th width="20%">创建时间</th>
<th width="20%">结束时间</th>
<th width="25%">操作</th>
</tr>
<c:forEach items="${cfProducts}" var="cfProduct">
<tr style=" width : 747px;">
<td><c:out value="${cfProduct.proId}"/></td>
<td><c:out value="${cfProduct.proTitle}"/></td>
<td><fmt:formatDate value="${cfProduct.proCreatTime}" pattern="yyyy-MM-dd HH:mm"/></td>
<td><fmt:formatDate value="${cfProduct.proEndTime}" pattern="yyyy-MM-dd HH:mm"/></td>
<td class="operate">
<a href="<%=request.getContextPath()%>/product/toUpdateProduct.action?proId=${cfProduct.proId}">修改</a>
<s:if test="cfProduct.proIsLock==0">
<span>|</span>
<a onclick="startLink('${cfProduct.proId}')">启用</a>
</s:if>
<s:else>
<span>|</span>
<a onclick="stopLink('${cfProduct.proId}')" class="operate">停止</a>
</s:else>
<span>|</span>
<a onclick="deleteLink('${cfProduct.proId}')" class="operate">删除</a>
</td>
</tr>
</c:forEach>
<tr>
<td colspan="5">
<div class="page">
<span class="disabled">第<s:text name="page.currentPage"></s:text>页</span>
<span class="disabled">共<s:text name="page.totalPage"></s:text>页</span>
<s:if test="page.hasPrePage">
<a href="<%=request.getContextPath()%>/product/toAppointPage.action?goPage=1">首页</a>
<a href="<%=request.getContextPath()%>/product/toAppointPage.action?goPage=${page.currentPage-1}">上一页</a>
</s:if>
<s:else>
<span class="disabled">首页</span>
<span class="disabled">上一页</span>
</s:else>
<s:if test="page.hasNextPage">
<a href="<%=request.getContextPath()%>/product/toAppointPage.action?goPage=${page.currentPage+1}">下一页</a>
<a href="<%=request.getContextPath()%>/product/toAppointPage.action?goPage=${page.totalPage}">尾页</a>
</s:if>
<s:else>
<span class="disabled">下一页</span>
<span class="disabled">尾页</span>
</s:else>
<span class="disabled">到</span>
<s:textfield name="goPage" id="goPage"></s:textfield>
<span class="disabled">页</span>
<a onclick="goPagelink()">Go</a>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</s:form>
</body>
</html>
(2)struts配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="product" extends="struts-default">
<action name="toAddProduct" class="productAction" method="toAddProduct">
<result>/product/product_add.jsp</result>
</action>
<action name="addProduct" class="productAction" method="addProduct">
<!--
<interceptor-ref name ="fileUpload" >
<param name ="allowedTypes" >
image/bmp,image/png,image/gif,image/jpeg,image/jpg
</param >
</interceptor-ref >
<interceptor-ref name ="defaultStack" />
-->
<result name="success" type="redirect">product/productList.action</result>
<result name="input">/product/product_add.jsp</result>
</action>
<action name="productList" class="productAction" method="listProducts">
<result>/product/product_list.jsp</result>
</action>
<action name="toUpdateProduct" class="productAction" method="toUpdateProduct">
<result name="success">/product/product_updata.jsp</result>
</action>
<action name="updateProduct" class="productAction" method="updateProduct">
<!--
<interceptor-ref name ="fileUpload" >
<param name ="allowedTypes" >
image/bmp,image/png,image/gif,image/jpeg,image/jpg
</param >
</interceptor-ref >
<interceptor-ref name ="defaultStack" />
-->
<result name="success" type="redirect">product/productList.action</result>
<result name="input">/product/product_updata.jsp</result>
</action>
<action name="startProduct" class="productAction" method="startProduct">
<result name="success" type="redirect">product/productList.action</result>
<result name="input">/product/product_list.jsp</result>
</action>
<action name="stopProduct" class="productAction" method="stopProduct">
<result name="success" type="redirect">product/productList.action</result>
<result name="input">/product/product_list.jsp</result>
</action>
<action name="toAppointPage" class="productAction" method="toAppointPage">
<result name="success">/product/product_list.jsp</result>
</action>
<action name="deleteProduct" class="productAction" method="deleteProduct">
<result name="success" type="redirect">product/productList.action</result>
<result name="input">/product/product_list.jsp</result>
</action>
</package>
</struts>
(3)action类
/**
* <p>ProductAction</p>
*
* 版权 (c) 2009
*
* <p>CFSW</p>
*
* 文件历史
* 日期 作者 描述
* 2009-10-09 xiangcheng.yue 创建
*
*/
package product.action;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.List;
import org.apache.struts2.ServletActionContext;
import product.beans.CfProduct;
import product.beans.CfProductclass;
import product.service.ProductService;
import util.IdGenerator;
import com.opensymphony.xwork2.ActionSupport;
import common.beans.Page;
import common.util.ConstantUtil;
import common.util.PageUtil;
/**
* 产品的查看,发布,修改,删除的Action.
*
* @author xiangcheng.yue
* @version 1.0
*/
public class ProductAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private static final int BUFFER_SIZE = 160 * 1024;
/** 产品Service。*/
private ProductService productService;
/** 产品对象。*/
private CfProduct cfProduct;
/** 企业对象。 */
//private CfCorporation cfCorporation;
/** 产品对象List。*/
private List<CfProduct> cfProducts;
/** 产品Id。*/
private String proId;
/** 产品修改页面是否修改产品图片标志,"0"表示没有修改,"1"表示修改。 */
private String updatePicFlag = "0";
/** 产品发布及修改页面是否修改企业信息的标志,"0"表示没有修改,"1"表示修改。 */
//private String updateCorporationFlag = "0";
/** 产品类型List。 */
private List<CfProductclass> productclassList;
/** 图片对象,为实现产品图片的上传及显示。 */
//private Picture pic = new Picture();
/** 分页对象,为实现产品List的分页。 */
private Page page = new Page();
/** 跳转页面。 */
private int goPage;
/** 为实现产品图片的上传及显示。 */
private File myFile;
private String contentType;
private String fileName;
private String imageFileName;
private String caption;
/**
* @since 2009-10-09
* 初始化产品List页面,并按照条件对List进行分页显示。
* @return String SUCCESS 显示产品List页面
*/
private static void copy(File src, File dst) {
try {
InputStream in = null ;
OutputStream out = null ;
try {
in = new BufferedInputStream( new FileInputStream(src), BUFFER_SIZE);
out = new BufferedOutputStream( new FileOutputStream(dst), BUFFER_SIZE);
byte [] buffer = new byte [BUFFER_SIZE];
while (in.read(buffer) > 0 ) {
out.write(buffer);
}
} finally {
if ( null != in) {
in.close();
}
if ( null != out) {
out.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public String getExtention(String fileName) {
int pos = fileName.lastIndexOf(".");
return fileName.substring(pos);
}
/**
* @since 2009-10-09
* 初始化产品List页面,并按照条件对List进行分页显示。
* @return String SUCCESS 显示产品List页面
*/
public String listProducts() {
cfProducts = productService.findAll();
page.setEveryPage(ConstantUtil.EVERYPAGE_SIZE);
page = PageUtil.createPage(page,cfProducts.size());
goPage = 1;
cfProducts = productService.getListByPage(CfProduct.class,page);
return SUCCESS;
}
/**
* @since 2009-10-09
* 确定要发布产品信息后,对新发布的产品的进行保存。
* @return String SUCCESS 返回产品List页面
*/
public String toAddProduct() {
cfProduct = new CfProduct();
productclassList = productService.findSortOfCfProductsort();
return SUCCESS;
}
/**
* @since 2009-10-09
* 确定要发布产品信息后,对新发布的产品的进行保存。
* @return String SUCCESS 返回产品List页面
*/
public String addProduct() {
cfProduct.setProId(IdGenerator.createId());
cfProduct.setProCreatTime(new Date());
imageFileName = new Date().getTime() + getExtention(fileName);
String picURl = ServletActionContext.getServletContext().getRealPath( "/uploadimages" ) + "\\" + imageFileName;
File imageFile = new File(ServletActionContext.getServletContext().getRealPath( "/uploadimages" ) + "/" + imageFileName);
copy(myFile, imageFile);
cfProduct.setProPicUrl(picURl);
productService.saveProduct(cfProduct);
return SUCCESS;
}
/**
* @since 2009-10-09
* 确定要修改产品信息后,初始化要修改的产品对象。
* @return String SUCCESS 跳转到产品修改页面
*/
@SuppressWarnings("unchecked")
public String toUpdateProduct() {
cfProduct = productService.findById(this.getProId());
productclassList = productService.findSortOfCfProductsort();
//cfCorporation = productService.findCfCorporationById(cfProduct.getProCorId());
return SUCCESS;
}
/**
* @since 2009-10-09
* 修改产品信息后,对修改了的产品的信息进行保存。
* @return String SUCCESS 跳转到产品List页面,并对该页面进行刷新。
*/
public String updateProduct() {
if("0" == updatePicFlag){
productService.saveProduct(cfProduct);
return SUCCESS;
}
productService.updateProduct(cfProduct);
return SUCCESS;
}
/**
* @since 2009-10-09
* 确定启用产品时,对产品的是否停用标志位进行修改。
* @return String SUCCESS 刷新List页面。
*/
public String startProduct() {
cfProduct = productService.findById(this.getProId());
cfProduct.setProIsLock(1);
productService.updateProduct(cfProduct);
return SUCCESS;
}
/**
* @since 2009-10-09
* 确定停用产品时,对产品的是否停用标志位进行修改。
* @return String SUCCESS 刷新List页面。
*/
public String stopProduct() {
cfProduct = productService.findById(this.getProId());
cfProduct.setProIsLock(0);
productService.updateProduct(cfProduct);
return SUCCESS;
}
/**
* @since 2009-10-09
* 点击分页标签时到确定页。
* @return String SUCCESS 指定确定页的数据,并刷新List页面。
*/
public String toAppointPage(){
page.setCurrentPage(this.goPage);
page = PageUtil.createPage(page);
cfProducts = productService.getListByPage(CfProduct.class,page);
return SUCCESS;
}
/**
* @since 2009-10-09
* 点击产品List页面,删除链接。
* @return String SUCCESS 把要删除的数据删除标志位设为1,并刷新List页面。
*/
public String deleteProduct(){
productService.deleteProduct(this.getProId());
return SUCCESS;
}
public void setCfProduct(CfProduct cfProduct) {
this.cfProduct = cfProduct;
}
public CfProduct getCfProduct() {
return cfProduct;
}
public List<CfProduct> getCfProducts() {
return cfProducts;
}
public void setCfProducts(List<CfProduct> cfProducts) {
this.cfProducts = cfProducts;
}
public ProductService getProductService() {
return productService;
}
public void setProductService(ProductService productService) {
this.productService = productService;
}
public String getProId() {
return proId;
}
public void setProId(String proId) {
this.proId = proId;
}
public static long getSerialVersionUID() {
return serialVersionUID;
}
public String getUpdatePicFlag() {
return updatePicFlag;
}
public void setUpdatePicFlag(String updatePicFlag) {
this.updatePicFlag = updatePicFlag;
}
public Page getPage() {
return page;
}
public void setPage(Page page) {
this.page = page;
}
public List<CfProductclass> getProductclassList() {
return productclassList;
}
public void setProductclassList(List<CfProductclass> productclassList) {
this.productclassList = productclassList;
}
public int getGoPage() {
return goPage;
}
public void setGoPage(int goPage) {
this.goPage = goPage;
}
public void setMyFileContentType(String contentType) {
this.contentType = contentType;
}
public void setMyFileFileName(String fileName) {
this.fileName = fileName;
}
public void setMyFile(File myFile) {
this.myFile = myFile;
}
public String getImageFileName() {
return imageFileName;
}
public String getCaption() {
return caption;
}
public void setCaption(String caption) {
this.caption = caption;
}
public void setImageFileName(String imageFileName) {
this.imageFileName = imageFileName;
}
}
(4)Service类及接口
接口类
/**
* <p>ProductService</p>
*
* 版权 (c) 2009
*
* <p>CFSW</p>
*
* 文件历史
* 日期 作者 描述
* 2009-10-09 xiangcheng.yue 创建
*
*/
package product.service;
import java.util.List;
import common.beans.Page;
import product.beans.CfProduct;
import product.beans.CfProductclass;
public interface ProductService {
public void saveProduct(CfProduct cfProduct);
public List<CfProduct> findAll();
public CfProduct findById(String proId);
public List<CfProductclass> findAllRootNode();
public List<CfProductclass> findAllChildNodeByParentNode(String parentNode);
public List<CfProductclass> findSortOfCfProductsort();
//public CfCorporation findCfCorporationById(String cfCorporationId);
public void updateProduct(CfProduct cfProduct);
@SuppressWarnings("unchecked")
public List<CfProduct> getListByPage(Class pojoClass,Page page);
public void deleteProduct(String proId);
}
实现类
/**
* <p>ProductServiceImpl</p>
*
* 版权 (c) 2009
*
* <p>CFSW</p>
*
* 文件历史
* 日期 作者 描述
* 2009-10-09 xiangcheng.yue 创建
*
*/
package product.service;
import java.util.ArrayList;
import java.util.List;
import common.beans.Page;
import corp.corporation.dao.CfCorporationDAO;
import product.beans.CfProduct;
import product.beans.CfProductclass;
import product.dao.CfProductDAO;
public class ProductServiceImpl implements ProductService {
/** 产品Dao。*/
private CfProductDAO cfProductDAO;
/** 企业Dao。*/
private CfCorporationDAO cfCorporationDAO;
/**
* @since 2009-10-09
* 保存产品对象。
* @param CfProduct cfProduct
* @return void 保存CfProduct对象。
*/
public void saveProduct(CfProduct cfProduct) {
cfProductDAO.save(cfProduct);
}
/**
* @since 2009-10-09
* 得到所有的产品List。
* @return List<CfProduct> 所有的产品List,即删除标志位为0的产品。
*/
@SuppressWarnings("unchecked")
public List<CfProduct> findAll() {
return cfProductDAO.findAll();
}
/**
* @since 2009-10-09
* 通过Id得到产品对象。
* @param String proId
* @return CfProduct 。
*/
public CfProduct findById(String proId) {
return cfProductDAO.findById(proId);
}
/**
* @since 2009-10-09
* 得到所有根产品类型,即父节点字段为空的数据。
* @return List<CfProductclass> 所有的父节点为空的产品类型。
*/
public List<CfProductclass> findAllRootNode() {
return cfProductDAO.findAllRootNode();
}
/**
* @since 2009-10-09
* 通过产品类型表中的父节点得到该父节点的所有子节点。
* @param String parentNode
* @return List<CfProductclass> 该父节点的所有子节点的List。
*/
public List<CfProductclass> findAllChildNodeByParentNode(String parentNode) {
return cfProductDAO.findAllChildNodeByParentNode(parentNode);
}
/**
* @since 2009-10-09
* 通过产品类型表中的父节点得到该父节点的所有子节点。
* @param String parentNode
* @return List<CfProductclass> 该父节点的所有子节点的List。
*/
@SuppressWarnings("unchecked")
public List<CfProductclass> findSortOfCfProductsort() {
List<CfProductclass> allRootNoteList = this.findAllRootNode();
List<CfProductclass> classOfCfCfProductclassList = new ArrayList();
if (allRootNoteList != null && allRootNoteList.size() > 0) {
for (CfProductclass rootNode : allRootNoteList) {
classOfCfCfProductclassList.add(rootNode);
List<CfProductclass> clildNodeOfOneParentList = this
.findAllChildNodeByParentNode(rootNode.getPsId());
if (clildNodeOfOneParentList != null
&& clildNodeOfOneParentList.size() > 0) {
List<CfProductclass> formatClildNodeOfOneParentList = new ArrayList();
for (CfProductclass clildNode : clildNodeOfOneParentList) {
clildNode.setPsName("--" + clildNode.getPsName());
formatClildNodeOfOneParentList.add(clildNode);
}
classOfCfCfProductclassList
.addAll(formatClildNodeOfOneParentList);
}
}
}
return classOfCfCfProductclassList;
}
/**
* @since 2009-10-09
* 通过企业Id得到企业对象。
* @param String cfCorporationId
* @return CfCorporation 该Id对应的企业对象。
*/
// @SuppressWarnings("unchecked")
// public CfCorporation findCfCorporationById(String cfCorporationId){
// return cfCorporationDAO.findById(cfCorporationId);
// }
/**
* @since 2009-10-09
* 通过class名和Page对象得到分页的数据。
* @param Class pojoClass
* @param Page page
* @return CfCorporation 要分页显示的数据的List。
*/
@SuppressWarnings("unchecked")
public List<CfProduct> getListByPage(Class pojoClass,Page page){
return cfProductDAO.getListByPage(pojoClass, page);
}
/**
* @since 2009-10-09
* 更新产品对象。
* @param CfProduct cfProduct
* @return viod 更新要更新的产品对象。
*/
public void updateProduct(CfProduct cfProduct){
cfProductDAO.attachDirty(cfProduct);
}
public void deleteProduct(String proId){
CfProduct cfProduct = cfProductDAO.findById(proId);
cfProduct.setProIsRecyle(1);
cfProductDAO.attachDirty(cfProduct);
}
public CfProductDAO getCfProductDAO() {
return cfProductDAO;
}
public void setCfProductDAO(CfProductDAO cfProductDAO) {
this.cfProductDAO = cfProductDAO;
}
public CfCorporationDAO getCfCorporationDAO() {
return cfCorporationDAO;
}
public void setCfCorporationDAO(CfCorporationDAO cfCorporationDAO) {
this.cfCorporationDAO = cfCorporationDAO;
}
}
(5)Dao类
package product.dao;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import common.beans.Page;
import product.beans.CfProduct;
import product.beans.CfProductclass;
/**
* Data access object (DAO) for domain model class CfProduct.
* @see com.alu.test.model.CfProduct
* @author MyEclipse Persistence Tools
*/
public class CfProductDAO extends HibernateDaoSupport {
private static final Log log = LogFactory.getLog(CfProductDAO.class);
//property constants
public static final String PRO_TITLE = "proTitle";
public static final String PRO_STITLE = "proSTitle";
public static final String PRO_MODEL = "proModel";
public static final String PRO_IS_FLAG = "proIsFlag";
public static final String PRO_MATERIAL = "proMaterial";
public static final String PRO_ORIGIN = "proOrigin";
public static final String PRO_TRADEMARK = "proTrademark";
public static final String PRO_SPEC = "proSpec";
public static final String PRO_TYPE = "proType";
public static final String PRO_PRICE = "proPrice";
public static final String PRO_PIC_URL = "proPicUrl";
public static final String PRO_CLASS_ID = "proClassId";
public static final String PRO_NAVI_CONTENT = "proNaviContent";
public static final String PRO_CONTENT = "proContent";
public static final String PRO_COR_ID = "proCorId";
public static final String PRO_CLICK = "proClick";
public static final String PRO_ORDER_ID = "proOrderId";
public static final String PRO_IS_LOCK = "proIsLock";
public static final String PRO_IS_RECYLE = "proIsRecyle";
public static final String PRO_IS_TIPS = "proIsTips";
public static final String PRO_IS_TIPS_READ = "proIsTipsRead";
public static final String PRO_IS_INDEX_FLAG = "proIsIndexFlag";
public static final String PRO_IS_RECOMMEND = "proIsRecommend";
protected void initDao() {
//do nothing
}
public void save(CfProduct transientInstance) {
log.debug("saving CfProduct instance");
try {
getHibernateTemplate().save(transientInstance);
log.debug("save successful");
} catch (RuntimeException re) {
log.error("save failed", re);
throw re;
}
}
public void delete(CfProduct persistentInstance) {
log.debug("deleting CfProduct instance");
try {
getHibernateTemplate().delete(persistentInstance);
log.debug("delete successful");
} catch (RuntimeException re) {
log.error("delete failed", re);
throw re;
}
}
public CfProduct findById( java.lang.String id) {
log.debug("getting CfProduct instance with id: " + id);
try {
CfProduct instance = (CfProduct) getHibernateTemplate()
.get(CfProduct.class, id);
return instance;
} catch (RuntimeException re) {
log.error("get failed", re);
throw re;
}
}
public List findByExample(CfProduct instance) {
log.debug("finding CfProduct instance by example");
try {
List results = getHibernateTemplate().findByExample(instance);
log.debug("find by example successful, result size: " + results.size());
return results;
} catch (RuntimeException re) {
log.error("find by example failed", re);
throw re;
}
}
public List findByProperty(String propertyName, Object value) {
log.debug("finding CfProduct instance with property: " + propertyName
+ ", value: " + value);
try {
String queryString = "from CfProduct as model where model."
+ propertyName + "= ?";
return getHibernateTemplate().find(queryString, value);
} catch (RuntimeException re) {
log.error("find by property name failed", re);
throw re;
}
}
public List findByProTitle(Object proTitle) {
return findByProperty(PRO_TITLE, proTitle);
}
public List findByProSTitle(Object proSTitle) {
return findByProperty(PRO_STITLE, proSTitle);
}
public List findByProModel(Object proModel) {
return findByProperty(PRO_MODEL, proModel);
}
public List findByProIsFlag(Object proIsFlag) {
return findByProperty(PRO_IS_FLAG, proIsFlag);
}
public List findByProMaterial(Object proMaterial) {
return findByProperty(PRO_MATERIAL, proMaterial);
}
public List findByProOrigin(Object proOrigin) {
return findByProperty(PRO_ORIGIN, proOrigin);
}
public List findByProTrademark(Object proTrademark) {
return findByProperty(PRO_TRADEMARK, proTrademark);
}
public List findByProSpec(Object proSpec) {
return findByProperty(PRO_SPEC, proSpec);
}
public List findByProType(Object proType) {
return findByProperty(PRO_TYPE, proType);
}
public List findByProPrice(Object proPrice) {
return findByProperty(PRO_PRICE, proPrice);
}
public List findByProPicUrl(Object proPicUrl) {
return findByProperty(PRO_PIC_URL, proPicUrl);
}
public List findByProClassId(Object proClassId) {
return findByProperty(PRO_CLASS_ID, proClassId);
}
public List findByProNaviContent(Object proNaviContent) {
return findByProperty(PRO_NAVI_CONTENT, proNaviContent);
}
public List findByProContent(Object proContent) {
return findByProperty(PRO_CONTENT, proContent);
}
public List findByProCorId(Object proCorId) {
return findByProperty(PRO_COR_ID, proCorId);
}
public List findByProClick(Object proClick) {
return findByProperty(PRO_CLICK, proClick);
}
public List findByProOrderId(Object proOrderId) {
return findByProperty(PRO_ORDER_ID, proOrderId);
}
public List findByProIsLock(Object proIsLock) {
return findByProperty(PRO_IS_LOCK, proIsLock);
}
public List findByProIsRecyle(Object proIsRecyle) {
return findByProperty(PRO_IS_RECYLE, proIsRecyle);
}
public List findByProIsTips(Object proIsTips) {
return findByProperty(PRO_IS_TIPS, proIsTips);
}
public List findByProIsTipsRead(Object proIsTipsRead) {
return findByProperty(PRO_IS_TIPS_READ, proIsTipsRead);
}
public List findByProIsIndexFlag(Object proIsIndexFlag) {
return findByProperty(PRO_IS_INDEX_FLAG, proIsIndexFlag);
}
public List findByProIsRecommend(Object proIsRecommend) {
return findByProperty(PRO_IS_RECOMMEND, proIsRecommend);
}
public List findAll() {
log.debug("finding all CfProduct instances");
try {
String queryString = "from CfProduct t where t.proIsRecyle = 0";
return getHibernateTemplate().find(queryString);
} catch (RuntimeException re) {
log.error("find all failed", re);
throw re;
}
}
public CfProduct merge(CfProduct detachedInstance) {
log.debug("merging CfProduct instance");
try {
CfProduct result = (CfProduct) getHibernateTemplate()
.merge(detachedInstance);
log.debug("merge successful");
return result;
} catch (RuntimeException re) {
log.error("merge failed", re);
throw re;
}
}
public void attachDirty(CfProduct instance) {
log.debug("attaching dirty CfProduct instance");
try {
getHibernateTemplate().saveOrUpdate(instance);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}
public void attachClean(CfProduct instance) {
log.debug("attaching clean CfProduct instance");
try {
getHibernateTemplate().lock(instance, LockMode.NONE);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}
public static CfProductDAO getFromApplicationContext(ApplicationContext ctx) {
return (CfProductDAO) ctx.getBean("CfProductDAO");
}
@SuppressWarnings("unchecked")
public List<CfProductclass> findAllRootNode(){
log.debug("finding all CfProductclass of rootNode");
try {
String queryString = "from CfProductclass t where t.psParentId is null";
return getHibernateTemplate().find(queryString);
} catch (RuntimeException re) {
log.error("find rootNode from CfProductclass failed", re);
throw re;
}
}
@SuppressWarnings("unchecked")
public List<CfProductclass> findAllChildNodeByParentNode(String parentNode){
log.debug("finding all ChildNode By ParentNode from CfProductclass");
try {
String queryString = "from CfProductclass t where t.psParentId =:parentNode";
Query query = this.getSession().createQuery(queryString);
query.setParameter("parentNode", parentNode);
return query.list();
} catch (RuntimeException re) {
log.error("find rootNode from CfProductclass failed", re);
throw re;
}
}
@SuppressWarnings("unchecked")
public List<CfProduct> getListByPage(Class pojoClass,Page page){
log.debug("saving CfProduct instance");
String querySentence = "from " + pojoClass.getName() + " as t where t.proIsRecyle = 0";
Query query = getSession().createQuery(querySentence);
query.setFirstResult(page.getBeginIndex())
.setMaxResults(page.getEveryPage());
return query.list();
}
}
(6)page类及pageUtil类
page类
package common.beans;
/**
* @author xiangcheng.yue
*
*/
public class Page{
/** imply if the page has previous page */
private boolean hasPrePage;
/** imply if the page has next page */
private boolean hasNextPage;
/** the number of every page */
private int everyPage;
/** the total page number */
private int totalPage;
/** the number of current page */
private int currentPage;
/** the begin index of the records by the current query */
private int beginIndex;
/** The default constructor */
public Page(){
}
/** construct the page by everyPage
* @param everyPage
* */
public Page(int everyPage){
this.everyPage = everyPage;
}
/** The whole constructor */
public Page(boolean hasPrePage, boolean hasNextPage,
int everyPage, int totalPage,
int currentPage, int beginIndex){
this.hasPrePage = hasPrePage;
this.hasNextPage = hasNextPage;
this.everyPage = everyPage;
this.totalPage = totalPage;
this.currentPage = currentPage;
this.beginIndex = beginIndex;
}
/**
* @return
* Returns the beginIndex.
*/
public int getBeginIndex(){
return beginIndex;
}
/**
* @param beginIndex
* The beginIndex to set.
*/
public void setBeginIndex(int beginIndex){
this.beginIndex = beginIndex;
}
/**
* @return
* Returns the currentPage.
*/
public int getCurrentPage(){
return currentPage;
}
/**
* @param currentPage
* The currentPage to set.
*/
public void setCurrentPage(int currentPage){
this.currentPage = currentPage;
}
/**
* @return
* Returns the everyPage.
*/
public int getEveryPage(){
return everyPage;
}
/**
* @param everyPage
* The everyPage to set.
*/
public void setEveryPage(int everyPage){
this.everyPage = everyPage;
}
/**
* @return
* Returns the hasNextPage.
*/
public boolean getHasNextPage(){
return hasNextPage;
}
/**
* @param hasNextPage
* The hasNextPage to set.
*/
public void setHasNextPage(boolean hasNextPage){
this.hasNextPage = hasNextPage;
}
/**
* @return
* Returns the hasPrePage.
*/
public boolean getHasPrePage(){
return hasPrePage;
}
/**
* @param hasPrePage
* The hasPrePage to set.
*/
public void setHasPrePage(boolean hasPrePage){
this.hasPrePage = hasPrePage;
}
/**
* @return Returns the totalPage.
*
*/
public int getTotalPage(){
return totalPage;
}
/**
* @param totalPage
* The totalPage to set.
*/
public void setTotalPage(int totalPage){
this.totalPage = totalPage;
}
}
pageUtil类
package common.util;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import common.beans.Page;
/**
* @author xiangcheng.yue
*
*/
public class PageUtil {
@SuppressWarnings("unused")
private static final Log logger = LogFactory.getLog(PageUtil.class);
public static Page createPage(Page page){
return createPage(page.getEveryPage(), page.getCurrentPage(), page.getTotalPage());
}
/**
* Use the origin page to create a new page
* @param page
* @param totalRecords
* @return
*/
public static Page createPage(Page page, int totalRecords){
return createPage(page.getEveryPage(), page.getCurrentPage(), totalRecords);
}
/**
* the basic page utils not including exception handler
* @param everyPage
* @param currentPage
* @param totalRecords
* @return page
*/
public static Page createPage(int everyPage, int currentPage, int totalRecords){
everyPage = getEveryPage(everyPage);
currentPage = getCurrentPage(currentPage);
int beginIndex = getBeginIndex(everyPage, currentPage);
int totalPage = getTotalPage(everyPage, totalRecords);
boolean hasNextPage = hasNextPage(currentPage, totalPage);
boolean hasPrePage = hasPrePage(currentPage);
return new Page(hasPrePage, hasNextPage,
everyPage, totalPage,
currentPage, beginIndex);
}
private static int getEveryPage(int everyPage){
return everyPage == 0 ? 10 : everyPage;
}
private static int getCurrentPage(int currentPage){
return currentPage == 0 ? 1 : currentPage;
}
private static int getBeginIndex(int everyPage, int currentPage){
return(currentPage - 1) * everyPage;
}
private static int getTotalPage(int everyPage, int totalRecords){
int totalPage = 0;
if(totalRecords == 0){
return totalPage++;
}
if(totalRecords % everyPage == 0)
totalPage = totalRecords / everyPage;
else
totalPage = totalRecords / everyPage + 1 ;
return totalPage;
}
private static boolean hasPrePage(int currentPage){
return currentPage == 1 ? false : true;
}
private static boolean hasNextPage(int currentPage, int totalPage){
return currentPage == totalPage || totalPage == 0 ? false : true;
}
}
上面是分页的全部代码,因为忙没时间仔细的整理,需要的人可以仔细看下,不好意思。
希望对看博客的有帮助。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ include file="/taglibs.jsp"%>
<%@ page language="java" pageEncoding="UTF-8" %>
<%@ taglib prefix="s" uri="/struts-tags" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>管理产品信息</title>
<link rel="stylesheet" type="text/css" href="../style/main.css"/>
</head>
<script type="text/javascript">
function startLink(productId){
if(confirm("你确信要启用该产品吗?")){
productListForm.action="startProduct.action?proId="+productId;
productListForm.submit();
return true;
}else{
return false;
}
}
function stopLink(productId){
if(confirm("你确信要停用该产品吗?")){
productListForm.action="stopProduct.action?proId="+productId;
productListForm.submit();
return true;
}else{
return false;
}
}
function deleteLink(productId){
if(confirm("你确信要删除该产品吗?")){
productListForm.action="deleteProduct.action?proId="+productId;
productListForm.submit();
return true;
}else{
return false;
}
}
function goPagelink(){
var go = document.getElementById("goPage").value;
document.location="toAppointPage.action?goPage="+go;
return true;
}
</script>
<body>
<div style="color:red">
<s:fielderror></s:fielderror>
</div>
<s:form action="productList" name="productListForm" method="post" theme="simple">
<div class="mainbox list">
<div class="morearea">
<a href="<%=request.getContextPath()%>/product/toAddProduct.action">发布产品信息</a>
</div>
<h1>管理产品信息</h1>
<table border="0" cellspacing="0" cellpadding="0" class="table" bordercolor="#cccccc">
<tbody>
<tr>
<th width="10%">编号</th>
<th width="25%">产品名称</th>
<th width="20%">创建时间</th>
<th width="20%">结束时间</th>
<th width="25%">操作</th>
</tr>
<c:forEach items="${cfProducts}" var="cfProduct">
<tr style=" width : 747px;">
<td><c:out value="${cfProduct.proId}"/></td>
<td><c:out value="${cfProduct.proTitle}"/></td>
<td><fmt:formatDate value="${cfProduct.proCreatTime}" pattern="yyyy-MM-dd HH:mm"/></td>
<td><fmt:formatDate value="${cfProduct.proEndTime}" pattern="yyyy-MM-dd HH:mm"/></td>
<td class="operate">
<a href="<%=request.getContextPath()%>/product/toUpdateProduct.action?proId=${cfProduct.proId}">修改</a>
<s:if test="cfProduct.proIsLock==0">
<span>|</span>
<a onclick="startLink('${cfProduct.proId}')">启用</a>
</s:if>
<s:else>
<span>|</span>
<a onclick="stopLink('${cfProduct.proId}')" class="operate">停止</a>
</s:else>
<span>|</span>
<a onclick="deleteLink('${cfProduct.proId}')" class="operate">删除</a>
</td>
</tr>
</c:forEach>
<tr>
<td colspan="5">
<div class="page">
<span class="disabled">第<s:text name="page.currentPage"></s:text>页</span>
<span class="disabled">共<s:text name="page.totalPage"></s:text>页</span>
<s:if test="page.hasPrePage">
<a href="<%=request.getContextPath()%>/product/toAppointPage.action?goPage=1">首页</a>
<a href="<%=request.getContextPath()%>/product/toAppointPage.action?goPage=${page.currentPage-1}">上一页</a>
</s:if>
<s:else>
<span class="disabled">首页</span>
<span class="disabled">上一页</span>
</s:else>
<s:if test="page.hasNextPage">
<a href="<%=request.getContextPath()%>/product/toAppointPage.action?goPage=${page.currentPage+1}">下一页</a>
<a href="<%=request.getContextPath()%>/product/toAppointPage.action?goPage=${page.totalPage}">尾页</a>
</s:if>
<s:else>
<span class="disabled">下一页</span>
<span class="disabled">尾页</span>
</s:else>
<span class="disabled">到</span>
<s:textfield name="goPage" id="goPage"></s:textfield>
<span class="disabled">页</span>
<a onclick="goPagelink()">Go</a>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</s:form>
</body>
</html>
(2)struts配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="product" extends="struts-default">
<action name="toAddProduct" class="productAction" method="toAddProduct">
<result>/product/product_add.jsp</result>
</action>
<action name="addProduct" class="productAction" method="addProduct">
<!--
<interceptor-ref name ="fileUpload" >
<param name ="allowedTypes" >
image/bmp,image/png,image/gif,image/jpeg,image/jpg
</param >
</interceptor-ref >
<interceptor-ref name ="defaultStack" />
-->
<result name="success" type="redirect">product/productList.action</result>
<result name="input">/product/product_add.jsp</result>
</action>
<action name="productList" class="productAction" method="listProducts">
<result>/product/product_list.jsp</result>
</action>
<action name="toUpdateProduct" class="productAction" method="toUpdateProduct">
<result name="success">/product/product_updata.jsp</result>
</action>
<action name="updateProduct" class="productAction" method="updateProduct">
<!--
<interceptor-ref name ="fileUpload" >
<param name ="allowedTypes" >
image/bmp,image/png,image/gif,image/jpeg,image/jpg
</param >
</interceptor-ref >
<interceptor-ref name ="defaultStack" />
-->
<result name="success" type="redirect">product/productList.action</result>
<result name="input">/product/product_updata.jsp</result>
</action>
<action name="startProduct" class="productAction" method="startProduct">
<result name="success" type="redirect">product/productList.action</result>
<result name="input">/product/product_list.jsp</result>
</action>
<action name="stopProduct" class="productAction" method="stopProduct">
<result name="success" type="redirect">product/productList.action</result>
<result name="input">/product/product_list.jsp</result>
</action>
<action name="toAppointPage" class="productAction" method="toAppointPage">
<result name="success">/product/product_list.jsp</result>
</action>
<action name="deleteProduct" class="productAction" method="deleteProduct">
<result name="success" type="redirect">product/productList.action</result>
<result name="input">/product/product_list.jsp</result>
</action>
</package>
</struts>
(3)action类
/**
* <p>ProductAction</p>
*
* 版权 (c) 2009
*
* <p>CFSW</p>
*
* 文件历史
* 日期 作者 描述
* 2009-10-09 xiangcheng.yue 创建
*
*/
package product.action;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.List;
import org.apache.struts2.ServletActionContext;
import product.beans.CfProduct;
import product.beans.CfProductclass;
import product.service.ProductService;
import util.IdGenerator;
import com.opensymphony.xwork2.ActionSupport;
import common.beans.Page;
import common.util.ConstantUtil;
import common.util.PageUtil;
/**
* 产品的查看,发布,修改,删除的Action.
*
* @author xiangcheng.yue
* @version 1.0
*/
public class ProductAction extends ActionSupport {
private static final long serialVersionUID = 1L;
private static final int BUFFER_SIZE = 160 * 1024;
/** 产品Service。*/
private ProductService productService;
/** 产品对象。*/
private CfProduct cfProduct;
/** 企业对象。 */
//private CfCorporation cfCorporation;
/** 产品对象List。*/
private List<CfProduct> cfProducts;
/** 产品Id。*/
private String proId;
/** 产品修改页面是否修改产品图片标志,"0"表示没有修改,"1"表示修改。 */
private String updatePicFlag = "0";
/** 产品发布及修改页面是否修改企业信息的标志,"0"表示没有修改,"1"表示修改。 */
//private String updateCorporationFlag = "0";
/** 产品类型List。 */
private List<CfProductclass> productclassList;
/** 图片对象,为实现产品图片的上传及显示。 */
//private Picture pic = new Picture();
/** 分页对象,为实现产品List的分页。 */
private Page page = new Page();
/** 跳转页面。 */
private int goPage;
/** 为实现产品图片的上传及显示。 */
private File myFile;
private String contentType;
private String fileName;
private String imageFileName;
private String caption;
/**
* @since 2009-10-09
* 初始化产品List页面,并按照条件对List进行分页显示。
* @return String SUCCESS 显示产品List页面
*/
private static void copy(File src, File dst) {
try {
InputStream in = null ;
OutputStream out = null ;
try {
in = new BufferedInputStream( new FileInputStream(src), BUFFER_SIZE);
out = new BufferedOutputStream( new FileOutputStream(dst), BUFFER_SIZE);
byte [] buffer = new byte [BUFFER_SIZE];
while (in.read(buffer) > 0 ) {
out.write(buffer);
}
} finally {
if ( null != in) {
in.close();
}
if ( null != out) {
out.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public String getExtention(String fileName) {
int pos = fileName.lastIndexOf(".");
return fileName.substring(pos);
}
/**
* @since 2009-10-09
* 初始化产品List页面,并按照条件对List进行分页显示。
* @return String SUCCESS 显示产品List页面
*/
public String listProducts() {
cfProducts = productService.findAll();
page.setEveryPage(ConstantUtil.EVERYPAGE_SIZE);
page = PageUtil.createPage(page,cfProducts.size());
goPage = 1;
cfProducts = productService.getListByPage(CfProduct.class,page);
return SUCCESS;
}
/**
* @since 2009-10-09
* 确定要发布产品信息后,对新发布的产品的进行保存。
* @return String SUCCESS 返回产品List页面
*/
public String toAddProduct() {
cfProduct = new CfProduct();
productclassList = productService.findSortOfCfProductsort();
return SUCCESS;
}
/**
* @since 2009-10-09
* 确定要发布产品信息后,对新发布的产品的进行保存。
* @return String SUCCESS 返回产品List页面
*/
public String addProduct() {
cfProduct.setProId(IdGenerator.createId());
cfProduct.setProCreatTime(new Date());
imageFileName = new Date().getTime() + getExtention(fileName);
String picURl = ServletActionContext.getServletContext().getRealPath( "/uploadimages" ) + "\\" + imageFileName;
File imageFile = new File(ServletActionContext.getServletContext().getRealPath( "/uploadimages" ) + "/" + imageFileName);
copy(myFile, imageFile);
cfProduct.setProPicUrl(picURl);
productService.saveProduct(cfProduct);
return SUCCESS;
}
/**
* @since 2009-10-09
* 确定要修改产品信息后,初始化要修改的产品对象。
* @return String SUCCESS 跳转到产品修改页面
*/
@SuppressWarnings("unchecked")
public String toUpdateProduct() {
cfProduct = productService.findById(this.getProId());
productclassList = productService.findSortOfCfProductsort();
//cfCorporation = productService.findCfCorporationById(cfProduct.getProCorId());
return SUCCESS;
}
/**
* @since 2009-10-09
* 修改产品信息后,对修改了的产品的信息进行保存。
* @return String SUCCESS 跳转到产品List页面,并对该页面进行刷新。
*/
public String updateProduct() {
if("0" == updatePicFlag){
productService.saveProduct(cfProduct);
return SUCCESS;
}
productService.updateProduct(cfProduct);
return SUCCESS;
}
/**
* @since 2009-10-09
* 确定启用产品时,对产品的是否停用标志位进行修改。
* @return String SUCCESS 刷新List页面。
*/
public String startProduct() {
cfProduct = productService.findById(this.getProId());
cfProduct.setProIsLock(1);
productService.updateProduct(cfProduct);
return SUCCESS;
}
/**
* @since 2009-10-09
* 确定停用产品时,对产品的是否停用标志位进行修改。
* @return String SUCCESS 刷新List页面。
*/
public String stopProduct() {
cfProduct = productService.findById(this.getProId());
cfProduct.setProIsLock(0);
productService.updateProduct(cfProduct);
return SUCCESS;
}
/**
* @since 2009-10-09
* 点击分页标签时到确定页。
* @return String SUCCESS 指定确定页的数据,并刷新List页面。
*/
public String toAppointPage(){
page.setCurrentPage(this.goPage);
page = PageUtil.createPage(page);
cfProducts = productService.getListByPage(CfProduct.class,page);
return SUCCESS;
}
/**
* @since 2009-10-09
* 点击产品List页面,删除链接。
* @return String SUCCESS 把要删除的数据删除标志位设为1,并刷新List页面。
*/
public String deleteProduct(){
productService.deleteProduct(this.getProId());
return SUCCESS;
}
public void setCfProduct(CfProduct cfProduct) {
this.cfProduct = cfProduct;
}
public CfProduct getCfProduct() {
return cfProduct;
}
public List<CfProduct> getCfProducts() {
return cfProducts;
}
public void setCfProducts(List<CfProduct> cfProducts) {
this.cfProducts = cfProducts;
}
public ProductService getProductService() {
return productService;
}
public void setProductService(ProductService productService) {
this.productService = productService;
}
public String getProId() {
return proId;
}
public void setProId(String proId) {
this.proId = proId;
}
public static long getSerialVersionUID() {
return serialVersionUID;
}
public String getUpdatePicFlag() {
return updatePicFlag;
}
public void setUpdatePicFlag(String updatePicFlag) {
this.updatePicFlag = updatePicFlag;
}
public Page getPage() {
return page;
}
public void setPage(Page page) {
this.page = page;
}
public List<CfProductclass> getProductclassList() {
return productclassList;
}
public void setProductclassList(List<CfProductclass> productclassList) {
this.productclassList = productclassList;
}
public int getGoPage() {
return goPage;
}
public void setGoPage(int goPage) {
this.goPage = goPage;
}
public void setMyFileContentType(String contentType) {
this.contentType = contentType;
}
public void setMyFileFileName(String fileName) {
this.fileName = fileName;
}
public void setMyFile(File myFile) {
this.myFile = myFile;
}
public String getImageFileName() {
return imageFileName;
}
public String getCaption() {
return caption;
}
public void setCaption(String caption) {
this.caption = caption;
}
public void setImageFileName(String imageFileName) {
this.imageFileName = imageFileName;
}
}
(4)Service类及接口
接口类
/**
* <p>ProductService</p>
*
* 版权 (c) 2009
*
* <p>CFSW</p>
*
* 文件历史
* 日期 作者 描述
* 2009-10-09 xiangcheng.yue 创建
*
*/
package product.service;
import java.util.List;
import common.beans.Page;
import product.beans.CfProduct;
import product.beans.CfProductclass;
public interface ProductService {
public void saveProduct(CfProduct cfProduct);
public List<CfProduct> findAll();
public CfProduct findById(String proId);
public List<CfProductclass> findAllRootNode();
public List<CfProductclass> findAllChildNodeByParentNode(String parentNode);
public List<CfProductclass> findSortOfCfProductsort();
//public CfCorporation findCfCorporationById(String cfCorporationId);
public void updateProduct(CfProduct cfProduct);
@SuppressWarnings("unchecked")
public List<CfProduct> getListByPage(Class pojoClass,Page page);
public void deleteProduct(String proId);
}
实现类
/**
* <p>ProductServiceImpl</p>
*
* 版权 (c) 2009
*
* <p>CFSW</p>
*
* 文件历史
* 日期 作者 描述
* 2009-10-09 xiangcheng.yue 创建
*
*/
package product.service;
import java.util.ArrayList;
import java.util.List;
import common.beans.Page;
import corp.corporation.dao.CfCorporationDAO;
import product.beans.CfProduct;
import product.beans.CfProductclass;
import product.dao.CfProductDAO;
public class ProductServiceImpl implements ProductService {
/** 产品Dao。*/
private CfProductDAO cfProductDAO;
/** 企业Dao。*/
private CfCorporationDAO cfCorporationDAO;
/**
* @since 2009-10-09
* 保存产品对象。
* @param CfProduct cfProduct
* @return void 保存CfProduct对象。
*/
public void saveProduct(CfProduct cfProduct) {
cfProductDAO.save(cfProduct);
}
/**
* @since 2009-10-09
* 得到所有的产品List。
* @return List<CfProduct> 所有的产品List,即删除标志位为0的产品。
*/
@SuppressWarnings("unchecked")
public List<CfProduct> findAll() {
return cfProductDAO.findAll();
}
/**
* @since 2009-10-09
* 通过Id得到产品对象。
* @param String proId
* @return CfProduct 。
*/
public CfProduct findById(String proId) {
return cfProductDAO.findById(proId);
}
/**
* @since 2009-10-09
* 得到所有根产品类型,即父节点字段为空的数据。
* @return List<CfProductclass> 所有的父节点为空的产品类型。
*/
public List<CfProductclass> findAllRootNode() {
return cfProductDAO.findAllRootNode();
}
/**
* @since 2009-10-09
* 通过产品类型表中的父节点得到该父节点的所有子节点。
* @param String parentNode
* @return List<CfProductclass> 该父节点的所有子节点的List。
*/
public List<CfProductclass> findAllChildNodeByParentNode(String parentNode) {
return cfProductDAO.findAllChildNodeByParentNode(parentNode);
}
/**
* @since 2009-10-09
* 通过产品类型表中的父节点得到该父节点的所有子节点。
* @param String parentNode
* @return List<CfProductclass> 该父节点的所有子节点的List。
*/
@SuppressWarnings("unchecked")
public List<CfProductclass> findSortOfCfProductsort() {
List<CfProductclass> allRootNoteList = this.findAllRootNode();
List<CfProductclass> classOfCfCfProductclassList = new ArrayList();
if (allRootNoteList != null && allRootNoteList.size() > 0) {
for (CfProductclass rootNode : allRootNoteList) {
classOfCfCfProductclassList.add(rootNode);
List<CfProductclass> clildNodeOfOneParentList = this
.findAllChildNodeByParentNode(rootNode.getPsId());
if (clildNodeOfOneParentList != null
&& clildNodeOfOneParentList.size() > 0) {
List<CfProductclass> formatClildNodeOfOneParentList = new ArrayList();
for (CfProductclass clildNode : clildNodeOfOneParentList) {
clildNode.setPsName("--" + clildNode.getPsName());
formatClildNodeOfOneParentList.add(clildNode);
}
classOfCfCfProductclassList
.addAll(formatClildNodeOfOneParentList);
}
}
}
return classOfCfCfProductclassList;
}
/**
* @since 2009-10-09
* 通过企业Id得到企业对象。
* @param String cfCorporationId
* @return CfCorporation 该Id对应的企业对象。
*/
// @SuppressWarnings("unchecked")
// public CfCorporation findCfCorporationById(String cfCorporationId){
// return cfCorporationDAO.findById(cfCorporationId);
// }
/**
* @since 2009-10-09
* 通过class名和Page对象得到分页的数据。
* @param Class pojoClass
* @param Page page
* @return CfCorporation 要分页显示的数据的List。
*/
@SuppressWarnings("unchecked")
public List<CfProduct> getListByPage(Class pojoClass,Page page){
return cfProductDAO.getListByPage(pojoClass, page);
}
/**
* @since 2009-10-09
* 更新产品对象。
* @param CfProduct cfProduct
* @return viod 更新要更新的产品对象。
*/
public void updateProduct(CfProduct cfProduct){
cfProductDAO.attachDirty(cfProduct);
}
public void deleteProduct(String proId){
CfProduct cfProduct = cfProductDAO.findById(proId);
cfProduct.setProIsRecyle(1);
cfProductDAO.attachDirty(cfProduct);
}
public CfProductDAO getCfProductDAO() {
return cfProductDAO;
}
public void setCfProductDAO(CfProductDAO cfProductDAO) {
this.cfProductDAO = cfProductDAO;
}
public CfCorporationDAO getCfCorporationDAO() {
return cfCorporationDAO;
}
public void setCfCorporationDAO(CfCorporationDAO cfCorporationDAO) {
this.cfCorporationDAO = cfCorporationDAO;
}
}
(5)Dao类
package product.dao;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import common.beans.Page;
import product.beans.CfProduct;
import product.beans.CfProductclass;
/**
* Data access object (DAO) for domain model class CfProduct.
* @see com.alu.test.model.CfProduct
* @author MyEclipse Persistence Tools
*/
public class CfProductDAO extends HibernateDaoSupport {
private static final Log log = LogFactory.getLog(CfProductDAO.class);
//property constants
public static final String PRO_TITLE = "proTitle";
public static final String PRO_STITLE = "proSTitle";
public static final String PRO_MODEL = "proModel";
public static final String PRO_IS_FLAG = "proIsFlag";
public static final String PRO_MATERIAL = "proMaterial";
public static final String PRO_ORIGIN = "proOrigin";
public static final String PRO_TRADEMARK = "proTrademark";
public static final String PRO_SPEC = "proSpec";
public static final String PRO_TYPE = "proType";
public static final String PRO_PRICE = "proPrice";
public static final String PRO_PIC_URL = "proPicUrl";
public static final String PRO_CLASS_ID = "proClassId";
public static final String PRO_NAVI_CONTENT = "proNaviContent";
public static final String PRO_CONTENT = "proContent";
public static final String PRO_COR_ID = "proCorId";
public static final String PRO_CLICK = "proClick";
public static final String PRO_ORDER_ID = "proOrderId";
public static final String PRO_IS_LOCK = "proIsLock";
public static final String PRO_IS_RECYLE = "proIsRecyle";
public static final String PRO_IS_TIPS = "proIsTips";
public static final String PRO_IS_TIPS_READ = "proIsTipsRead";
public static final String PRO_IS_INDEX_FLAG = "proIsIndexFlag";
public static final String PRO_IS_RECOMMEND = "proIsRecommend";
protected void initDao() {
//do nothing
}
public void save(CfProduct transientInstance) {
log.debug("saving CfProduct instance");
try {
getHibernateTemplate().save(transientInstance);
log.debug("save successful");
} catch (RuntimeException re) {
log.error("save failed", re);
throw re;
}
}
public void delete(CfProduct persistentInstance) {
log.debug("deleting CfProduct instance");
try {
getHibernateTemplate().delete(persistentInstance);
log.debug("delete successful");
} catch (RuntimeException re) {
log.error("delete failed", re);
throw re;
}
}
public CfProduct findById( java.lang.String id) {
log.debug("getting CfProduct instance with id: " + id);
try {
CfProduct instance = (CfProduct) getHibernateTemplate()
.get(CfProduct.class, id);
return instance;
} catch (RuntimeException re) {
log.error("get failed", re);
throw re;
}
}
public List findByExample(CfProduct instance) {
log.debug("finding CfProduct instance by example");
try {
List results = getHibernateTemplate().findByExample(instance);
log.debug("find by example successful, result size: " + results.size());
return results;
} catch (RuntimeException re) {
log.error("find by example failed", re);
throw re;
}
}
public List findByProperty(String propertyName, Object value) {
log.debug("finding CfProduct instance with property: " + propertyName
+ ", value: " + value);
try {
String queryString = "from CfProduct as model where model."
+ propertyName + "= ?";
return getHibernateTemplate().find(queryString, value);
} catch (RuntimeException re) {
log.error("find by property name failed", re);
throw re;
}
}
public List findByProTitle(Object proTitle) {
return findByProperty(PRO_TITLE, proTitle);
}
public List findByProSTitle(Object proSTitle) {
return findByProperty(PRO_STITLE, proSTitle);
}
public List findByProModel(Object proModel) {
return findByProperty(PRO_MODEL, proModel);
}
public List findByProIsFlag(Object proIsFlag) {
return findByProperty(PRO_IS_FLAG, proIsFlag);
}
public List findByProMaterial(Object proMaterial) {
return findByProperty(PRO_MATERIAL, proMaterial);
}
public List findByProOrigin(Object proOrigin) {
return findByProperty(PRO_ORIGIN, proOrigin);
}
public List findByProTrademark(Object proTrademark) {
return findByProperty(PRO_TRADEMARK, proTrademark);
}
public List findByProSpec(Object proSpec) {
return findByProperty(PRO_SPEC, proSpec);
}
public List findByProType(Object proType) {
return findByProperty(PRO_TYPE, proType);
}
public List findByProPrice(Object proPrice) {
return findByProperty(PRO_PRICE, proPrice);
}
public List findByProPicUrl(Object proPicUrl) {
return findByProperty(PRO_PIC_URL, proPicUrl);
}
public List findByProClassId(Object proClassId) {
return findByProperty(PRO_CLASS_ID, proClassId);
}
public List findByProNaviContent(Object proNaviContent) {
return findByProperty(PRO_NAVI_CONTENT, proNaviContent);
}
public List findByProContent(Object proContent) {
return findByProperty(PRO_CONTENT, proContent);
}
public List findByProCorId(Object proCorId) {
return findByProperty(PRO_COR_ID, proCorId);
}
public List findByProClick(Object proClick) {
return findByProperty(PRO_CLICK, proClick);
}
public List findByProOrderId(Object proOrderId) {
return findByProperty(PRO_ORDER_ID, proOrderId);
}
public List findByProIsLock(Object proIsLock) {
return findByProperty(PRO_IS_LOCK, proIsLock);
}
public List findByProIsRecyle(Object proIsRecyle) {
return findByProperty(PRO_IS_RECYLE, proIsRecyle);
}
public List findByProIsTips(Object proIsTips) {
return findByProperty(PRO_IS_TIPS, proIsTips);
}
public List findByProIsTipsRead(Object proIsTipsRead) {
return findByProperty(PRO_IS_TIPS_READ, proIsTipsRead);
}
public List findByProIsIndexFlag(Object proIsIndexFlag) {
return findByProperty(PRO_IS_INDEX_FLAG, proIsIndexFlag);
}
public List findByProIsRecommend(Object proIsRecommend) {
return findByProperty(PRO_IS_RECOMMEND, proIsRecommend);
}
public List findAll() {
log.debug("finding all CfProduct instances");
try {
String queryString = "from CfProduct t where t.proIsRecyle = 0";
return getHibernateTemplate().find(queryString);
} catch (RuntimeException re) {
log.error("find all failed", re);
throw re;
}
}
public CfProduct merge(CfProduct detachedInstance) {
log.debug("merging CfProduct instance");
try {
CfProduct result = (CfProduct) getHibernateTemplate()
.merge(detachedInstance);
log.debug("merge successful");
return result;
} catch (RuntimeException re) {
log.error("merge failed", re);
throw re;
}
}
public void attachDirty(CfProduct instance) {
log.debug("attaching dirty CfProduct instance");
try {
getHibernateTemplate().saveOrUpdate(instance);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}
public void attachClean(CfProduct instance) {
log.debug("attaching clean CfProduct instance");
try {
getHibernateTemplate().lock(instance, LockMode.NONE);
log.debug("attach successful");
} catch (RuntimeException re) {
log.error("attach failed", re);
throw re;
}
}
public static CfProductDAO getFromApplicationContext(ApplicationContext ctx) {
return (CfProductDAO) ctx.getBean("CfProductDAO");
}
@SuppressWarnings("unchecked")
public List<CfProductclass> findAllRootNode(){
log.debug("finding all CfProductclass of rootNode");
try {
String queryString = "from CfProductclass t where t.psParentId is null";
return getHibernateTemplate().find(queryString);
} catch (RuntimeException re) {
log.error("find rootNode from CfProductclass failed", re);
throw re;
}
}
@SuppressWarnings("unchecked")
public List<CfProductclass> findAllChildNodeByParentNode(String parentNode){
log.debug("finding all ChildNode By ParentNode from CfProductclass");
try {
String queryString = "from CfProductclass t where t.psParentId =:parentNode";
Query query = this.getSession().createQuery(queryString);
query.setParameter("parentNode", parentNode);
return query.list();
} catch (RuntimeException re) {
log.error("find rootNode from CfProductclass failed", re);
throw re;
}
}
@SuppressWarnings("unchecked")
public List<CfProduct> getListByPage(Class pojoClass,Page page){
log.debug("saving CfProduct instance");
String querySentence = "from " + pojoClass.getName() + " as t where t.proIsRecyle = 0";
Query query = getSession().createQuery(querySentence);
query.setFirstResult(page.getBeginIndex())
.setMaxResults(page.getEveryPage());
return query.list();
}
}
(6)page类及pageUtil类
page类
package common.beans;
/**
* @author xiangcheng.yue
*
*/
public class Page{
/** imply if the page has previous page */
private boolean hasPrePage;
/** imply if the page has next page */
private boolean hasNextPage;
/** the number of every page */
private int everyPage;
/** the total page number */
private int totalPage;
/** the number of current page */
private int currentPage;
/** the begin index of the records by the current query */
private int beginIndex;
/** The default constructor */
public Page(){
}
/** construct the page by everyPage
* @param everyPage
* */
public Page(int everyPage){
this.everyPage = everyPage;
}
/** The whole constructor */
public Page(boolean hasPrePage, boolean hasNextPage,
int everyPage, int totalPage,
int currentPage, int beginIndex){
this.hasPrePage = hasPrePage;
this.hasNextPage = hasNextPage;
this.everyPage = everyPage;
this.totalPage = totalPage;
this.currentPage = currentPage;
this.beginIndex = beginIndex;
}
/**
* @return
* Returns the beginIndex.
*/
public int getBeginIndex(){
return beginIndex;
}
/**
* @param beginIndex
* The beginIndex to set.
*/
public void setBeginIndex(int beginIndex){
this.beginIndex = beginIndex;
}
/**
* @return
* Returns the currentPage.
*/
public int getCurrentPage(){
return currentPage;
}
/**
* @param currentPage
* The currentPage to set.
*/
public void setCurrentPage(int currentPage){
this.currentPage = currentPage;
}
/**
* @return
* Returns the everyPage.
*/
public int getEveryPage(){
return everyPage;
}
/**
* @param everyPage
* The everyPage to set.
*/
public void setEveryPage(int everyPage){
this.everyPage = everyPage;
}
/**
* @return
* Returns the hasNextPage.
*/
public boolean getHasNextPage(){
return hasNextPage;
}
/**
* @param hasNextPage
* The hasNextPage to set.
*/
public void setHasNextPage(boolean hasNextPage){
this.hasNextPage = hasNextPage;
}
/**
* @return
* Returns the hasPrePage.
*/
public boolean getHasPrePage(){
return hasPrePage;
}
/**
* @param hasPrePage
* The hasPrePage to set.
*/
public void setHasPrePage(boolean hasPrePage){
this.hasPrePage = hasPrePage;
}
/**
* @return Returns the totalPage.
*
*/
public int getTotalPage(){
return totalPage;
}
/**
* @param totalPage
* The totalPage to set.
*/
public void setTotalPage(int totalPage){
this.totalPage = totalPage;
}
}
pageUtil类
package common.util;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import common.beans.Page;
/**
* @author xiangcheng.yue
*
*/
public class PageUtil {
@SuppressWarnings("unused")
private static final Log logger = LogFactory.getLog(PageUtil.class);
public static Page createPage(Page page){
return createPage(page.getEveryPage(), page.getCurrentPage(), page.getTotalPage());
}
/**
* Use the origin page to create a new page
* @param page
* @param totalRecords
* @return
*/
public static Page createPage(Page page, int totalRecords){
return createPage(page.getEveryPage(), page.getCurrentPage(), totalRecords);
}
/**
* the basic page utils not including exception handler
* @param everyPage
* @param currentPage
* @param totalRecords
* @return page
*/
public static Page createPage(int everyPage, int currentPage, int totalRecords){
everyPage = getEveryPage(everyPage);
currentPage = getCurrentPage(currentPage);
int beginIndex = getBeginIndex(everyPage, currentPage);
int totalPage = getTotalPage(everyPage, totalRecords);
boolean hasNextPage = hasNextPage(currentPage, totalPage);
boolean hasPrePage = hasPrePage(currentPage);
return new Page(hasPrePage, hasNextPage,
everyPage, totalPage,
currentPage, beginIndex);
}
private static int getEveryPage(int everyPage){
return everyPage == 0 ? 10 : everyPage;
}
private static int getCurrentPage(int currentPage){
return currentPage == 0 ? 1 : currentPage;
}
private static int getBeginIndex(int everyPage, int currentPage){
return(currentPage - 1) * everyPage;
}
private static int getTotalPage(int everyPage, int totalRecords){
int totalPage = 0;
if(totalRecords == 0){
return totalPage++;
}
if(totalRecords % everyPage == 0)
totalPage = totalRecords / everyPage;
else
totalPage = totalRecords / everyPage + 1 ;
return totalPage;
}
private static boolean hasPrePage(int currentPage){
return currentPage == 1 ? false : true;
}
private static boolean hasNextPage(int currentPage, int totalPage){
return currentPage == totalPage || totalPage == 0 ? false : true;
}
}
上面是分页的全部代码,因为忙没时间仔细的整理,需要的人可以仔细看下,不好意思。
希望对看博客的有帮助。
相关推荐
java+hibernate实现分页 public String execute() throws Exception { System.out.println("Page:" + page); pagePlanList = ps.findPlantByPage(page, rowsPerPage); totalPage = ps.getPlanTotalPage...
### 使用Hibernate实现分页查询 #### 一、分页查询概念及原理 分页查询是一种在数据量较大的情况下,为了提高用户体验和系统性能而采取的一种技术手段。它将查询结果分成若干页显示,用户可以通过翻页操作查看不同...
### Hibernate 实现分页查询详解 #### 一、引言 在进行数据库操作时,为了提高用户体验和系统性能,分页查询是一项非常重要的技术。...以上就是关于Hibernate实现分页查询的具体介绍,希望对大家有所帮助。
根据提供的标题、描述以及部分内文,我们可以梳理出关于如何使用Struts2与Hibernate实现分页功能的关键知识点。 ### Struts2与Hibernate简介 - **Struts2**:这是一个基于MVC架构的开源Web框架,它能帮助开发者...
【Struts2+Hibernate实现分页详解】 在Java Web开发中,Struts2和Hibernate是两个非常重要的框架,它们分别负责MVC模式中的控制层和持久层。Struts2提供了强大的Action类和拦截器,使得业务逻辑处理更加简洁;而...
本项目“Extjs+Spring+Hibernate实现分页”结合了三种强大的技术,以构建一个高效、灵活的数据分页系统。下面将详细介绍这三个技术以及它们在分页中的应用。 1. **ExtJS**:ExtJS是一个JavaScript库,专门用于构建...
在Java Web开发中,"Struts+Hibernate 实现分页"是一个常见的需求,尤其是在处理大量数据展示时。...通过研究这些文件,你可以更好地理解和学习如何在实际项目中结合Struts和Hibernate实现分页功能。
本篇文章将深入探讨如何在Struts框架中利用Hibernate实现分页功能,以提高应用性能和用户体验。 分页是大型数据集展示的关键技术,它允许用户逐步浏览大量信息,而不是一次性加载所有数据,这可以显著提升页面加载...
本项目“struts+hibernate实现分页”结合了这两个强大的工具,旨在展示如何在实际应用中实现数据的分页显示,以提高用户体验并优化系统性能。 分页是Web应用程序中常见的功能,特别是在处理大量数据时。它将数据库...
本教程将详细介绍如何使用Hibernate实现分页功能。 首先,我们需要了解分页的基本概念。分页是将一个大的数据集分割成多个小的部分,每个部分称为一页。用户每次只加载一页数据,可以按需浏览或跳转到其他页。在...
本教程将详细讲解如何使用Hibernate实现分页功能,这对于处理大量数据的Web应用来说至关重要,因为它能够有效地减少数据库负载,提高用户体验。 一、Hibernate分页基础 1. Hibernate的Query和Criteria API都提供了...
hibernate分页
以下将详细讲解如何使用这三个框架实现分页功能。 首先,Spring作为核心的依赖注入(DI)和面向切面编程(AOP)框架,负责管理应用程序的组件,包括SessionFactory的创建和管理。在配置文件中,我们可以通过Spring...
根据提供的标题、描述、标签及部分内容,我们可以了解到这篇文章主要探讨的是如何在Struts2与Hibernate框架结合下实现分页功能。接下来将详细解析Struts2与Hibernate如何协作完成这一任务。 ### Struts2与Hibernate...
ExtJs4.2+Mysql+Struts2+Hibernate3实现分页查询 1.libs目录缺少hibernate核心jar包 2.libs目录缺少struts jar 3.WebRoot目录缺少ExtJs4.2核心类库 以上信息我都在项目里面注明了,因为这些内容的文件太大了,CSDN不...
总的来说,使用Struts和Hibernate实现分页功能,需要结合MVC模式和ORM思想,合理设计数据访问层和业务逻辑层,同时确保前端与后端的有效通信。这个过程涉及到了Java编程、数据库操作、框架理解和页面渲染等多个方面...
**二、使用Hibernate实现分页** 在Hibernate中,我们可以使用`Criteria`、`Query`或者`JPQL`(Java Persistence Query Language)来实现分页。这里以`Criteria`为例: 1. **创建Criteria**:首先,我们需要通过...
#### 四、dwr+hibernate实现分页技术的关键步骤 ##### 4.1 准备工作 在开始之前,确保项目中已经集成了dwr和hibernate两个框架。这通常涉及到添加相应的依赖库以及配置文件的设置。对于dwr,主要是引入必要的...