已经搜索过论坛,没有找到答案,谢谢!
解决方案 »
- 每次修改代码都要重启JBoss.
- Strust2.0Hibernate3.2和spring2.5整合出错
- 如何把java.sql.date 转成java.util.date
- 怎么样在MyEclipse中引用Properites?
- 突然有点晕呀!!请教大家
- jboss更改jsp都需要重新启动服务器
- tomcat连接池问的人太多,做了个例子,谁要?
- (求救,给你们全部我的分)帮帮忙,什么问题,如何解决.编译ejb(StudentInfoHome.java)时出现,选种java2iiop
- 服务端代码如何兼容各个版本的客户端?
- java.lang.IllegalStateException: BeanFactory not initialized or already closed
- struts2 2..3.4.1 spring3.1.2 hibernate4.1.6中用注解dao无法注入service
- 字符串分割问题
package com.biristone.service.tree;import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.biristone.service.ProTypeService;
import com.biristone.pojo.ProType;public class Category {
private static String configFile = "applicationContext-services.xml";
private static ApplicationContext context = new ClassPathXmlApplicationContext(configFile);
private static Map<Long, Category> catMap = new HashMap<Long, Category>();
private static ProTypeService ps = (ProTypeService)context.getBean("proTypeService");
private static List<ProType> ptp;
static
{
BuildAllCategory(-1);
}
//根据产品分类构造树
public static void BuildAllCategory(int id)
{
ptp = ps.getTypeByPid(id);
List<Category> childs = new ArrayList<Category>();
//如果没有父节点,则为根节点
if(id==-1)
{
new Category(0,ptp.get(0).getName());
BuildAllCategory(ptp.get(0).getTid());
}
else
{
for(ProType node:ptp)
{
childs.add(new Category((long)node.getTid(),node.getName()));
}
//挂到相应的父节点
Category.getById((long)id).setChildren(childs);
//如果子节点还有子节点
for(Category child:childs)
{
if(null!=ps.getTypeByPid((int)child.getId()))
{
BuildAllCategory((int)child.getId());
}
}
}
}
public static Category getById(long id) {
return catMap.get(id);
} private Long id;
private String name;
private List<Category> children;
private boolean toggle; public Category(long id, String name, Category... children) {
this.id = id;
this.name = name;
this.children = new ArrayList<Category>();
for (Category child : children) {
this.children.add(child);
} catMap.put(id, this);
} public long getId() {
return id;
} public void setId(long id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public List<Category> getChildren() {
return children;
} public void setChildren(List<Category> children) {
this.children = children;
} public void toggle() {
toggle = !toggle;
} public boolean isToggle() {
return toggle;
}
}tp_type表:tid为分类,pid为上级分类,name为分类名
Category.getById(根节点编号)就返回整个分类树了关键部分都列出来了,里面的表结构映射的bean和action、Category转成json就不用我多说了吧。