三级联动
丑是丑了点,但是还是有成就感的 -------
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!DOCTYPE HTML > <html> <head> <base href="<%=basePath%>"> <title>添加页面</title> <!-- The styles --> <link id="bs-css" href="css/bootstrap-cerulean.css" rel="stylesheet"> <style type="text/css"> body { padding-bottom: 40px; } .sidebar-nav { padding: 9px 0; } </style> <!-- The HTML5 shim, for IE6-8 support of HTML5 elements --> <!--[if lt IE 9]> <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script> <![endif]--> <!-- The fav icon --> <link rel="shortcut icon" href="img/favicon.ico"> </head> <body> <div class="row-fluid sortable"> <div class="box span12"> <div class="box-header well" data-original-title> <h2><i class="icon-edit"></i>添加界面</h2> <div class="box-icon"> <a href="#" class="btn btn-setting btn-round"><i class="icon-cog"></i></a> <a href="#" class="btn btn-minimize btn-round"><i class="icon-chevron-up"></i></a> <a href="#" class="btn btn-close btn-round"><i class="icon-remove"></i></a> </div> </div> <div class="box-content"> <form class="form-horizontal" method="post" action="addServlet" enctype="multipart/form-data"> <fieldset> <legend>添加界面</legend> <div class="control-group"> <label class="control-label" for="typeahead">商品名称</label> <div class="controls"> <input type="text" class="span6 typeahead" id="typeahead" name="name" value="" > </div> </div> <div class="control-group"> <label class="control-label" for="typeahead">商品描述</label> <div class="controls"> <input type="text" name="description" value="" > </div> </div> <div class="control-group"> <label class="control-label" for="typeahead">商品价格</label> <div class="controls"> <input name="price" value="" > </div> </div> <div class="control-group"> <label class="control-label" for="typeahead">商品库存</label> <div class="controls"> <input name="stock" value="" > </div> </div> <div id="sanji"> 分类 </div> <div class="control-group"> <label class="control-label" for="typeahead">商品图片</label> <div class="controls"> <input type="text" name="fileName" value="" > </div> </div> <div class="control-group"> <label class="control-label" for="typeahead">是否删除</label> <div class="controls"> <input type="text" name="isDelete" value=""> </div> </div> <div class="form-actions"> <button type="submit" class="btn btn-primary" >添加</button> </div> </fieldset> </form> </div> </div><!--/span--> </div><!--/row--> <script type="text/javascript" src="js/jquery-1.8.2.min.js"></script> <script type="text/javascript"> $(document).ready(function(e){ //三个下拉列表 //加载显示数据 $("#sanji").html("<select id='sheng'></select><select id='shi'></select><select id='qu'></select>"); //加载省份 FillSheng(); //加载市 FillShi(); //加载区 FillQu(); $("#sheng").change(function(){ FillShi(); FillQu(); }); //当市发生改变 $("#shi").change(function(){ FillQu(); }); }); //加载省份信息 function FillSheng() { //根据父级代号 //取父级代号 var pcode = 0; //根据父级代号查数据 $.ajax({ async:false, url:"ProductcategoryList", data:{"pcode":pcode}, type:"POST", dataType:"JSON", success:function(data) { var str = ""; for(var sj in data) { str = str+"<option value = '"+data[sj].id+"'>"+data[sj].name+"</option>"; } $("#sheng").html(str); } }); } //加载市 function FillShi() { //根据父级代号 //取父级代号 var pcode = $("#sheng").val(); //根据父级代号查数据 $.ajax({ async:false, //取消异步 url:"ProductcategoryList", data:{"pcode":pcode}, type:"POST", dataType:"JSON", success:function(data) { var str = ""; for(var sj in data) { str = str+"<option value = '"+data[sj].id+"'>"+data[sj].name+"</option>"; } $("#shi").html(str); } }); } //加载区 function FillQu() { //根据父级代号 //取父级代号 var pcode = $("#shi").val(); //根据父级代号查数据 $.ajax({ url:"ProductcategoryList", data:{"pcode":pcode}, type:"POST", dataType:"JSON", success:function(data) { var str = ""; for(var sj in data) { str = str+"<option value = '"+data[sj].id+"'>"+data[sj].name+"</option>"; } $("#qu").html(str); } }); } </script> </body> </html>
import cn.easy.util.ProductCategoryList; @WebServlet("/ProductcategoryList") public class ProductcategoryList extends HttpServlet{ @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Product_CategoryService service=new Product_CategoryServiceImpl(); try { int pcode = Integer.parseInt(request.getParameter("pcode")); List<Product_Category> list = service.selProductCate(pcode); System.out.println("service.selProductCate() list"+list); Gson gson=new Gson(); String json = gson.toJson(list); PrintWriter writer = response.getWriter(); writer.print(json); writer.close(); } catch (Exception e) { e.printStackTrace(); } } }
@Override public List<Product_Category> selProductCate(int id) throws Exception { String sql=" SELECT * FROM easybuy_product_category where parentld=?"; ResultSet rs = executeQuery(sql,id ); List<Product_Category> list = new ArrayList<Product_Category>(); while(rs.next()){ Product_Category category =new Product_Category(); category.setId(rs.getInt("id")); category.setName(rs.getString("name")); category.setParentld(rs.getInt("parentld")); category.setType(rs.getInt("type")); list.add(category); } return list; }