两级联动Select的TAG实现

JSP实现太复杂而且不方便,那来个幽雅点的Tag包装!
package com.jamesby.cm.common.utils;

import java.util.List;

import javax.servlet.jsp.tagext.Tag;
import javax.servlet.jsp.tagext.TagSupport;

public class TwoLevelDropdownTag extends TagSupport {
	public final String _PAGE_SCOPE = "page";
	public final String _REQUERT_SCOPE = "request";
	public final String _SESSION_SCOPE = "session";
	public final String _APPLICATION_SCOPE = "application";
	
	public final String _JS_GET_METHOD_NAME = "_do_get_second";

	public final String _JS_ONE_ARRAY_NAME = "_js_one_array_name";

	public final String _JS_TWO_ARRAY_NAME = "_js_two_array_name";

	public final String _STYLE_WIDTH = "100";

	public final String _PARAM_FORM_NAME = "cmForm";

	public final String _TWO_DEFAULT_TEXT = "";

	private String paramFormName = _PARAM_FORM_NAME;

	private String paramOneIdField;

	private String paramOneNameField;

	private String paramLinkField;

	private String paramTwoIdField;

	private String paramTwoNameField;
	
	private java.util.List paramTwoArrayList;
	
	private String paramTwoDefaultText = _TWO_DEFAULT_TEXT;

	private String paramStyleWidth = _STYLE_WIDTH;

	private String paramJsOneArrayName = _JS_ONE_ARRAY_NAME;

	private String paramJsTwoArrayName = _JS_TWO_ARRAY_NAME;

	private String paramJsGetMethodName = _JS_GET_METHOD_NAME;


	private String paramPreJavascript = "";

	private String paramPostJavascript = "";
	
	private String scope = _REQUERT_SCOPE;
	
	private String itemsName="items";

	public String getItemsName() {
		return itemsName;
	}

	public void setItemsName(String itemsName) {
		this.itemsName = itemsName;
	}
	public TwoLevelDropdownTag() {
	}

	public int doEndTag() {
		try{		
			if (!_PAGE_SCOPE.equals(scope)&&!_REQUERT_SCOPE.equals(scope)&&!_SESSION_SCOPE.equals(scope)&&!_APPLICATION_SCOPE.equals(scope))
			{
				throw new RuntimeException("scope is invalid.....................");
			}
			
			if (_PAGE_SCOPE.equals(scope))
			{
				paramTwoArrayList = (List)this.pageContext.getAttribute(itemsName);
			}
			if (_REQUERT_SCOPE.equals(scope))
			{
				paramTwoArrayList = (List)this.pageContext.getRequest().getAttribute(itemsName);
			}
			if (_SESSION_SCOPE.equals(scope))
			{
				paramTwoArrayList = (List)this.pageContext.getSession().getAttribute(itemsName);
			}
			if (_APPLICATION_SCOPE.equals(scope))
			{
				paramTwoArrayList = (List)this.pageContext.getServletContext().getAttribute("itemsName");
			}
			
			if (null==paramTwoArrayList) throw new RuntimeException("items not exists............");
			
			java.io.Writer out = this.pageContext.getOut();
			out.write("<script language=\"javascript\">\n");
			out.write("<!--\n");	
			out.write(getParamPreJavascript());	
			out.write("var "+getParamJsTwoArrayName()+" = new Array("+getParamTwoArrayList().size()+");\n");
			java.util.HashMap _local = null;
			for (int i=0;i<getParamTwoArrayList().size();i++)
			{
			  	_local =  (java.util.HashMap)getParamTwoArrayList().get(i);
			   	out.write(getParamJsTwoArrayName()+"["+i+"] = new Array(3);\n");
			   	out.write(getParamJsTwoArrayName()+"["+i+"][0] = '"+_local.get(getParamTwoIdField())+"';\n");
			   	out.write(getParamJsTwoArrayName()+"["+i+"][1] = '"+_local.get(getParamTwoNameField())+"';\n");
			   	out.write(getParamJsTwoArrayName()+"["+i+"][2] = '"+_local.get(getParamLinkField())+"';\n");		  			
			}
			out.write("function "+getParamJsGetMethodName()+"(){\n");
			out.write("firstid = "+getParamFormName()+"."+getParamOneIdField()+".options["+getParamFormName()+"."+getParamOneIdField()+".selectedIndex].value;\n");
			out.write("a = "+getParamFormName()+"."+getParamTwoIdField()+".length;\n");
			out.write("for (i=0;i<a;i++){\n");
			out.write(getParamFormName()+"."+getParamTwoIdField()+".remove(0);\n");
			out.write("}\n");
			out.write("op = new Option();\n");
			out.write("op.text=\""+getParamTwoDefaultText()+"\";\n");
			out.write("op.value=\"\";\n");
			out.write(getParamFormName()+"."+getParamTwoIdField()+".add(op);\n");
			out.write("if (firstid==0){\n");
			out.write("return;\n");
			out.write("}\n");
			out.write("for (i=0;i<"+getParamJsTwoArrayName()+".length;i++){\n");
			out.write("if ("+getParamJsTwoArrayName()+"[i][2]==firstid)\n");
			out.write("{\n");
			out.write("op1 = new Option();\n");
			out.write("op1.value = "+getParamJsTwoArrayName()+"[i][0];\n");
			out.write("op1.text = "+getParamJsTwoArrayName()+"[i][1];\n");
			out.write(getParamFormName()+"."+getParamTwoIdField()+".options.add(op1);\n");
			out.write("}\n");
			out.write("}\n");
			out.write(getParamFormName()+"."+getParamTwoIdField()+".style.width="+getParamStyleWidth()+";\n");
			out.write("}\n");	
			out.write(getParamPostJavascript());	
			out.write("//-->\n");
			out.write("</script>\n");	
			out.flush();
		}catch(java.io.IOException ex)
		{
			ex.printStackTrace();
		}		
		return Tag.EVAL_PAGE;
	}

