系统采用Struts2+Spring+Hibernate架构,classpath分层结构如下图:
表结构如下图:
表映射为pojo为类Tree.java代码如下:
private Integer id;
private String name;
private String children;
private Tree parent;
public Tree() {
}
public Tree(Integer id, String name, String children) {
this.id = id;
this.name = name;
this.children=children;
}
public Tree(Integer id, String name, String children, Tree parent) {
this.id = id;
this.name = name;
this.children = children;
this.parent = parent;
}
//省略get,set }
Hibernate映射文件tree.hbm.xml
<hibernate-mapping>
<class name="zoboya.Tree" table="tree" >
<id name="id" type="java.lang.Integer">
<generator class="native" />
</id>
<property name="name" type="string">
<column name="name" length="255" />
</property>
<property name="children" type="string">
<column name="children" length="255" />
</property>
<many-to-one name="parent" class="zoboya.Tree" fetch="select">
<column name="parentid" not-null="true" />
</many-to-one>
</class>
</hibernate-mapping>
TreeDao.java代码如下:
public class TreeDAO extends HibernateDaoSupport implements ITreeDAO {
private static final Log log = LogFactory.getLog(TreeDAO.class);
public Tree findById(java.lang.Integer id) {
log.debug("getting Tree instance with id: " + id);
try {
Tree instance = (Tree) getSession().get("zoboya.Tree", id);
return instance;
} catch (RuntimeException re) {
log.error("get failed", re);
throw re;
}
}
//其它方法省略
}
节点类,Category
public class Category
{
//保存了所有节点数据的Map
private static Map<Long, Category> catMap = new HashMap<Long, Category>();
//用作节点的id
private long id;
//用作节点的标题
private String name;
//包含子节点对象的列表
private List<Category> children;
public static Category getById(long id)
{
return catMap.get(id);
}
public Category(long id, String name,List<Category> children){
this.id=id;
this.name=name;
this.children=children;
catMap.put(id, this);
}
public Category(long id, String name, Category... children)
{
this.id = id;
this.name = name;
//初始化Category对象的children属性,并放入子对象
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;
}
}
Service层的类TreeService
public class TreeService implements ITreeService {
private ITreeDAO treeDao;
public ITreeDAO getTreeDao() {
return treeDao;
}
public void setTreeDao(ITreeDAO treeDao) {
this.treeDao = treeDao;
}
//递归方法
public Category createTree(Tree tree){
System.out.println("Name= "+tree.getName());
//如果有儿子就循环调用自己去找,把找到的所有儿子生成一个list作为children加入到父中
List<Category> children=new ArrayList<Category>();
if (tree.getChildren()!=null) {
String[] childrenId=tree.getChildren().split(",");
for (int i = 0; i < childrenId.length; i++) {
//递归
Tree branch=treeDao.findById(Integer.parseInt(childrenId[i]));
Category child = createTree(branch);
children.add(child);
}
}
Category me=new Category(tree.getId(),tree.getName(), children);
return me;
}
}
Action
public class ShowDynamicTreeAction extends ActionSupport
{
private static final long serialVersionUID = 7437613038859411044L;
private ITreeService treeService;
private Category category;
public Category getCategory() {
return category;
}
public void setCategory(Category category) {
this.category = category;
}
@Override
public String execute() throws Exception {
this.category =this.getTreeRootNode();
return SUCCESS;
}
public void setTreeService(ITreeService treeService) {
this.treeService = treeService;
}
public Category getTreeRootNode()
{ Tree tree=new Tree(0,"ZH根目录","1,5"); //选择构造一个根目录,其子目录用逗号隔开!
System.out.println("开始递归!");
return treeService.createTree(tree);
}
}
在applicationContext.xml文件中加入
<!-- DAO -->
<bean id="treeDao" class="zoboya.struts2.dao.TreeDAO">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>
<!-- Services -->
<bean id="treeService" class="zoboya.struts2.service.TreeService">
<property name="treeDao">
<ref bean="treeDao"/>
</property>
</bean>
<!-- Action -->
<bean id="treeAction" class="zoboya.struts2.action.ShowDynamicTreeAction" singleton="false">
<property name="treeService">
<ref bean="treeService"/>
</property>
</bean>
在struts.xml中加入
<action name="showDynamicTreeAction"
class="treeAction">
<result>/ajaxTags/treeTag/treeExampleDynamic.jsp</result>
</action>
部署后访问以下地址就可以看到动态树了
http://localhost:8080/项目名/ajaxTags/treeTag/showDynamicTreeAction.action
分享到:
相关推荐
struts2-showcase.rar struts2自带的源码例子
struts2-showcase-2.1.8.1.warstruts2-showcase-2.1.8.1.warstruts2-showcase-2.1.8.1.warstruts2-showcase-2.1.8.1.warstruts2-showcase-2.1.8.1.war
struts2-showcase.war
struts2的jquery插件演示,其中还使用了FHP(Full Hibernate Plug)
自己的总结 还很不全面希望对你您有用 主要是控制url
struts2 的showcase,可直接导入到myeclise,struts2自带的是maven的,
要求配置条件比较苛刻,同时,一些特定版本没有看到有沙盒绕过,所以,目前exp只是基于S2-045改写的,所以exp并不是所有版本都能用,正常情况下Struts 2.3.5-2.3.31,Struts 2.5-2.5.10版本可以使用此exp。...
struts2 最新版 不用去官网上下载了,压缩包里有我的邮箱,如果资源有问题,请发邮件我,我会将这个资源通过邮箱发给你。
struts2.0自带,不错的例子哦.
增加S2-048 Struts 2.3.X 支持检查官方示例struts2-showcase应用的代码执行漏洞,参考地址:http://127.0.0.1:8080/struts2-showcase/integration/saveGangster.action
struts2.0官方项目之四(showcase) <br>=================================================== Struts2.0官方项目共4个,名字如下: <br>blank mailreader portlet showcase <br>这4个官方...
juery集成到struts2。这是其中的一个例子的源代码,好处在于你不仅看到了程序演示的效果,还可以在页面上看到实现相关功能的代码。
struts和bootstrap的标签库
要求配置条件比较苛刻,同时,一些特定版本没有看到有沙盒绕过,说以,目前exp只是基于S2-045改写的,所以exp并不是所有版本都能用,正常情况下Struts 2.3.5-2.3.31,Struts 2.5-2.5.10版本可以使用此exp。...
Struts2漏洞检查工具2019版 警告: 本工具为漏洞自查工具,请勿非法攻击他人网站! ==漏洞编号==============影响版本=========================官方公告==========================================影响范围====...
文件包含struts-2.5.16-源码+示例,利用附件文件可以很方便的搭建出调试S2-057的漏洞环境,欢迎各位取用
要求配置条件比较苛刻,同时,一些特定版本没有看到有沙盒绕过,说以,目前exp只是基于S2-045改写的,所以exp并不是所有版本都能用,正常情况下Struts 2.3.5-2.3.31,Struts 2.5-2.5.10版本可以使用此exp。...
官方app应用示例,包含基本action,下载后import工程,参照lib目录下图片中包含的jar包导入部署即可运行。
增加S2-048 Struts 2.3.X,支持检查官方示例struts2-showcase应用的代码执行漏洞。增加安恒信息研究员nike.zheng发现的S2-045。jakatar处理复杂数据类型时,异常处理不当,导致OGNL代码执行,通过在请求的Content-...
richfaces-distribution-4.5.8.Final的richfaces-showcase的部署包