两级联动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("\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;
	}
}






  1.0
  1.1
  display
  http://jakarta.apache.org/taglibs/display
  
         A description of the display tag library goes here...
  
  
    select2
    com.jamesby.cm.common.utils.TwoLevelDropdownTag
    EMPTY
    
      paramOneIdField
      true
      true
    
    
      paramOneNameField
      true
      true
     
    
      paramLinkField
      true
      true
      
    
      paramTwoIdField
      true
      true
    
    
      paramTwoNameField
      true
      true
                   
    
      paramFormName
      false
      true
    
    
      scope
      false
      true
    
    
      itemsName
      false
      true
    
    
      paramTwoDefaultText
      false
      true
      
    
      paramStyleWidth
      false
      true
    
    
      paramJsOneArrayName
      false
      true
     
    
      paramJsTwoArrayName
      false
      true
    
    
      paramJsGetMethodName
      false
      true
    
    
      paramPreJavascript
      false
      true
    
    
      paramPostJavascript
      false
      true
                                    
   


<%@ 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);
%>

公司名称 部门名称

你可能感兴趣的:(Java,JSP,Servlet,SUN,Apache)