本文实现了递归思路的无限分类,虽然递归效率比较低,但是应付一般的项目还是可以的,这里我直接贴代码说明
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/classes/beans.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>struts.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <constant name="struts.devMode" value="true" /> <constant name="struts.multipart.maxSize" value="2147483648"/> <constant name="struts.objectFactory" value="spring" /> <package name="index" namespace="/" extends="struts-default,json-default"> <action name="index" class="com.schoolaround.action.IndexAction" method="index"> <result>/admin/index.jsp</result> </action> </package> <package name="shop" namespace="/" extends="struts-default,json-default"> <action name="shopcategory" class="com.schoolaround.action.ShopControlAction" method="shopcategory"> <result>/admin/shopcategory.jsp</result> </action> </package> </struts>beasns.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <context:component-scan base-package="com.schoolaround.*"/> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"> <property name="driverClass" value="com.mysql.jdbc.Driver"/> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/schoolaround?useUnicode=true&characterEncoding=UTF-8"/> <property name="user" value="root"/> <property name="password" value="root"/> <property name="initialPoolSize" value="3"/> <property name="minPoolSize" value="5"/> <property name="maxPoolSize" value="20"/> <property name="maxIdleTime" value="120"/> <property name="acquireIncrement" value="2"/> <property name="idleConnectionTestPeriod" value="60"/> </bean> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="mappingResources"> <list> <value>com/schoolaround/pojo/ShopCategory.hbm.xml</value> </list> </property> <property name="hibernateProperties"> <value> hibernate.dialect=org.hibernate.dialect.MySQL5Dialect hibernate.hbm2ddl.auto=update hibernate.show_sql=true hibernate.format_sql=false </value> </property> </bean> <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <tx:annotation-driven transaction-manager="txManager"/> </beans>pojo
ShopCategory.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 package="com.schoolaround.pojo"> <class name="ShopCategory" table="shopcategory"> <id name="shopcategoryid" type="int"> <column name="shopcategoryid"/> <generator class="increment" /> </id> <property name="shopcategory" length="10"></property> <property name="pid" length="10"></property> </class> </hibernate-mapping>
package com.schoolaround.pojo; public class ShopCategory { private int shopcategoryid; private String shopcategory; private int pid; public int getShopcategoryid() { return shopcategoryid; } public void setShopcategoryid(int shopcategoryid) { this.shopcategoryid = shopcategoryid; } public String getShopcategory() { return shopcategory; } public void setShopcategory(String shopcategory) { this.shopcategory = shopcategory; } public int getPid() { return pid; } public void setPid(int pid) { this.pid = pid; } }下面dao层和service层的代码就不贴出来了,我直接贴Action中是如何实现的
ShopControlAction.java
import java.io.ByteArrayInputStream; import java.io.InputStream; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.annotation.Resource; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletRequest; import org.apache.struts2.ServletActionContext; import org.apache.struts2.json.annotations.JSON; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Controller; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; import com.schoolaround.pojo.ShopCategory; import com.schoolaround.service.ShopCategoryService; @Controller @Scope(value = "prototype") public class ShopControlAction extends ActionSupport{ private String result = null; private int shopcategoryid; private String shopcategory; private int pid; private List categoryList = new ArrayList(); private List category = new ArrayList(); private ShopCategoryService shopcategoryService; private List<ShopCategory> resultlist = new ArrayList<ShopCategory>(); public int getShopcategoryid() { return shopcategoryid; } public void setShopcategoryid(int shopcategoryid) { this.shopcategoryid = shopcategoryid; } public String getShopcategory() { return shopcategory; } public void setShopcategory(String shopcategory) { this.shopcategory = shopcategory; } public int getPid() { return pid; } public void setPid(int pid) { this.pid = pid; } public List<ShopCategory> getCategoryList() { return categoryList; } public void setCategoryList(List<ShopCategory> categoryList) { this.categoryList = categoryList; } public List getCategory() { return category; } public void setCategory(List category) { this.category = category; } public ShopCategoryService getShopcategoryService() { return shopcategoryService; } @Resource public void setShopcategoryService(ShopCategoryService shopcategoryService) { this.shopcategoryService = shopcategoryService; } public String shopcategory() throws Exception{ List<ShopCategory> list = this.shopcategoryService.categoryList();//这里categoryList()为从数据库取出的分类表所有数据的list this.category = genCate(list,0,0,"shopcategory"); return SUCCESS; } //这里是递归部分的实现,pid是上级分类,list是要递归的数据list,level是遍历的层级,keyword主要用于输出树状的格式,主要在分类名前面连接树状符号 public List<ShopCategory> genCate(List<ShopCategory> list, int pid, int level,String keyword) { if(level==10) return resultlist; String l = ""; for(int i=0;i<level;i++){ l+=" "; } l=l+"└"; for(ShopCategory categorylist : list) { if(categorylist.getPid()==pid) { ShopCategory cate = new ShopCategory(); cate.setPid(categorylist.getPid()); cate.setShopcategoryid(categorylist.getShopcategoryid()); cate.setShopcategory(l+categorylist.getShopcategory()); resultlist.add(cate); genCate(list,categorylist.getShopcategoryid(), level+1,keyword); } } return resultlist; } }shopcategory.jsp主要代码
<%@ page language="java" pageEncoding="utf-8"%>
<%@ taglib uri="/struts-tags" prefix="s" %>
<s:select list="category" name="pid" listKey="shopcategoryid" listValue="shopcategory"></s:select>
<form id="fm_shopcategory" method="post" novalidate>
<div class="fitem">
<label>上级分类:</label>
<s:select list="category" name="pid" listKey="shopcategoryid" listValue="shopcategory"></s:select>
</div>
<div class="fitem">
<label>类别名:</label>
<input name="shopcategory" class="easyui-validatebox" required="true">
</div>
</form>
这里我用到了easyui,可以自行更改成一般的form表单,那么基本上就大功告成了
虽然代码拙劣,但是却付出了心血,如果有转载的童鞋,请附上转载信息
作者:Hunter_first
本作品由Hunter_first创作。 欢迎转载,但任何转载必须保留完整文章,在显要地方显示署名以及原文链接。如您有任何疑问,请给我留言