日期:2014-05-16  浏览次数:20358 次

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

?

?