关于java自定义标签的写法(下拉列表两级联动)

java代码:

package com.newsicom.common.web.tag;

import java.util.Map;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
import com.newsicom.common.constants.Constants;
import com.newsicom.common.domain.PdEmployeeInfoV;
import com.newsicom.common.domain.PdRegionMaster;
import com.newsicom.common.util.Util;
import com.newsicom.common.web.global.model.impl.RegionMasterMapImpl;

public class CompanySelectTag extends TagSupport {

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

	private String style;

	private String id;

	private boolean hasSaleFlg;

	/**
	 * 获取 hasSaleFlg
	 * 
	 * @return hasSaleFlg
	 */
	public boolean isHasSaleFlg() {
		return hasSaleFlg;
	}

	/**
	 * 设置 hasSaleFlg
	 * 
	 * @param hasSaleFlg
	 *            : hasSaleFlg
	 */
	public void setHasSaleFlg(boolean hasSaleFlg) {
		this.hasSaleFlg = hasSaleFlg;
	}

	public String getCssClass() {
		return cssClass;
	}

	public void setCssClass(String cssClass) {
		this.cssClass = cssClass;
	}

	public String getStyle() {
		return style;
	}

	public void setStyle(String style) {
		this.style = style;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	private String name;

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name
	 *            the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	@Override
	public int doStartTag() throws JspException {

		try {
			PdEmployeeInfoV employeeInfoV = (PdEmployeeInfoV) pageContext
					.getSession()
					.getAttribute(Constants.USER_INFO_HTTP_SESSION);
			StringBuilder sb = new StringBuilder("");
			String orgFlg = employeeInfoV.getOrganizationClassify();
			boolean disabled = false;
			if (orgFlg.equals("02")) {
				// 只锁销售
				if (hasSaleFlg
						&& (employeeInfoV.getFuncIds().indexOf("1") >= 0 || employeeInfoV
								.getFuncIds().indexOf("3") >= 0)) {
					disabled = true;
				} else if (!hasSaleFlg
						&& (employeeInfoV.getFuncIds().indexOf("2") >= 0 || employeeInfoV
								.getFuncIds().indexOf("4") >= 0)) {
					disabled = true;
				}
			}
			sb.append("<select ");
			// 设置ID
			sb.append(id != null ? "id=\"" + id + "\" "
					: Constants.EMPTY_STRING);
			// 设置css
			sb.append(cssClass != null ? "class=\"" + cssClass + "\" "
					: Constants.EMPTY_STRING);
			// 设置名字
			sb.append(name != null ? "name=\"" + name + "\" "
					: Constants.EMPTY_STRING);
			// 设置style
			sb.append(style != null ? "style=\"" + style + "\" "
					: Constants.EMPTY_STRING);
			// 设置是否锁死
			sb.append(disabled ? " disabled " : Constants.EMPTY_STRING);

			sb.append(">\n");
			if (orgFlg.equals("03")) {
				if ((hasSaleFlg && (employeeInfoV.getFuncIds().indexOf("1") >= 0 || employeeInfoV
						.getFuncIds().indexOf("3") >= 0))
						|| (!hasSaleFlg && (employeeInfoV.getFuncIds().indexOf(
								"2") >= 0 || employeeInfoV.getFuncIds()
								.indexOf("4") >= 0))) {
					Map<Integer, PdRegionMaster> map = RegionMasterMapImpl
							.getInstance().getCompanyMap(
									employeeInfoV.getOrganizationId());
					sb.append("<option value=\"\" ></option>\n");
					for (Map.Entry<Integer, PdRegionMaster> entry : map
							.entrySet()) {
						sb.append("<option value=\""
								+ entry.getValue().getSubCompanyId() + "\" >"
								+ entry.getValue().getSubCompanyName()
								+ "</option>\n");
					}
				}
			} else if (orgFlg.equals("02")) {
				if (disabled) {
					sb.append("<option value=\""
							+ employeeInfoV.getOrganizationId()
							+ "\" >"
							+ RegionMasterMapImpl
									.getInstance()
									.getCompany(
											employeeInfoV
													.getUpperOrganizationId(),
											employeeInfoV.getOrganizationId())
									.getSubCompanyName() + "</option>\n");
				}
			}

			sb.append("</select>");

			this.pageContext.getOut().write(sb.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
		return TagSupport.EVAL_PAGE;
	}
}

 tld代码:

<?xml version="1.0" encoding="UTF-8" ?>
<taglib xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
	version="2.0">

	<description>大区选择列表框</description>
	<tlib-version>1.0</tlib-version>
	<short-name>nsctags-common-region-subcompany</short-name>
	<uri>/nsctags-common-region-subcompany</uri>

	<tag>
		<name>RegionSelect</name>
		<tag-class>com.newsicom.common.web.tag.RegionSelectTag</tag-class>
		<body-content>empty</body-content>

		<attribute>
			<name>id</name>
			<required>true</required>
			<rtexprvalue>false</rtexprvalue>
			<type>java.lang.String</type>
		</attribute>
		
		<attribute>
			<name>name</name>
			<required>true</required>
			<rtexprvalue>false</rtexprvalue>
			<type>java.lang.String</type>
		</attribute>
		
		<attribute>
			<name>subcompanySelectId</name>
			<required>true</required>
			<rtexprvalue>false</rtexprvalue>
			<type>java.lang.String</type>
		</attribute>
		
		<attribute>
			<name>cssClass</name>
			<required>false</required>
			<rtexprvalue>false</rtexprvalue>
			<type>java.lang.String</type>
		</attribute>
		
		<attribute>
			<name>style</name>
			<required>false</required>
			<rtexprvalue>false</rtexprvalue>
			<type>java.lang.String</type>
		</attribute>
		<attribute>
			<name>hasSaleFlg</name>
			<required>true</required>
			<rtexprvalue>false</rtexprvalue>
			<type>java.lang.Boolean</type>
		</attribute>
	</tag>
	
	<tag>
		<name>CompanySelect</name>
		<tag-class>com.newsicom.common.web.tag.CompanySelectTag</tag-class>
		<body-content>empty</body-content>

		<attribute>
			<name>id</name>
			<required>true</required>
			<rtexprvalue>false</rtexprvalue>
			<type>java.lang.String</type>
		</attribute>
		
		<attribute>
			<name>hasSaleFlg</name>
			<required>true</required>
			<rtexprvalue>false</rtexprvalue>
			<type>java.lang.Boolean</type>
		</attribute>
		
		<attribute>
			<name>name</name>
			<required>false</required>
			<rtexprvalue>false</rtexprvalue>
			<type>java.lang.String</type>
		</attribute>
		
		<attribute>
			<name>cssClass</name>
			<required>false</required>
			<rtexprvalue>false</rtexprvalue>
			<type>java.lang.String</type>
		</attribute>
		
		<attribute>
			<name>style</name>
			<required>false</required>
			<rtexprvalue>false</rtexprvalue>
			<type>java.lang.String</type>
		</attribute>
		
	</tag>
</taglib>

  HTML调用:

 

 
<%@taglib prefix="nsc" uri="/nsctags-common-region-subcompany"%>
<td class=headerlabel width=94 align=right> 安装大区 </td>
            <td width=80 align=left>
              <nsc:RegionSelect hasSaleFlg="false" subcompanySelectId="installSubcompanyId" id="installRegionId" name="installRegionId" style="width: 100px" />
            </td>
            <td class=headerlabel width=92 align=right> 安装分公司 </td>
            <td class=headerlabel width=110 align=left>
            <nsc:CompanySelect hasSaleFlg="false" id="installSubcompanyId" name="installSubcompanyId" style="width: 110px"/>	
          
 

 

你可能感兴趣的:(自定义标签)