	public String get_JS_GET_METHOD_NAME() {
		return _JS_GET_METHOD_NAME;
	}
	public String get_JS_ONE_ARRAY_NAME() {
		return _JS_ONE_ARRAY_NAME;
	}
	public String get_JS_TWO_ARRAY_NAME() {
		return _JS_TWO_ARRAY_NAME;
	}


	public String get_PARAM_FORM_NAME() {
		return _PARAM_FORM_NAME;
	}

	public String get_STYLE_WIDTH() {
		return _STYLE_WIDTH;
	}

	public String get_TWO_DEFAULT_TEXT() {
		return _TWO_DEFAULT_TEXT;
	}

	public String getParamFormName() {
		return paramFormName;
	}

	public void setParamFormName(String paraFormName) {
		this.paramFormName = paraFormName;
	}

	public String getParamJsGetMethodName() {
		return paramJsGetMethodName;
	}

	public void setParamJsGetMethodName(String paramJsGetMethodName) {
		this.paramJsGetMethodName = paramJsGetMethodName;
	}

	public String getParamJsOneArrayName() {
		return paramJsOneArrayName;
	}

	public void setParamJsOneArrayName(String paramJsOneArrayName) {
		this.paramJsOneArrayName = paramJsOneArrayName;
	}

	public String getParamJsTwoArrayName() {
		return paramJsTwoArrayName;
	}

	public void setParamJsTwoArrayName(String paramJsTwoArrayName) {
		this.paramJsTwoArrayName = paramJsTwoArrayName;
	}

	public String getParamLinkField() {
		return paramLinkField;
	}

	public void setParamLinkField(String paramLinkField) {
		this.paramLinkField = paramLinkField;
	}

	public String getParamOneIdField() {
		return paramOneIdField;
	}

	public void setParamOneIdField(String paramOneIdField) {
		this.paramOneIdField = paramOneIdField;
	}

	public String getParamOneNameField() {
		return paramOneNameField;
	}

	public void setParamOneNameField(String paramOneNameField) {
		this.paramOneNameField = paramOneNameField;
	}

	public String getParamPostJavascript() {
		return paramPostJavascript;
	}

	public void setParamPostJavascript(String paramPostJavascript) {
		this.paramPostJavascript = paramPostJavascript;
	}

	public String getParamPreJavascript() {
		return paramPreJavascript;
	}

	public void setParamPreJavascript(String paramPreJavascript) {
		this.paramPreJavascript = paramPreJavascript;
	}

	public String getParamStyleWidth() {
		return paramStyleWidth;
	}

	public void setParamStyleWidth(String paramStyleWidth) {
		this.paramStyleWidth = paramStyleWidth;
	}


	public String getParamTwoDefaultText() {
		return paramTwoDefaultText;
	}

