巴巴运动网的产品类别管理
1、项目图解
这次我们做的是产品管理的功能,也就是增删改查。。。
2、首先我们引入相应的jar包
3、我们开始做我们的相应的功能模块
页面的素材我会上传的,链接是:http://download.csdn.net/detail/cutter_point/8803985
这里我们先把上节的有几个工具类补足上来
PageIndex.java
/**
* 功能:存放第一个页码和最后一个页码
* 时间:2015年5月19日09:06:26
* 文件:PageIndex.java
* 作者:cutter_point
*/
package com.cutter_point.bean;
publicclass PageIndex
{
privatelongstartpage;
privatelongendpage;
public PageIndex()
{
}
public PageIndex(long startpage, long endpage)
{
this.startpage = startpage;
this.endpage = endpage;
}
publiclong getStartpage()
{
returnstartpage;
}
publicvoid setStartpage(long startpage)
{
this.startpage = startpage;
}
publiclong getEndpage()
{
returnendpage;
}
publicvoid setEndpage(long endpage)
{
this.endpage = endpage;
}
}
PageView.java
/**
* 功能:封装页面需要显示的信息
* 时间:2015年5月19日10:59:35
* 文件:PageView.java
* 作者:cutter_point
*/
package com.cutter_point.bean;
import java.util.List;
publicclass PageView<T>
{
private List<T> records; //要显示的当前页记录数据
private PageIndex pageindex; //页码显示的范围
privatelongtotalpage = 1; //总页数,先默认值是1
privateintmaxresult = 12; //每页需要显示的个数,首先默认是12
privateintcurrentpage = 1; //当前的页数
privatelongtotalrecords; //显示的总的记录数
public PageView(int maxresult, int currentpage) //构造函数
{
this.maxresult = maxresult;
this.currentpage = currentpage;
}
publicvoid setQueryResult(QueryResult qr)
{
this.setTotalrecords(qr.getTotalrecord()); //得到全部的记录数
this.setTotalpage(totalpage);
this.setRecords(qr.getResultList()); //设置要显示的记录
}
public List<T> getRecords()
{
returnrecords;
}
publicvoid setRecords(List<T> records)
{
this.records = records;
}
public PageIndex getPageindex()
{
returnpageindex;
}
publicvoid setPageindex(PageIndex pageindex)
{
this.pageindex = pageindex;
}
publiclong getTotalpage()
{
returntotalpage;
}
//总页数,先默认值是1
publicvoid setTotalpage(long totalpage)
{
this.totalpage = totalpage;
this.pageindex = WebTool.getPageIndex(maxresult,currentpage, totalpage);
}
publicint getMaxresult()
{
returnmaxresult;
}
publicvoid setMaxresult(int maxresult)
{
this.maxresult = maxresult;
}
publicint getCurrentpage()
{
returncurrentpage;
}
publicvoid setCurrentpage(int currentpage)
{
this.currentpage = currentpage;
}
publiclong getTotalrecords()
{
returntotalrecords;
}
publicvoid setTotalrecords(long totalrecords)
{
this.totalrecords = totalrecords; //得到数据库中的总记录数
long pageuse1 = totalrecords % maxresult;
long pageuse2 = totalrecords / maxresult;
totalpage = pageuse1 == 0 ? pageuse2 : pageuse2 + 1; //得到总页数
}
}
QueryResult.java
/**
* 功能:这儿类是为了存放分页的时候从数据库里面查找出来的数据和记录数
* 文件:QueryResult.java
* 时间:2015年5月14日21:45:42
* 作者:cutter_point
*/
package com.cutter_point.bean;
import java.util.List;
publicclass QueryResult<T>
{
private List<T> resultList;
privatelongtotalrecord; //总记录数
public List<T> getResultList()
{
returnresultList;
}
publicvoid setResultList(List<T> resultList)
{
this.resultList = resultList;
}
publiclong getTotalrecord()
{
returntotalrecord;
}
publicvoid setTotalrecord(long totalrecord)
{
this.totalrecord = totalrecord;
}
}
WebTool.java
/**
* 功能:计算出页码的显示
* 时间:2015年5月19日09:06:26
* 文件:WebTool.java
* 作者:cutter_point
*/
package com.cutter_point.bean;
publicclass WebTool
{
/**
* 获取页码索引范围
* @param viewpagecount 显示的页码数
* @param currentpage 当前页码
* @param totalpage 总页码
* @return
*/
publicstatic PageIndex getPageIndex(long viewpagecount, intcurrentpage, long totalpage)
{
long startpage = currentpage - (viewpagecount % 2 == 0 ? viewpagecount/2 - 1 :viewpagecount/2); //页面显示的开始页面
long endpage = currentpage + viewpagecount / 2; //得到页面终止的页面
if(startpage < 1)
{
startpage = 1;
if(totalpage >= viewpagecount) //如果总页数比显示的页面大的话
{
endpage = viewpagecount; //那么最后一个页码就是显示的个数
}
else
{
endpage = totalpage; //如果总页码比要求显示的页码还要少的话,那么就把总得页码当做该显示的最后的页码
}
}
if(endpage > totalpage) //如果页码的最后一位比总页数还大
{
endpage = totalpage; //吧总页码数当做应该显示的最后一个页码
if((endpage - viewpagecount) > 0)
{
//如果最后的页码比要求显示的页码大的话
startpage = endpage - viewpagecount + 1; //设定开始页码
}
else
{
//如果最后的页码比要求显示的总页码小的话
startpage = 1;
}
}
returnnew PageIndex(startpage, endpage);
}
}
ProductTypeManageAction.java
/**
* 功能:这个是父类与之类的添加与修改
* 时间:2015年5月20日15:40:17
* 文件:ProductTypeManageAction.java
* 作者:cutter_point
*/
packagecom.cutter_point.web.action.product;
import java.util.Map;
importjavax.annotation.Resource;
importorg.springframework.context.annotation.Scope;
importorg.springframework.stereotype.Component;
importcom.cutter_point.bean.product.ProductType;
importcom.cutter_point.service.product.ProductTypeService;
importcom.opensymphony.xwork2.ActionContext;
importcom.opensymphony.xwork2.ActionSupport;
@Component
@Scope("prototype")
public classProductTypeManageAction extends ActionSupport
{
@Resource
private ProductTypeService productTypeService;
private Integer parentid; //父类id
private String name;
private String note;
/**
* 显示类别添加界面
* @return Stringstruts2的返回跳转result
* @throws Exception
*/
public String addUI() throws Exception
{
return "add";
}
public String add() throws Exception
{
System.out.println(parentid + " 123132132131");
Map request = (Map)ActionContext.getContext().get("request");
ProductType type = new ProductType(this.getName(),this.getNote());
if(this.getParentid() != null &&this.getParentid() > 0)
type.setParent(newProductType(this.getParentid()));
productTypeService.save(type); //保存到数据库
request.put("message", "添加类别成功");
return "message";
}
public ProductTypeService getProductTypeService()
{
return productTypeService;
}
public void setProductTypeService(ProductTypeServiceproductTypeService)
{
this.productTypeService = productTypeService;
}
public Integer getParentid()
{
return parentid;
}
public void setParentid(Integer parentid)
{
this.parentid = parentid;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public String getNote()
{
return note;
}
public void setNote(String note)
{
this.note = note;
}
}
显示界面
producttypelist.jsp
<%@ page isELIgnored="false"contentType="text/html;charset=UTF-8" %>
<%@taglib uri="/struts-tags" prefix="s"%>
<html>
<head>
<title>产品类别管理</title>
<meta http-equiv="Content-Type"content="text/html; charset=UTF-8">
<link rel="stylesheet"href="../css/vip.css" type="text/css">
<script type="text/javascript">
//到指定的分页页面
function topage(page)
{
document.getElementById("page").value = page;
//alert(document.getElementById("page").value);
//document.form1.method= 'post';
//document.form1.submit();
var obj = document.getElementsByName("form1").item(0).submit();
}
</script>
<SCRIPT language=JavaScript src="../js/FoshanRen.js"></SCRIPT>
</head>
<body bgcolor="#FFFFFF"text="#000000" marginwidth="0" marginheight="0">
<form id="form11"name="form1" action="producttypelist"method="post">
<s:hidden id="page" name="page"/>
<s:hidden name="parentid" />
<table width="98%"border="0" cellspacing="1"cellpadding="2" align="center">
<tr ><td colspan="6" bgcolor="6f8ac4" align="right" >
<%@ include file="../share/fenye.jsp"%>
</td></tr>
<tr>
<td width="8%" bgcolor="6f8ac4"><div align="center"><fontcolor="#FFFFFF">代号</font></div></td>
<td width="5%" nowrapbgcolor="6f8ac4"><div align="center"><fontcolor="#FFFFFF">修改</font></div></td>
<td width="20%" bgcolor="6f8ac4"><div align="center"><fontcolor="#FFFFFF">产品类别名称</font></div></td>
<td width="10%" nowrapbgcolor="6f8ac4"><div align="center"><fontcolor="#FFFFFF">创建下级类别</font></div></td>
<td width="15%" bgcolor="6f8ac4"><div align="center"><fontcolor="#FFFFFF">所属父类</font></div></td>
<td nowrap bgcolor="6f8ac4"><div align="center"><fontcolor="#FFFFFF">备注</font></div></td>
</tr>
<!---------------------------LOOPSTART------------------------------>
<s:iterator value="#request.pageView.records" var="productType">
<tr>
<td bgcolor="f5f5f5"><div align="center"><s:propertyvalue="#productType.typeid"/></div></td>
<td bgcolor="f5f5f5"><div align="center">
<img src="../images/edit.gif" width="15" height="16"border="0"></a></div></td>
<td bgcolor="f5f5f5"><div align="center"><a href="producttypelist?parentid=<s:property value="#productType.typeid"/>"><s:propertyvalue="#productType.name"/></a></div></td>
<td bgcolor="f5f5f5"><div align="center"><a href="<s:url action="add-producttypemanage"/>?parentid=<s:property value="#productType.typeid"/>">创建子类别</a></div></td>
<td bgcolor="f5f5f5" align="center"><s:if test="%{#productType.parent != null}"><s:propertyvalue="#productType.parent.name"/></s:if></td>
<td bgcolor="f5f5f5"><s:property value="#productType.note"/></td>
</tr>
</s:iterator>
<!----------------------LOOPEND------------------------------->
<tr>
<td bgcolor="f5f5f5" colspan="6"align="center"><table width="100%"border="0" cellspacing="1"cellpadding="3">
<tr>
<td width="5%"></td>
<td width="85%">
<input name="AddDic" type="button"class="frm_btn" id="AddDic"onClick="javascript:window.location.href='<s:url action="add-producttypemanage" />?parentid=${param.parentid}'"value="添加类别">
<input name="query" type="button"class="frm_btn" id="query"onClick="javascript:" value=" 查询 ">
</td>
</tr>
</table></td>
</tr>
</table>
</form>
</body>
</html>
add_productType.jsp
<%@ page isELIgnored="false"contentType="text/html;charset=UTF-8" %>
<%@ taglib uri="/struts-tags" prefix="s"%>
<html>
<head>
<title>添加类别</title>
<meta http-equiv="Content-Type"content="text/html; charset=UTF-8">
<link rel="stylesheet"href="/css/vip.css" type="text/css">
<SCRIPT language=JavaScript src="/js/FoshanRen.js"></SCRIPT>
<script language="JavaScript">
function checkfm(form)
{
if (trim(form.name.value)=="")
{
alert("类别名称不能为空!");
form.name.focus();
return false;
}
if (byteLength(form.note.value)>200)
{
alert("备注不能大于100字!");
form.note.focus();
return false;
}
return true;
}
</script>
</head>
<body bgcolor="#FFFFFF"text="#000000" leftmargin="0" topmargin="0"marginwidth="0" marginheight="0">
<s:form action="/control/producttypemanage-add" method="post" οnsubmit="returncheckfm(this)">
<s:hidden name="parentid" />
<br>
<table width="90%"border="0" cellspacing="2"cellpadding="3" align="center">
<tr bgcolor="6f8ac4">
<td colspan="2" >
<fontcolor="#FFFFFF">添加类别:</font>
</td>
</tr>
<tr bgcolor="f5f5f5">
<td width="22%" >
<div align="right">类别名称:</div>
</td>
<td width="78%">
<s:textfield name="name" size="50"maxlength="50" />
<fontcolor="#FF0000">*</font>
</td>
</tr>
<tr bgcolor="f5f5f5">
<td width="22%"><div align="right">备注(100字以内):</div></td>
<td width="78%"><s:textfield name="note"size="80" maxlength="100"/>
</td>
</tr>
<tr bgcolor="f5f5f5">
<td colspan="2"><div align="center">
<input type="submit" name="SYS_SET"value=" 确定 " class="frm_btn">
</div></td>
</tr>
</table>
</s:form>
<br>
</body>
</html>
4、struts2的配置
<?xml version="1.0"encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD StrutsConfiguration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<include file="struts-default.xml" />
<constant name="struts.ObjectFactory" value="spring" /><!-- 表示这里面的action由spring进行创建 -->
<constant name="struts.devMode" value="true" />
<package name="control" namespace="/control"extends="struts-default">
<global-results>
<result name="message">/page/share/message.jsp</result>
</global-results>
<action name="center-*"><!-- 直接跳转,不需要经过class的验证,默认返回success -->
<result name="success">/page/controlcenter/{1}.jsp</result>
</action>
<action name="producttypelist" class="productTypeAction" method="execute">
<result name="list">/page/product/producttypelist.jsp</result>
</action>
<action name="*-producttypemanage" class="productTypeManageAction" method="{1}UI">
<result name="{1}">/page/product/{1}_productType.jsp</result>
</action>
<action name="producttypemanage-*" class="productTypeManageAction" method="{1}">
<result name="{1}">/page/product/{1}_productType.jsp</result>
</action>
</package>
</struts>
5、接下来我们测试一下页面的效果
我们访问这个网站
http://localhost:8080/ babaSport_0900_producttypemanage/control/center-main
我们把产品管理打开
这里就是分类了子类,前台第一页只显示初始的顶级父类
比如葫芦娃的子类
我们的sql语句
我们有添加功能
关于网站为什么要这样写,上一篇blog有介绍
6、总结
这里我们主要是要注意到,我们的页面是显示了顶级父类的,我们在查询的时候加了一个条件也就是父类的id,如果没有就是null,那么就是顶级父类
其次这里我们做了添加功能,也就是一个insert语句的,这里我们调用接口的方法productTypeService.save(type); //保存到数据库