|
所谓的三级连动就是类似于有三个下拉框,当你在第一个下拉框中选择省份时,第二个下拉框中显示相应的市名,当在第二个下拉框中选择市时在地三个下拉框中显示区县。。这样的效果我们团队称之为三级连动。。他的struts实现是我们的师哥写出来的,本人个人觉得比较值得借鉴,所以写出来,供大家评价。。。。 本文就以省-市-县 的三级为例,说明在struts中如何实现。。 (1)、相关数据库结构,Action 、ActionForm、javaBean、jsp文件说明。 数据库结构如下: province_view 表
provinceId 区号 provinceName 省名
210000 江苏省 ----------------------------------------- city_view
cityId cityName provinceId
212000 镇江 210000
--------------------------------------------- area_view
areaId areaName cityId
212010 京口区 212020
--------------------------------------------- struts 框架说明: (not used ActinForm)
Action GetCAAction.java //用来实现主要的内部逻辑,产生javaBean组
Jsp placeFind.jsp //效果显示页面
LabelValueBean LabelValueBean.java //用到的javaBean
/* org.apache.struts.util.LabelValueBean (can be used for <html:options />)
Some ArrayLists of Beans were transfered between the Action and Jsp files. */
(2)
Action GetCAAction.java 描述:
---
//Created by MyEclipse Struts // XSL source (default): platform:/plugin/com.genuitec.eclipse.cross.easystruts.eclipse_4.0.0/xs lt/JavaClass.xsl
package com.teyunwang.action.line; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import org.apache.struts.action.Action; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.util.LabelValueBean; import com.teyunwang.DataBase; public class GetCAAction extends Action { //这个action相当之复杂,只因我对标签十分之不熟,无奈之举 public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { DataBase db = new DataBase(); Connection conn = db.getConnection(); String sprovince = request.getParameter("provinceID"); String scity = request.getParameter("cityID"); String sarea=request.getParameter("areaID"); String table;String where; HttpSession session = request.getSession(); //省份下拉框变化,作出的动作 if (sprovince != null) { table = "province_view"; where = "provinceID='" + sprovince + "'"; ResultSet selectedProvinceName = db.getResultSet(conn, table, where); ResultSet rs = db.getResultSet(conn, table); try { ArrayList province = new ArrayList(); while (selectedProvinceName.next()) province.add(new LabelValueBean(selectedProvinceName.getString(2), sprovince)); province.add(new LabelValueBean("请选择所在省/直辖市",000000")); while (rs.next()) { province.add(new LabelValueBean(rs.getString(2), rs.getString(1))); } session.setAttribute("province",province); } catch (SQLException e) { // TODO Auto- generated catch block e.printStackTrace(); } table = "city_view"; where = "provinceID='" + sprovince + "'"; ArrayList city=new ArrayList(); city.add(new LabelValueBean("请选择所在地级市","000000")); ResultSet rs2=db.getResultSet(conn,table,where); try { while(rs2.next()){ city.add(new LabelValueBean(rs2.getString(2),rs2.getString(1))); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } session.setAttribute("city",city); ArrayList area=new ArrayList(); area.add(new LabelValueBean("请选择所在地点", "000000")); session.setAttribute("area",area); } //当地点下拉框作出变化时 if(sarea!=null){table="area_view"; where="areaID='"+sarea+"'"; ResultSet selectedAreaName=db.getResultSet(conn,table,where); ArrayList area=new ArrayList(); String cityID="000000"; try { while(selectedAreaName.next()){ cityID=selectedAreaName.getString(3); area.add(new LabelValueBean(selectedAreaName.getString(2),sarea)); } } catch (SQLException e) { e.printStackTrace(); } area.add(new LabelValueBean("请选择所在地点","000000")); where="cityID='"+cityID+"'"; ResultSet rs=db.getResultSet(conn,table,where); try { while(rs.next()){ area.add(new LabelValueBean(rs.getString(2),rs.getString(1))); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } session.setAttribute("area",area); } //当地级市下拉框作出变化作出的变化 if (scity != null) { table="city_view"; where="cityID='"+scity+"'"; ResultSet selectedCityName=db.getResultSet(conn,table,where); //System.out.println(selectedCityName.toString()); String ProvinceID="000000” ArrayList city=new ArrayList(); try { while(selectedCityName.next()){ ProvinceID=selectedCityName.getString(3); city.add(new LabelValueBean(selectedCityName.getString(2),scity)); //System.out.println(selectedCityName.getString(2)); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } city.add(new LabelValueBean("请选择所在地级市","000000")); where="provinceID='"+ProvinceID+"'"; ResultSet rs=db.getResultSet(conn,table,where); try { while(rs.next()){ city.add(new LabelValueBean(rs.getString(2),rs.getString(1))); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } session.setAttribute("city",city); table = "area_view"; where = "cityID='" + scity + "'"; ArrayList area=new ArrayList(); area.add(new LabelValueBean("请选择所在地点","000000")); ResultSet rs2=db.getResultSet(conn,table,where); try { while(rs2.next()){ area.add(new LabelValueBean(rs2.getString(2),rs2.getString(1))); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } session.setAttribute("area", area); } db.closeDB(); // TODO Auto-generated method stub return mapping.findForward("find"); } }
-------------------------------------------------------------------------- 三级(province--city---area)连动 逻辑思路
(1) province --- city (2) city----area
(3) area
------------------------------------------------------------------------------
(3)jsp 文件说明
<%@ page contentType="text/html; charset=gb2312"%> <%@ page import="java.util.*"%> <%@ taglib uri="http://jakarta.apache.org/struts/tags-bean" prefix="bean" %> <%@ taglib uri="http://jakarta.apache.org/struts/tags-html" prefix="html" %> <%@ taglib uri="http://jakarta.apache.org/struts/tags-logic" prefix="logic" %> <%@ taglib uri="http://jakarta.apache.org/struts/tags-tiles" prefix="tiles" %> <%@ taglib uri="http://jakarta.apache.org/struts/tags-template" prefix="template" %> <%@ taglib uri="http://jakarta.apache.org/struts/tags-nested" prefix="nested" %>
<script language="JavaScript"> function getCityOptions(province) { form = province.form; form.action = "/teyunwang/getCA.do?provinceID="+province.value; form.submit( ); } function getAreaOptions(city,id){ city.options[id].selected=true; form = city.form; form.action = "/teyunwang/getCA.do?cityID="+city.value; form.submit( );
} </script> <% //out.println(session.getAttribute("selectedProvince")); ArrayList city=(ArrayList)request.getAttribute("city"); pageContext.setAttribute("city",city); //pageContext.setAttribute("selectedProvince",session.getAttribute("selectedProvince")); %> <% ArrayList area=(ArrayList)request.getAttribute("area"); pageContext.setAttribute("area",area); %>
<p>搜索大件会员信息</p> <table width="305"> <html:form method="post" action="/placeFind"> <tr> <td colspan="4"> 按地点:</td> </tr> <tr> <td> <html:select property="province" size="1" onchange="getCityOptions(this)"> <logic:present name="province"> <html:options collection="province" property="value" labelProperty="label"/> </logic:present> <logic:notPresent name="provicne"> <html:option value="000000">请选择所在省/直辖市</html:option> <html:option value="110000">北京市</html:option> <html:option value="120000">天津市</html:option> <html:option value="130000">河北省</html:option> <html:option value="140000">山西省</html:option> <html:option value="150000">内蒙古自治区</html:option> <html:option value="210000">辽宁省</html:option> <html:option value="220000">吉林省</html:option> <html:option value="230000">黑龙江省</html:option> <html:option value="310000">上海市</html:option> <html:option value="320000">江苏省</html:option> <html:option value="330000">浙江省</html:option> <html:option value="340000">安徽省</html:option> <html:option value="350000">福建省</html:option> <html:option value="360000">江西省</html:option> <html:option value="370000">山东省</html:option> <html:option value="410000">河南省</html:option> <html:option value="420000">湖北省</html:option> <html:option value="430000">湖南省</html:option> <html:option value="440000">广东省</html:option> <html:option value="450000">广西壮族自治区</html:option> <html:option value="460000">海南省</html:option> <html:option value="500000">重庆市</html:option> <html:option value="510000">四川省</html:option> <html:option value="520000">贵州省</html:option> <html:option value="530000">云南省</html:option> <html:option value="540000">西藏自治区</html:option> <html:option value="610000">陕西省</html:option> <html:option value="620000">甘肃省</html:option> <html:option value="630000">青海省</html:option> <html:option value="640000">宁夏回族自治区</html:option> <html:option value="650000">新疆维吾尔自治区</html:option> </logic:notPresent> </html:select> </td> <td> <html:select property="city" size="1" onchange="getAreaOptions(this,this.selectedIndex)"> <logic:notPresent name="city"> <html:option value="000000">请选择所在地级市</html:option> </logic:notPresent> <logic:present name="city"> <html:options collection="city" property="value" labelProperty="label"/> </logic:present> </html:select> </td>
<td> <html:select property="area" size="1" value="<bean write name='selectedArea'/>"> <logic:notPresent name="area"> <html:option value="000000">请选择所在地点</html:option> </logic:notPresent> <logic:present name="area"> <html:options collection="area" property="value" labelProperty="label"/> </logic:present> </html:select> </td> <td><html:submit >搜索</html:submit></td> </tr> </html:form> </table>
------------------------------------------------------------------------------ 另:DateBase.java文件如下:
package com.teyunwang;
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList;
import org.apache.struts.util.LabelValueBean;
public class DataBase { Connection conn;
Statement stmt;
ResultSet rs;
public Connection getConnection() { try { Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver") .newInstance(); } catch (ClassNotFoundException e) {
} catch (IllegalAccessException ex) {
} catch (InstantiationException ex) {
}
try { String url = "jdbc:microsoft:sqlserver://202.114.101.78:1433;databasename=teyunwang"; String user = "sa"; String password = "123";
conn = DriverManager.getConnection(url, user, password); // 获得连接
} catch (SQLException e) { System.out.print(e); } return conn; }
public ResultSet getResultSet(Connection conn, String sql) { try { stmt = conn.createStatement(); sql = "select * from " + sql; rs = stmt.executeQuery(sql); return rs; } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } // 获得执行SQL语言的容器 // 获得结果集
}
public boolean insertRecords(Connection conn, String table, String column, String value) { try { String sql; stmt = conn.createStatement(); sql = "insert into "; sql = sql + table; sql = sql + "(" + column + ") "; sql = sql + "values(" + value + ")"; boolean bl = stmt.execute(sql); return bl;
} catch (SQLException e) { // TODO Auto-generated catch block
e.printStackTrace(); return false; }
}
public ResultSet getResultSet(Connection conn, String table, String where) { try { stmt = conn.createStatement(); String sql = "select * from "; sql = sql + table; sql = sql + " where "; sql = sql + where; rs = stmt.executeQuery(sql); return rs; } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } // 获得执行SQL语言的容器 // 获得结果集
}
public ArrayList getArray(ResultSet rs) {
ArrayList result = new ArrayList(); try { while (rs.next()) {
result.add(new LabelValueBean(rs.getString(2), rs .getString(1)));// id // System.out.println(rs.getString(1)); // tempR.add(rs.getString(2)); // System.out.println(rs.getString(2)); // tempR.add(rs.getString(3));// name // System.out.println(rs.getString(3)); // 上级类 } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return result; }
public void closeDB() { try { if (rs != null) { rs.close(); } if (stmt != null) { stmt.close(); } if (conn != null) { conn.close(); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } //main函数测试用。。 public static void main(String args[]) { DataBase db = new DataBase(); Connection conn = db.getConnection(); String name = "luohuiheng"; String pwd = "123"; String classid = "1"; // ClassChange cc=new ClassChange(); // Date time=cc.changeStringToDate("1979-11-7"); String time = "1979-11-05"; double fl = 999.6;
String table = "user_basic"; String column = "user_name,user_pwd,user_class_id,user_lastlogtime,compa_capital"; String value = "'" + name + "','" + pwd + "'," + classid + ",'" + time + "'," + fl; db.insertRecords(conn, table, column, value); db.closeDB();
/* * String ss="110228"; ResultSet * rs=db.getResultSet(db.getConnection(),"area_view","areaID='"+ss+"'"); * * try { rs.next(); System.out.println(rs.getString("othername")); } * catch (SQLException e) { // TODO Auto-generated catch block * e.printStackTrace(); } //ArrayList array=db.getArray(rs); //for(int * i=0;i<array.size();i++){ //System.out.println(array.get(i)); //} */
} }
附件:1156086776470lStuts.rar(4K)
|