標籤:j2ee 頁面類別添加 s2sh javascript 資料庫
巴巴運動網的產品類別管理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" onsubmit="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); //儲存到資料庫
【j2ee spring】34、巴巴運動網的產品類別管理