在J2EE开发中不可避免会使用的树(Tree),一般情况有二种实现方式,第一种是初始化的时候将树信息就全部加载到内存中,这种方式适用于小数据或者性能要求不高的情况,优点是加载完后对服务器就不会有压力并且打开树节点速度快;第二种是数据异步加载即ajax技术,每次都加载少量的必须的数据,这种方式适用于大量数据和性能要求较高的情况;我们做为软件设计和开发者都希望软件性能最佳、用户体验最好;
目前网上介绍struts2 ajax tree的实例很多但就是不太全面、完整,就连struts官方网站都没有完整实例;下面我来介绍一下struts2 ajax tree的使用方法及技巧,这个实例全部引用自jdframe开发框架中的组织机构管理功能源码:
1. jsp页面
首先JSP要引用,其次在中包含< sx:head/>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ taglib prefix="sx" uri="/struts-dojo-tags"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
JDFrame System Organization Tree
|
|
2. Struts2配置文件
/sys/core/frame/treeAjaxDynamic.ftl ... /sys/core/org/tree.jsp
3. java代码
AjaxDynamicTreeAction.java
package com.jdframe.sys.core.model.tree;
import com.jdframe.sys.core.action.JdframeAction;
import com.jdframe.sys.core.model.tree.Category;
// TODO: Auto-generated Javadoc
/**
* The Path : com.jdframe.sys.core.model.tree.AjaxDynamicTreeAction.java
* The AjaxDynamicTreeAction
* Last-Modified-Time : 2014-2-20 10:44:11
*
* @author support@jdframe.com
* @version 2.0.3.0
* http://www.jdframe.com
* @see
*/
public abstract class AjaxDynamicTreeAction extends JdframeAction {
//default value
/** The node id. */
protected String nodeId = null;
//for ftl
/** The category. */
public Category category;
/* (非 Javadoc)
* Title: perform
* Description:
* @return
* @see com.jdframe.sys.core.action.JdframeAction#perform()
*/
/* (non-Javadoc)
* @see com.jdframe.sys.core.action.JdframeAction#perform()
*/
@Override
protected abstract String perform();
/**
* Select tree node.
*
* @return the string
*/
public abstract String selectTreeNode() ;
/* (非 Javadoc)
* Title: validators
* Description:
* @see com.jdframe.sys.core.action.JdframeAction#validators()
*/
/* (non-Javadoc)
* @see com.jdframe.sys.core.action.JdframeAction#validators()
*/
@Override
protected abstract void validators() ;
/* (非 Javadoc)
* Title: initial
* Description:
* @return
* @see com.jdframe.sys.core.action.JdframeAction#initial()
*/
/* (non-Javadoc)
* @see com.jdframe.sys.core.action.JdframeAction#initial()
*/
@Override
protected abstract String initial() ;
/**
* Gets the category.
*
* @return the category
*/
public Category getCategory() {
return category;
}
/**
* Sets the category.
*
* @param category the new category
*/
public void setCategory(Category category) {
this.category = category;
}
/**
* Gets the node id.
*
* @return the node id
*/
public String getNodeId() {
return nodeId;
}
/**
* Sets the node id.
*
* @param nodeId the new node id
*/
public void setNodeId(String nodeId) {
this.