自定义标签+反射

说明:为了使用dhtmlx的Tree控件,动态生成所需的xml文件,通过一个父子关系的节点拼出来。
引用
只是为了参考

tag 标签(XMLForTreeTag.java)
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
import org.apache.commons.lang.StringUtils;

/**
 *
 * @author gby
 */
public class XMLForTreeTag extends TagSupport {

    private Object rootNode;
    private String nodeIdProperty;
    private String nodeTypeProperty;
    private String nodeTitleProperty;
    private String childCollectionProperty;
   
    /**
     *
     * @return int
     * @throws JspException
     */
    @Override
    public int doStartTag() throws JspException {
        StringBuffer sb = new StringBuffer();
        try {
            sb = toHTML(rootNode,
                    nodeIdProperty,
                    nodeTitleProperty,
                    nodeTypeProperty,
                    childCollectionProperty);
        } catch (Exception ex) {
            Logger.getLogger(XMLForTreeTag.class.getName()).log(Level.SEVERE, null, ex);
        }
        JspWriter out = pageContext.getOut();
        try {
            out.println(sb.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }

        return SKIP_BODY;
    }

    /**
     * <tree id="0" >
        <item text="name1" id="id1">
           <item text="name1-1" id="id1-1">
           </item>
        </item>
        </tree>
     * @param rootNode
     * @param nodeIdProperty
     * @param nodeTitleProperty
     * @param nodeTypeProperty
     * @param childCollectionProperty
     * @return
     * @throws Exception
     */
    public StringBuffer toHTML(Object rootNode,
            String nodeIdProperty,
            String nodeTitleProperty,
            String nodeTypeProperty,
            String childCollectionProperty) throws Exception {
        
        StringBuffer sb = new StringBuffer();
        sb.append("<tree id=\"0\">\n");
        String nodes = this.createXML(rootNode,
                nodeIdProperty,
                nodeTitleProperty,
                nodeTypeProperty,
                childCollectionProperty,
                sb);
        sb.append("</tree>\n");

        return sb;
    }

    private String createXML(Object node,
            String nodeIdProperty,
            String nodeTitleProperty,
            String nodeTypeProperty,
            String childCollectionProperty,
            StringBuffer sb) throws Exception {
        Class<?> clazz = node.getClass();
        //获得属性的get方法名。
        
        String nodeIdMethodName = this.createMethodName(nodeIdProperty);
        String nodeTitleMethodName = this.createMethodName(nodeTitleProperty);
        String nodeTypeMethodName = this.createMethodName(nodeTypeProperty);
        String childCollectionMethodName = this.createMethodName(childCollectionProperty);

        //根据反射获得method
        Method nodeIdMethod = clazz.getMethod(nodeIdMethodName, new Class[0]);
        Method nodeTitleMethod = clazz.getMethod(nodeTitleMethodName, new Class[0]);
        Method nodeTypeMethod = clazz.getMethod(nodeTypeMethodName, new Class[0]);
        Method childCollectionMethod = clazz.getMethod(childCollectionMethodName, new Class[0]);

        if (node != null) {
            String nodeId = (String) nodeIdMethod.invoke(node, new Object[0]);
            String nodeTitle = (String) nodeTitleMethod.invoke(node, new Object[0]);
            String nodeType = (String) nodeTypeMethod.invoke(node, new Object[0]);

            String itemId = nodeId + "__" + nodeType;
            
            sb.append("<item text=\"").append(nodeTitle).append("\" ")
                    .append(" id=\"").append(itemId).append("\">");

            List children = (List) childCollectionMethod.invoke(node, new Object[0]);
            if (children != null && !children.isEmpty()) {
                for (Object child : children) {
                    this.createXML(child, nodeIdProperty, nodeTitleProperty, nodeTypeProperty, childCollectionProperty, sb);
                }
            }
            sb.append("</item>\n");
        }

        return sb.toString();
    }

    private String createMethodName(String property) {
        if (StringUtils.isEmpty(property)) {
            return null;
        }
        String method = "get" + property.substring(0, 1).toUpperCase() + property.substring(1);
        return method;
    }
    
    getters().. setters()..
}

tld文件(TldName.tld)
<?xml version="1.0" encoding="UTF-8"?>

<!-- edited with XML Spy v4.3 U (http://www.xmlspy.com) by LEGO (LEGO) -->

<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN" "http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">
<taglib>
    <tag>
        <name>xmlForTree</name>
        <tagclass>XMLForTreeTag</tagclass>
        <info>为显示多选树形拼的XML-dhtmlx</info>
        <attribute>
            <name>rootNode</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>nodeIdProperty</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>nodeTypeProperty</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>nodeTitleProperty</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
            <name>childCollectionProperty</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
    </tag>
</taglib>

节点类(NodeDTO.java)
import java.io.Serializable;
import java.util.List;

/**
 * 树形显示DTO
 * @author gby
 */
public class NodeDTO implements Serializable{
    /**
     * node的唯一标识
     */
    private String id;
    /**
     * node的唯一标识
     */
    private String parentId;
    /**
     * node的名称
     */
    private String name;
    /**
     * node的父节点
     */
    private NodeDTO parentDTO;
    /**
     * node的孩子节点
     */
    private List<NodeDTO> childrenDTO;
    //节点类型
    private String nodeType;
    getters().. setters()..
}

你可能感兴趣的:(java,jsp,xml,servlet,sun)