Pure JSP+DWR to achieve three level linkage drop down menu implementation skills

  • 2020-06-01 10:47:38
  • OfStack

I saw some examples on the Internet, for a simple 3 level linkage, all add what Struts, Hibernate and so on framework. This Ajax linkage doesn't really have anything to do with these frames, so why does a small Demo make such a big one?

Today I did an example of dwr+jsp.
web.xml:
 
<?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"> 
<servlet> 
<servlet-name>dwr-invoker</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> 
<servlet-name>SelectServlet</servlet-name> 
<servlet-class>com.action.SelectServlet</servlet-class> 
</servlet> 
<servlet-mapping> 
<servlet-name>dwr-invoker</servlet-name> 
<url-pattern>/dwr/*</url-pattern> 
</servlet-mapping> 
<servlet-mapping> 
<servlet-name>SelectServlet</servlet-name> 
<url-pattern>/select</url-pattern> 
</servlet-mapping> 
<welcome-file-list> 
<welcome-file>index.jsp</welcome-file> 
</welcome-file-list> 
</web-app> 

dwr.xml:
 
<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 
2.0//EN" "http://www.getahead.ltd.uk/dwr/dwr20.dtd"> 
<dwr> 
<!--  Without it DWR Nothing can be done  --> 
<allow> 
<create creator="new" javascript="menu"> 
<param name="class" value="com.dao.CountryDAO" /> 
</create> 
<!--  To convert Bean --> 
<convert converter="bean" match="com.vo.Country" /> 
<convert converter="bean" match="com.vo.Province" /> 
<convert converter="bean" match="com.vo.City" /> 
</allow> 
</dwr> 

test.jsp:
 
<%@ page language="java" import="java.util.*,com.vo.*" 
pageEncoding="GBK"%> 
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c"%> 
<html> 
<head> 
<title>DWR3 Levels of linkage </title> 
<script type='text/javascript' 
src='/mutiplyMenu/dwr/interface/menu.js'></script> 
<script type='text/javascript' src='/mutiplyMenu/dwr/engine.js'></script> 
<script type='text/javascript' src='/mutiplyMenu/dwr/util.js'></script> 
</head> 
<body> 
<script type="text/javascript"> 
// According to the national id Inquire province or state  
function queryProvince() 
{ 
var countryId = $("country").value; 
// The default is not to select  
if(countryId == 0) 
{ 
${"province"}.options.length=0; 
${"city"}.options.length=0; 
} 
else 
{ 
menu.queryProvinceById(countryId,provinceCallback); 
} 
} 
// According to the national id Query the callback function for the province or state  
function provinceCallback(provinces) 
{ 
${"province"}.options.length=0; 
// Every time you get a new piece of data 2 The length of the drop-down frame is clear 0 
for(var i=0;i< provinces.length;i ++){ 
var value = provinces[i].id; 
var text = provinces[i].provinceName; 
var option = new Option(text, value); 
// According to each group value and text The value of the tag is created 1 a option object  
try{ 
$("province").add(option);// will option Object to add to the first 2 In a drop-down box  
}catch(e){ 
} 
} 
// Simultaneous correlation control 3 level  
var provinceId = ${"province"}.value; 
menu.queryCityById(provinceId,cityCallback); 
} 
// Query the city  
function queryCity() 
{ 
var provinceId = $("province").value; 
menu.queryCityById(provinceId,cityCallback); 
} 
// Query the host city callback function  
function cityCallback(citys) 
{ 
// Every time you get a new piece of data 3 The length of the drop-down frame is clear 0 
${"city"}.options.length=0; 
for(var i=0;i< citys.length;i ++){ 
var value = citys[i].id; 
var text = citys[i].cityName; 
var option = new Option(text, value); 
// According to each group value and text The value of the tag is created 1 a option object  
try{ 
$("city").add(option);// will option Object to add to the first 3 In a drop-down box  
}catch(e){ 
} 
} 
} 
function change1() 
{ 
queryProvince(); 
} 
function change2() 
{ 
queryCity(); 
} 
</script> 
<div align="center"> 
<h3> 
<br> 
</h3> 
<h3> 
DWR3 Stage linkage demonstration  
</h3> 
<!--  I'm surprised. Mine <c> The label is not available here  --> 
<select id="country" onchange="change1();"> 
<option selected="selected" value="0"> 
 Please select a  
</option> 
<% 
@SuppressWarnings("unchecked") 
List list = (List) request.getAttribute("countrys"); 
for (int i = 0; i < list.size(); i++) 
{ 
Country temp = (Country) list.get(i); 
%> 
<option value="<%=temp.getId()%>"><%=temp.getCountryName()%></option> 
<% 
} 
%> 
</select> 
<select id="province" onchange="change2();"> 
</select> 
<select id="city"> 
</select> 
</div> 
</body> 
</html> 

servlet,dao will not post, want to know can go to download the source code

Related articles: