Java framework SSH combined with Easyui control to realize the example of three-level linkage between cities and counties _jquery

Source: Internet
Author: User
Tags object object ssh

Easyui call database to realize the effect of three-level linkage between county and province if the next






1, first to design the database, as shown in the picture. one has 4 fields Code,note,pycode. Code: Administrative division codes, note: Chinese annotation, pycode: pinyin abbreviation. Where code is made up of 6 fields. If the last 4 is the provincial level is 0000, if the prefecture-level city last 2 is 00, the other is County district.

I have uploaded the relevant database code to my CSDN resources, the students need to download.


2, I use the Java, SSH framework combined with Easyui control

3, the HTML code is as follows

     <tr>                               
       <td class= "left" > Province:</td>  
       <td><input name= "contact.province" id= "province" Style= "width:174px" ></td>     
       <td class= "left" > City:</td> <td><input          
       name=  " Contact.city "id=" City "style=" WIDTH:174PX; ></td>                       
       <td class= "left" > County District:</td>  
       <td><input name= "Contact.county" County "style=" WIDTH:174PX; "></td>
    </tr>

4, the corresponding JS code is as follows

$ (function () {//dropdown box Select control, the contents of the Drop-down box is dynamic query database information $ (' #province '). ComboBox ({url: ' apply/provincecombobox_combobox.action ' , Editable:false,//non-editable State Cache:false,//panelheight: ' Auto ',//Auto height fit Valuefield: ' Code ', TextField
   : ' Note ', Onhidepanel:function () {$ ("#city"). ComboBox ("SetValue", "");
   $ ("#county"). ComboBox ("SetValue", "");
  $ ("#cregicounty"). Val ('); 
  var province = $ (' #province '). ComboBox (' GetValue '); if (province!= ') {$.ajax ({type: "POST", url: "apply/citycombobox_combobox.action?province=" +province, Cache:fals
                 E, DataType: "JSON", success:function (data) {$ ("#city"). ComboBox ("LoadData", data); 
              }
                }); 
 
 }
           } 
         }); $ (' #city '). ComboBox ({editable:false,//non-editable state Cache:false,//panelheight: ' Auto ',//automatic height fit Valuefield: ' Code
   ', TextField: ' Note ', Onhidepanel:function () {$ ("#cregicounty"). Val (');
 $ ("#county"). ComboBox ("SetValue", ""); var city = $ (' #city '). ComboBox (' GetValue '); if (city!= ') {$.ajax ({type: "POST", url: "apply/countycombobox_combobox.action?city=" +city, Cache:false, Datat
                 ype: "JSON", success:function (data) {$ ("#county"). ComboBox ("LoadData", data); 
              }
                }); 
 }
           }
  }); $ (' #county '). ComboBox ({editable:false,//non-editable State Cache:false,//panelheight: ' Auto ',//Auto height fit Valuefield: ' Cod
   E ', TextField: ' Note ', onhidepanel:function () {var str=$ (' #county '). ComboBox (' GetText '); 
           $ ("#cregicounty"). Val (str); 
 
  }
 }); $ (' #country '). ComboBox ({//Country code initialization Valuefield: ' 中文版 ', TextField: ' Note ', url: ' Json/country.json ', Cache:fal
   SE,//panelheight: ' Auto ',//Auto height fit onchange:function (newvalue,oldvalue) {countrysearch (newvalue);
   Countrys (NewValue);
}
 }); 

 });

5, the Java Action Code

Query the national Administrative Code Province public
 String Provincecombobox () throws exception{
 
 List list=comboboxservice.findprovince ();
 This.jsonutil (list);
 return null;
 }
 
 Query the national Administrative Code City public
 String Citycombobox () throws exception{
  
 List list=comboboxservice.findcity (province);
 This.jsonutil (list);
   return null;
 }
 
 Query national Administrative Code County District public
 String Countycombobox () throws exception{
 
 List list=comboboxservice.findcounty (city) ;
 This.jsonutil (list);
   return null;
 }
 
 
 Invokes the JSON tool method, passing in the parameter alist public
 void Jsonutil (Object accountlist) throws exception{HttpServletResponse
  Response = Servletactioncontext.getresponse (); 
  Log.info ("JSON format:" + accountlist.tostring ());
  
  String Returnjson = Jsonconvert.returnjson (accountlist);
  Response.setcharacterencoding ("Utf-8");
  Response.getwriter (). println (Returnjson);
 

6. Tool JSON code

Import Java.io.StringWriter;

Import Org.codehaus.jackson.map.ObjectMapper;

public class JsonConvert {
 static String jsonstr;
 public static String Returnjson (Object object) throws exception{
 objectmapper objectmapper = new Objectmapper ();
 StringWriter StringWriter = new StringWriter ();
 Objectmapper.writevalue (StringWriter, object);
 
 Jsonstr = Stringwriter.tostring ();
 Return Jsonstr
 }
}

7, the corresponding interface code

Query the province public
List Findprovince () throws Exception;
Query the city public
List findcity (String code) throws Exception;
Query county Public
List Findcounty (String code) throws Exception;

8, the corresponding interface implementation class code

 Dropdown box--query Province public List findprovince () {log.info ("= = Dropdown Box--Query province");
 Criteria criteria=this.sessionfactory.getcurrentsession (). Createcriteria (Careacodetblqg.class);
 Criteria.add (Restrictions.like ("code", "%0000"));
 
 Criteria.addorder (ORDER.ASC ("Code"));
 return Criteria.list ();
 //dropdown box--query City public List findcity (String code2) {log.info ("= = Dropdown Box--Query City");

 String id=code2.substring (0,2);
 Criteria criteria=this.sessionfactory.getcurrentsession (). Createcriteria (Careacodetblqg.class);
 Criteria.add (Restrictions.like ("code", id+ "%00"));
 Criteria.add (Restrictions.ne ("code", Code2));
 
 Criteria.addorder (ORDER.ASC ("Code"));
 return Criteria.list ();

 //dropdown box--query county public List findcounty (String code3) {log.info ("= = Dropdown box-query county area");
 String id=code3.substring (0,4); 
 
 Criteria criteria=this.sessionfactory.getcurrentsession (). Createcriteria (Careacodetblqg.class);
 Criteria.add (Restrictions.like ("code", id+ "%"));
 Criteria.add (Restrictions.not (Restrictions.like ("code", "%01")); CrIteria.add (Restrictions.ne ("code", Code3));
 
 Criteria.addorder (ORDER.ASC ("Code"));
 return Criteria.list ();

 }

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.