巴巴运动网学习笔记(61-65)
1.为产品实现选择分级类别的功能
a.根据parentId,获取该父类所有的产品类别数据
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public ActionForward selectUI(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response) 2 throws Exception { 3 ProductForm productForm = (ProductForm)form; 4 String jpql; 5 List<Object> params = new ArrayList<Object>(); 6 if(productForm.getProductTypeId()>0){ 7 jpql = " p.parent.id = ?1"; 8 params.add(productForm.getProductTypeId()); 9 }else { 10 jpql = " p.parent is null"; 11 } 12 13 request.setAttribute("productTypes", productService.getScrollResult(ProductType.class, -1, -1, jpql, params.toArray()).getResultList()); 14 return mapping.findForward("typelist"); 15 } 16 }
b.在页面中,根据是否有子类,判断是否可以选择
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <c:forEach items="${productTypes}" var="type" varStatus="status"> 2 <td><c:if test="${fn:length(type.child)>0}"> 3 <a style="text-decoration: none" href="<html:rewrite action='/control/product/manager'/>?method=selectUI&productTypeId=${type.id }" >${type.name}</a> 4 </c:if> 5 <c:if test="${fn:length(type.child)==0}"> 6 <input type="radio" name="types" onClick="javascript:getDicName('${type.id }','${type.name}')"/>${type.name } 7 </c:if></td> 8 <c:if test="${status.count%5==0}"></tr><tr></c:if> 9 </c:forEach>
2.整齐排列选择页面中的内容(5个一行)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <table width="100%" border="0" cellspacing="1" cellpadding="1"> 2 <tr><td id="typecontent"> 3 <table width="100%" border="0"> 4 <tr> 5 <c:forEach items="${productTypes}" var="type" varStatus="status"> 6 <td><c:if test="${fn:length(type.child)>0}"> 7 <a style="text-decoration: none" href="<html:rewrite action='/control/product/manager'/>?method=selectUI&productTypeId=${type.id }" >${type.name}</a> 8 </c:if> 9 <c:if test="${fn:length(type.child)==0}"> 10 <input type="radio" name="types" onClick="javascript:getDicName('${type.id }','${type.name}')"/>${type.name } 11 </c:if></td> 12 <c:if test="${status.count%5==0}"></tr><tr></c:if> 13 </c:forEach> 14 </tr></table>
3.在类别选择界面中增加导航菜单
a.根据当前productTypeId来获取导航数据,直到productTypeId为空
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 Stack<ProductType> navigation = new Stack<ProductType>(); 2 ProductType parent = productService.find(ProductType.class,productForm.getProductTypeId()); 3 while(parent!=null){ 4 navigation.push(parent); 5 parent = parent.getParent(); 6 } 7 List<ProductType> navigationList = new ArrayList<ProductType>(); 8 while(!navigation.empty()){ 9 navigationList.add(navigation.pop()); 10 } 11 request.setAttribute("navigation", navigationList);
b.在jsp页面中显示导航
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 >><a href="<html:rewrite action='/control/product/manager'/>?method=selectUI">顶级分类</a> 2 <c:forEach items="${navigation}" var="nv"> 3 >><a href="<html:rewrite action='/control/product/manager'/>?method=selectUI&productTypeId=${nv.id }">${nv.name }</a> 4 </c:forEach>
4.优化产品表单的客户端验证代码
a.设置一个类来存储每一个输入项,该类包括输入项的名称和错误描述
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 function formField(name,desc){ 2 this.name = name; 3 this.desc = desc; 4 }
b.将多个类存储到数组中,改多个if为一个for,检测每一个类
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 function verifyForm(objForm){ 2 tinyMCE.triggerSave();//手动把iframe的值赋给textarea表单元素 3 var formArray = new Array(new formField("name","产品名称"),new formField("productType","产品类别"),new formField("basePrice","底价"), 4 new formField("marketPrice","市场价"),new formField("sellPrice","销售价"),new formField("code","货号"),new formField("styleName","样式名称"), 5 new formField("image","样式图片"),new formField("name","产品名称"),new formField("brandId","品牌"),new formField("sex","性别"),new formField("description","产品描述")); 6 for(var i=0;i<formArray.length;i++){ 7 var objField = eval("objForm."+formArray[i].name); 8 if(trim(objField.value)==""){ 9 alert(formArray[i].desc+"不能为空"); 10 if(objfield.type!="hidden") objfield.focus(); 11 return false; 12 } 13 } 14 var fileName = trim(objForm.image.value); 15 if(fileName!=""){ 16 var extName = fileName.substring(fileName.lastIndexOf('.')+1,fileName.split('').length); 17 var validateType = ["bmp","png","gif","jpg","txt","pdf","doc","xls","ppt"]; 18 for(var i=0;i<validateType.length;i++){ 19 if(extName==validateType[i]) 20 return true; 21 } 22 alert("图片格式不合法"); 23 return false; 24 } 25 }
5.实现html编辑器本地图片上传