JSP自定义标签无限极(数据来源于数据库)下拉联动

说明:这个工程的环境需要:JSP自定义标签、dwr,这个例子提供了几个实际应用的例子,包括了修改时候自动选中等。

首先看看web.xml的配置:

1、

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" 
	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-app_2_4.xsd">
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
  
  <servlet>
		<servlet-name>dwr-remote</servlet-name>
		<servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
		<init-param>
			<param-name>debug</param-name>
			<param-value>true</param-value>
		</init-param>
	</servlet>
	<servlet-mapping>
		<servlet-name>dwr-remote</servlet-name>
		<url-pattern>/dwr/*</url-pattern>
	</servlet-mapping>
  
  <jsp-config>
  	<taglib>
  		<taglib-uri>/tag</taglib-uri>
  		<taglib-location>/WEB-INF/ne.tld</taglib-location>
  	</taglib>
  </jsp-config>
</web-app>

 

2、dwr的配置文件dwr.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 3.0//EN" "dwr30.dtd">

<dwr>
	<allow>
		<create creator="new" javascript="LINK">
			<param name="class" value="com.core.LinkageAdapter"></param>
		</create>
	</allow>
</dwr>

 

3、tag.xml,jsp自定义标签的配置文件

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

<taglib>
	<tlib-version>1.0</tlib-version>
	<jsp-version>1.2</jsp-version>
	<uri>/tag</uri>
	
	<tag>
		<name>select</name>
		<body-content>empty</body-content>
		<info>下拉框标签</info>
		<tag-class>com.tag.link.SelectTag</tag-class>
		<attribute>
			<description>select框的name属性</description>
			<required>true</required>
			<rtexprvalue>true</rtexprvalue>
			<name>name</name>
		</attribute>
		<attribute>
			<description>select框的sql属性</description>
			<required>true</required>
			<rtexprvalue>true</rtexprvalue>
			<name>sql</name>
		</attribute>
		<attribute>
			<name>bind</name>
			<description>绑定下一级别的ID属性</description>
			<required>false</required>
			<rtexprvalue>true</rtexprvalue>
		</attribute>
		<attribute>
			<name>bindSelectValue</name>
			<description>绑定联动的下拉框的值,当联动的下拉框的值为当前的值的时候选中</description>
			<required>false</required>
			<rtexprvalue>true</rtexprvalue>
		</attribute>
		<attribute>
			<name>bindSql</name>
			<description>下一级别的查询SQL语句</description>
			<required>false</required>
			<rtexprvalue>true</rtexprvalue>
		</attribute>
	</tag>
	<tag>
		<name>select:link</name>
		<body-content>empty</body-content>
		<info>联动的子级标签</info>
		<tag-class>com.tag.link.LinkSelect</tag-class>
		<attribute>
			<description>select框的name属性</description>
			<required>true</required>
			<rtexprvalue>true</rtexprvalue>
			<name>name</name>
		</attribute>
		<attribute>
			<name>bind</name>
			<description>绑定下一级别的ID属性</description>
			<required>false</required>
			<rtexprvalue>true</rtexprvalue>
		</attribute>
		<attribute>
			<name>bindSql</name>
			<description>绑定下一级别的ID属性</description>
			<required>false</required>
			<rtexprvalue>true</rtexprvalue>
		</attribute>
		<attribute>
			<name>bindSelectValue</name>
			<description>选中该下拉框的属性值</description>
			<required>false</required>
			<rtexprvalue>true</rtexprvalue>
		</attribute>
	</tag>
</taglib>

 

4、有了上面的配置后,在jsp页面调用如下:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="tag" uri="/tag" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>下拉联动的测试</title>
    <style type="text/css">
    	*{
    		padding: 15px;
    	}
    </style>
	<meta http-equiv="pragma" content="no-cache" />
	<meta http-equiv="cache-control" content="no-cache" />
	<meta http-equiv="expires" content="0" />    
	
	<script type='text/javascript' src='<%=request.getContextPath() %>/dwr/interface/LINK.js'></script>
  	<script type='text/javascript' src='<%=request.getContextPath() %>/dwr/engine.js'></script>
  	<script type='text/javascript' src='<%=request.getContextPath() %>/dwr/util.js'></script>

  </head>
  
  <body>
    
    <fieldset>
    	<legend>省县乡级联</legend>
	    <tag:select name="province" sql="select povinceid,povincename from povince" bind="city" 
	    bindSql="select cityid,cityname from city where father = " />
	    
	    <tag:select:link name="city" bind="area"  
	    bindSql="select areaid,areaname from area where father = "/>
	    
	    <tag:select:link name="area" />
    </fieldset>
    
    <fieldset>
    	<legend>省县级联</legend>
    	<tag:select name="sheng" sql="select povinceid,povincename from povince" bind="xian" bindSql="select cityid,cityname from city where father = "/>
    	<tag:select:link name="xian" />
    </fieldset>
    
    <fieldset>
    	<legend>县乡级联</legend>
    	<tag:select name="xianxian" sql="select cityid,cityname from city " bind="xiangxiang" bindSql="select areaid,areaname from area where father = "/>
    	<tag:select:link name="xiangxiang" />
    </fieldset>
    
    <script type="text/javascript" src="<%=request.getContextPath() %>/js/select.js"></script>
  </body>
</html>

 

5、请注意查看标签调用的那部分,这个页面就实现了3组的联动,详细的就不说了,附上可以运行的工程。

详细也可以看

http://haiyangyiba.iteye.com/admin/blogs/650250

这里有详细的信息。

你可能感兴趣的:(JavaScript,sql,jsp,servlet,DWR)