Before touching an infinite classification, this time to use, I wrote a bit, posted for reference.
Design of database category table and data
-- ------------------------------ Table structure for msg_type-- ----------------------------CREATE TABLE `msg_type` ( `id` int(11) NOT NULL AUTO_INCREMENT, `typename` varchar(50) DEFAULT NULL, `remarks` varchar(200) DEFAULT NULL, `parentid` int(11) DEFAULT NULL, `state` int(11) DEFAULT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8;INSERT INTO `msg_type` VALUES (‘1‘, ‘苗木‘, null, ‘0‘, ‘1‘);INSERT INTO `msg_type` VALUES (‘2‘, ‘花卉‘, null, ‘0‘, ‘1‘);INSERT INTO `msg_type` VALUES (‘3‘, ‘景观小品‘, null, ‘0‘, ‘1‘);INSERT INTO `msg_type` VALUES (‘4‘, ‘松树‘, null, ‘1‘, ‘1‘);INSERT INTO `msg_type` VALUES (‘5‘, ‘落叶松‘, null, ‘4‘, ‘1‘);INSERT INTO `msg_type` VALUES (‘6‘, ‘雪松‘, null, ‘4‘, ‘1‘);INSERT INTO `msg_type` VALUES (‘7‘, ‘杨树‘, null, ‘1‘, ‘1‘);INSERT INTO `msg_type` VALUES (‘8‘, ‘速生杨‘, null, ‘7‘, ‘1‘);
Entities corresponding to the Database category table
/** * Information Category entity * @Description TODO * @author ZK * @version 1.0 * @date 2015-4-5 PM 5:53:48 */public class Messaget ype {private int id; Private String TypeName; Private String remarks; private int parentid; private int state; public int Getparentid () {return parentid; } public void Setparentid (int parentid) {this.parentid = ParentID; } public int getId () {return id; } public void setId (int id) {this.id = ID; } public String Gettypename () {return typename; } public void Settypename (String typename) {this.typename = typename; } public String Getremarks () {return remarks; } public void Setremarks (String remarks) {this.remarks = remarks; } public int getState () {return state; public void SetState (int.) {this.state = state; }}
Category Entity Intermediate processing
/*** 类别 显示中间 处理* * @Description TODO* @author zk* @version 1.0* @date 2015-4-11 上午10:56:03*/public class MessageTypeLev extends MessageType { private int lev; public int getLev() { return lev; } public void setLev(int lev) { this.lev = lev; } /** * 类型的转换 * @param messageType */public void from(MessageType messageType) { this.setId(messageType.getId()); this.setParentid(messageType.getParentid()); this.setRemarks(messageType.getRemarks()); this.setState(messageType.getState()); this.setTypename(messageType.getTypename());} }
Controller Request Database Core
----------------------Core------------------------------//Call the business layer to get all the data for the category table Messagetypeservice ms = new Mess Agetypeservice (); list<messagetype> mlist = Ms.findall (); The final data to be obtained is "list<messagetypelev> backlist = new arraylist<messagetypelev> () after an infinitely classified order"; The Mlist collection is sorted and then reported to Backlist in Recmessagetype (backlist, mlist, 0, 0); The sorted data is processed again and output for (Messagetypelev messagetype:backlist) {messagetype.settypename (setnbsp (messag EType) + messagetype.gettypename ()); System.out.println (Messagetype.gettypename ()); ------------------------Core----------------------------/** * Displays the category name based on the hierarchical relationship * @param messageType * @return */private String setnbsp (Messagetypelev messageType) {stringbuffer sb = new StringBuffer (); for (int i = 0; i < Messagetype.getlev (); i++) {sb.append ("---"); }return sb.tostring (); }/** * Sort an array of infinite categorical collections * * @param mlist the collection to sort * @param ID of the parent ID is 0 start * @param lev layer in order to show indent and add */public void Recmessa Getype (list<messagetypelev> blist, list<messagetype> mlist, int id, int lev) {for (MessageType Messaget Ype:mlist) {if (Messagetype.getparentid () = = ID) {Messagetypelev Messagetypelev = new Messagetypelev (); /* In order to show the number of layers but also encapsulates a Messagetypelev type, and in the inheritance of the original MessageType also made a conversion [Messagetypelev.from (MessageType);] Here the method feels a little bit undesirable, after the solution, at present solve the problem first * * * MESSAGETYPELEV.FROM (MessageType); Messagetypelev.setlev (Lev); Blist.add (Messagetypelev); Recmessagetype (blist, Mlist, Messagetype.getid (), Lev + 1); } }}
Output Run effect above
苗木 ---松树------落叶松------雪松---杨树------速生杨花卉景观小品
Foreground interface display processing, generally saved to the request domain object, the foreground value
Display of drop-down boxes
<Select name="typeID" width="Max"> <option value="">--Please choose--</option> <C:foreach var ="category" items="${backlist}"> <option value="${category.id}">${category.typename}</option> </C:foreach> </Select>
At this point, the infinite classification of goods, as well as the front-end display and, to be continued to add classification and logical judgment, core code and ideas will be affixed. Reprint please declare, thank you.
Java code enables unlimited class display of commodity categories