	public void setParamTwoDefaultText(String paramTwoDefaultText) {
		this.paramTwoDefaultText = paramTwoDefaultText;
	}

	public String getParamTwoIdField() {
		return paramTwoIdField;
	}

	public void setParamTwoIdField(String paramTwoIdField) {
		this.paramTwoIdField = paramTwoIdField;
	}

	public String getParamTwoNameField() {
		return paramTwoNameField;
	}

	public void setParamTwoNameField(String paramTwoNameField) {
		this.paramTwoNameField = paramTwoNameField;
	}

	public String getScope() {
		return scope;
	}

	public void setScope(String scope) {
		this.scope = scope;
	}

	public java.util.List getParamTwoArrayList() {
		return paramTwoArrayList;
	}
}



<?xml version="1.0" encoding="iso-8859-1"?>
<!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>
  <tlibversion>1.0</tlibversion>
  <jspversion>1.1</jspversion>
  <shortname>display</shortname>
  <uri>http://jakarta.apache.org/taglibs/display</uri>
  <info>
         A description of the display tag library goes here...
  </info>
  <tag>
    <name>select2</name>
    <tagclass>com.jamesby.cm.common.utils.TwoLevelDropdownTag</tagclass>
    <bodycontent>EMPTY</bodycontent>
    <attribute>
      <name>paramOneIdField</name>
      <required>true</required>
      <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
      <name>paramOneNameField</name>
      <required>true</required>
      <rtexprvalue>true</rtexprvalue>
    </attribute> 
    <attribute>
      <name>paramLinkField</name>
      <required>true</required>
      <rtexprvalue>true</rtexprvalue>
    </attribute>  
    <attribute>
      <name>paramTwoIdField</name>
      <required>true</required>
      <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
      <name>paramTwoNameField</name>
      <required>true</required>
      <rtexprvalue>true</rtexprvalue>
    </attribute>               
    <attribute>
      <name>paramFormName</name>
      <required>false</required>
      <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
      <name>scope</name>
      <required>false</required>
      <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
      <name>itemsName</name>
      <required>false</required>
      <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
      <name>paramTwoDefaultText</name>
      <required>false</required>
      <rtexprvalue>true</rtexprvalue>
    </attribute>  
    <attribute>
      <name>paramStyleWidth</name>
      <required>false</required>
      <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
      <name>paramJsOneArrayName</name>
      <required>false</required>
      <rtexprvalue>true</rtexprvalue>
    </attribute> 
    <attribute>
      <name>paramJsTwoArrayName</name>
      <required>false</required>
      <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
      <name>paramJsGetMethodName</name>
      <required>false</required>
      <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
      <name>paramPreJavascript</name>
      <required>false</required>
      <rtexprvalue>true</rtexprvalue>
    </attribute>
    <attribute>
      <name>paramPostJavascript</name>
      <required>false</required>
      <rtexprvalue>true</rtexprvalue>
    </attribute>                                
  </tag> 
</taglib>


<%@ page contentType="text/html;charset=GBK" language="java"%>
<%@ taglib uri="/tags/taglib_xb" prefix="xb"%>
<%
	java.util.List paramTwoArrayList = new java.util.ArrayList();
	java.util.Map a1 = new java.util.HashMap();
	a1.put("compid","1");
	a1.put("deptid","10");	
	a1.put("deptname","行政部");
	
	java.util.Map b1 = new java.util.HashMap();
	b1.put("compid","1");
	b1.put("deptid","20");	
	b1.put("deptname","财务部");	
	
	java.util.Map c1 = new java.util.HashMap();
	c1.put("compid","2");
	c1.put("deptid","30");	
	c1.put("deptname","市场部");				
	
	paramTwoArrayList.add(a1);
	paramTwoArrayList.add(b1);
	paramTwoArrayList.add(c1);
	pageContext.setAttribute("items",paramTwoArrayList);
%>
<xb:select2 paramOneIdField="compid" paramOneNameField="compname" paramLinkField="compid" paramTwoIdField="deptid" paramTwoNameField="deptname" scope="page"/>
<form name="cmForm">
	公司名称<select name="compid" onchange="_do_get_second();" style="width:100">
		<option value=""></option>
		<option value="1">公司一</option>
		<option value="2">公司二</option>
		<option value="3">公司三</option>
	</select>
	部门名称
	<select name="deptid" style="width:100">
	</select>
</form>

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