简单jsp自定义标签及xml文件解析应用实例

xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<codeMaster>
	<!-- season -->
	<type name="seasonCode">
		<item code="1" value="Spring" />
		<item code="2" value="Summer" />
		<item code="3" value="Fall" />
	</type>

	<!-- month -->
	<type name="monthCode">
		<item code="01" value="january" />
		<item code="02" value="february" />
		<item code="03" value="march" />
		<item code="04" value="april" />
		<item code="05" value="may" />
		<item code="06" value="june" />
		<item code="07" value= "july"/>
		<item code="08" value="qugust" />
		<item code="09" value= "september"/>
		<item code="10" value="october" />
		<item code="11" value= "november"/>
		<item code="10" value="december" />
	</type>
</codeMaster>

jsp文件:

<%@ taglib uri="xxf-tag" prefix="xxf"%>



<xxf:codeValue type="monthCode">05</xxf:codeValue>

src:

目录:简单jsp自定义标签及xml文件解析应用实例_第1张图片

package dom4j.common.bean;

public class CodeItem {

	private String code;

	private String value;

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

}




package dom4j.common.bean;

import java.util.Vector;

public class CodeMaster {

	private Vector<CodeType> codeTypes = new Vector<CodeType>();

	public Vector<CodeType> getCodeTypes() {
		return codeTypes;
	}

	public void setCodeTypes(Vector<CodeType> codeTypes) {
		this.codeTypes = codeTypes;
	}

	public void addCodeType(CodeType codeType) {
		codeTypes.add(codeType);
	}
}




package dom4j.common.bean;

import java.util.Vector;

public class CodeType {

	private String name;

	private Vector<CodeItem> codeItems = new Vector<CodeItem>();

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Vector<CodeItem> getCodeItems() {
		return codeItems;
	}

	public void setCodeItems(Vector<CodeItem> codeItems) {
		this.codeItems = codeItems;
	}

	public void addCodeItem(CodeItem codeItem) {
		codeItems.add(codeItem);
	}

}


package dom4j.common;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import org.apache.commons.digester.Digester;
import org.apache.struts.util.LabelValueBean;

import dom4j.common.bean.CodeItem;
import dom4j.common.bean.CodeMaster;
import dom4j.common.bean.CodeType;

public class CodeMasterUtil {

	/** serialVersionUID */
	private static final long serialVersionUID = 8006834405602164630L;

	private static Map<String, List<LabelValueBean>> mapCode = new HashMap<String, List<LabelValueBean>>();

	static {

		Digester digester = new Digester();
		digester.addObjectCreate("codeMaster", CodeMaster.class);

		digester.addObjectCreate("codeMaster/type", CodeType.class);
		digester.addSetProperties("codeMaster/type");

		digester.addObjectCreate("codeMaster/type/item", CodeItem.class);
		digester.addSetProperties("codeMaster/type/item");

		digester.addSetNext("codeMaster/type/item", "addCodeItem");

		digester.addSetNext("codeMaster/type", "addCodeType");

		try {
			// 解析
			CodeMaster codeMaster = (CodeMaster) digester.parse(Thread
					.currentThread().getContextClassLoader().getResource(
							"codemaster.xml"));

			Vector<CodeType> codeTypes = codeMaster.getCodeTypes();

			List<LabelValueBean> listCode = new ArrayList<LabelValueBean>();

			for (int i = 0; i < codeTypes.size(); i++) {

				Vector<CodeItem> codeItem = codeTypes.get(i).getCodeItems();
				listCode = new ArrayList<LabelValueBean>();
				for (int j = 0; j < codeItem.size(); j++) {
					LabelValueBean codeValue = new LabelValueBean();
					codeValue.setLabel(codeItem.get(j).getValue());
					codeValue.setValue(codeItem.get(j).getCode());
					listCode.add(codeValue);
				}
				mapCode.put(codeTypes.get(i).getName(), listCode);
			}
		} catch (Exception e) {

		}

	}

	public static List<LabelValueBean> getCodeList(String type) {

		List<LabelValueBean> listCodeStatic = mapCode.get(type);
		List<LabelValueBean> listCode = new ArrayList<LabelValueBean>();
		for (LabelValueBean labelValueBean : listCodeStatic) {
			listCode.add(labelValueBean);
		}
		return listCode;
	}

	public static String getCode(String type, String code) {

		List<LabelValueBean> listCode = mapCode.get(type);
		String value = null;
		if (listCode != null) {
			for (LabelValueBean labelValueBean : listCode) {

				if (code.equals(labelValueBean.getValue())) {

					value = labelValueBean.getLabel();

					return value;
				}
			}
		}
		return value;
	}
}




package dom4j;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyContent;
import javax.servlet.jsp.tagext.BodyTagSupport;

import org.apache.struts.taglib.TagUtils;

import dom4j.common.CodeMasterUtil;

public class TagSupportDemo extends BodyTagSupport {

	/**
	 *
	 */
	private static final long serialVersionUID = 1L;
	private String type = null;

	@Override
	public int doEndTag() throws JspException {

		String codeValue = null;

		// code取得
		BodyContent codeBody = getBodyContent();
		String code = codeBody.getString();

		// 根据type和code ,取值
		if (type != null && code != null) {
			code = code.trim();
			type = type.trim();
			codeValue = CodeMasterUtil.getCode(type, code);
		}
		if (codeValue != null) {
			TagUtils.getInstance().write(pageContext, codeValue);

		} else {
			TagUtils.getInstance().write(pageContext, "");
		}

		return TagSupportDemo.EVAL_PAGE;
	}

	/**
	 * code
	 * @return type code
	 */
	public String getType() {
		return type;
	}

	/**
	 * code
	 * @param type code
	 */
	public void setType(String type) {
		this.type = type;
	}

}

web.xml   及 tld 配置

在web.xml中追加如下:

<jsp-config>
    	<taglib>
			<taglib-uri>xxf-tag</taglib-uri>
			<taglib-location>/WEB-INF/lib/xxf-code.tld</taglib-location>
		</taglib>
    </jsp-config>

xxf-code.tld文件如下:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE taglib
        PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
        "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
    <tlib-version>1.3</tlib-version>
    <jsp-version>1.2</jsp-version>
    <short-name>code</short-name>
    <uri>http://struts.apache.org/xxf-code</uri>
    <description>
        <![CDATA[


  ]]>
    </description>
   <tag>
        <name>codeValue</name>
        <tag-class>dom4j.TagSupportDemo</tag-class>
        <body-content>JSP</body-content>
        <description>
            <![CDATA[


    ]]>
        </description>
        <attribute>
            <name>type</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
            <description>
                <![CDATA[
      <p>Specifies the attribute name of the bean whose property is accessed
      to retrieve the value specified by <code>property</code> (if
      specified).  If <code>property</code> is not specified, the value of
      this bean itself will be rendered.</p>
      ]]>
            </description>
        </attribute>
    </tag>


</taglib>

以上 ,End!

 

 

你可能感兴趣的:(xml解析,tld配置)