z_xiaofei168 发表于 2013-1-23 01:24:28

B2C电子商务网站:三大框架、ajax、jquery和json整合开发的商品类型的自连接操作(即无限级别分类)

B2C电子商务网站:三大框架、ajax、jquery和json整合开发的商品类型的自连接操作(即无限级别分类)
 
        最近一直在做B2C电子商务网站,前天在写商品类型的自连接操作(即无限级别分类),以前写过一个用servlet+jsp写的。但是现在我们要用三大框架整合开发。开始还没思路,后来经过反复的思考,慢慢的琢磨出来了,今天就和大家分享一下,或许大家以后能用的到。
        详细代码如下:
 list.jsp<s:a href="types/insert.jsp">添加</s:a>insert.jsp<tr><td class="gridViewHeader">类别所属分类</td><td class="gridViewItem" id="parents"></td></tr> 
  Types.java
 
package cn.z_xiaofei168.domain;import java.util.HashSet;import java.util.Set;public class Types implements java.io.Serializable {/** * @author z_xiaofei168 */private static final long serialVersionUID = 1L;private Integer id;private String name;private String description;/** 自关联 */private Types parents;// 父节点private Set<Types> childrens;// 子节点private Set<Goods> goodses = new HashSet<Goods>(0);                省略所有的set和get方法} 
  Types.hbm.xml
 
  
<?xml version="1.0" encoding="utf-8"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"                                 "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><hibernate-mapping default-lazy="false"package="cn.z_xiaofei168.domain"><class name="Types" table="types"><id name="id" type="java.lang.Integer"><column name="id" /><generator class="identity" /></id><property generated="never" lazy="false" name="name"type="java.lang.String"><column length="50" name="name" not-null="true" /></property><property generated="never" lazy="false" name="description"type="java.lang.String"><column length="100" name="description" /></property><many-to-one name="parents" class="Types" column="types_id"lazy="false" /><set name="childrens" inverse="true" cascade="save-update"><key column="types_id" /><one-to-many class="Types" /></set><set inverse="true" lazy="false" name="goodses" sort="unsorted"><key><column name="type_id" not-null="true" /></key><one-to-many class="Goods" /></set></class></hibernate-mapping> 
  
<script type="text/javascript"src="${pageContext.request.contextPath}/js/jquery-1.6.js"></script><script type="text/javascript">window.onload = function(){alert("test1");fun();}function fun(){$.ajax({type : "post",url : "csdn/types_findTypes.action",dataType : "json",success : function(data) {$("#parents").empty();var sel = $("<select>").attr("name", "parents.id");var opt = $("<option>").attr("value", 0).text("--根目录--");sel.append(opt);$.each(data.entities, function(i, entity) {var opts = $("<option>").attr("value", entity.id);opts.text(entity.name);sel.append(opts);});sel.appendTo($("#parents"));}});}</script>  
  TypesAction.java
 
   TypesServiceImpl.java
 
  
package cn.z_xiaofei168.action;import java.util.List;import cn.z_xiaofei168.dao.Pagination;import cn.z_xiaofei168.domain.Types;import cn.z_xiaofei168.service.TypesServiceImpl;import com.opensymphony.xwork2.ActionSupport;public class TypesAction extends ActionSupport {/** * @author z_xiaofei168 */private static final long serialVersionUID = 1L;/** 业务层对象 */private TypesServiceImpl typesServiceImpl;private Types entity;private List<Types> entities;private Types parents;//省略所有属性的set和get方法/** 添加的操作 */public String add() throws Exception {/** 设置其父节点 */if (parents.getId() == null||parents.getId()<=0) {entity.setParents(null);} else {parents = typesServiceImpl.findById(parents.getId());entity.setParents(parents);}typesServiceImpl.add(entity);return "add";}public String findTypes() throws Exception {entities = typesServiceImpl.findAll();return SUCCESS;}} 
  TypesServiceImpl.java
 
 package cn.z_xiaofei168.service;import java.util.List;import cn.z_xiaofei168.dao.TypesDaoImpl;import cn.z_xiaofei168.domain.Types;public class TypesServiceImpl implements TypesService {private TypesDaoImpl typesDaoImpl;public void setTypesDaoImpl(TypesDaoImpl typesDaoImpl) {this.typesDaoImpl = typesDaoImpl;}public void add(Types entity) throws Exception {typesDaoImpl.add(entity);}public List<Types> findAll() throws Exception {return typesDaoImpl.findAll();}public Types findById(Integer id) throws Exception {return typesDaoImpl.findById(id);}} 
 
  TypesDaoImpl.java
 
 
package cn.z_xiaofei168.dao;import java.util.List;import java.util.Set;import org.springframework.orm.hibernate3.support.HibernateDaoSupport;import cn.z_xiaofei168.domain.Goods;import cn.z_xiaofei168.domain.Messages;import cn.z_xiaofei168.domain.Orderdetails;import cn.z_xiaofei168.domain.Types;public class TypesDaoImpl extends HibernateDaoSupport implements TypesDao {public void add(Types entity) throws Exception {this.getHibernateTemplate().save(entity);}@SuppressWarnings("unchecked")public List<Types> findAll() throws Exception {return this.getHibernateTemplate().find("from Types");}} 
  struts.xml
 
 
<!-- TypesAction的bean --><action name="types_*" class="typesAction" method="{1}"><result name="add" type="chain">types_list</result><result name="success" type="json"><param name="includeProperties">entities\[\d+\]\.id,entities\[\d+\]\.name</param></result></action  
    备注:struts.xml   返回的数据是<package name="goods" extends="json-default" namespace="/xiaofei">
 
     以上是大部分代码,像一些接口之类的我就没有往上面贴。相信大家都明白。若想看最后显示的结果,可以到我的相册看图片:  jaj3.bmp。


 
 
页: [1]
查看完整版本: B2C电子商务网站:三大框架、ajax、jquery和json整合开发的商品类型的自连接操作(即无限级别分类)