Java framework SSH combined with Easyui controls to implement sample parsing of provincial/municipal/county-level linkage, ssheasyui

Source: Internet
Author: User

Java framework SSH combined with Easyui controls to implement sample parsing of provincial/municipal/county-level linkage, ssheasyui

Easyui calls the database to achieve the effect of three levels of association between province, city, and district.






1. First, design the database ,.One has four fields: code, note, and pycode. Code: Administrative Division code, note: Chinese comments, pycode: pinyin abbreviation. The code consists of six fields. If the last four digits of the province are 0000, the last two digits of the prefecture-level city are 00, and the other digits are counties and districts.

I have uploaded the relevant database code to my csdn resources, and you need to download it yourself.


2. I am using the java and SSH frameworks combined with Easyui controls.

3. the html code is as follows:

<Tr> <td class = "left"> Province: </td> <input name = "contact. province "id =" province "style =" width: 174px; "> </td> <td class =" left "> city: </td> <input name = "contact. city "id =" city "style =" width: 174px; "> </td> <td class =" left "> county: </td> <input name = "contact. county "id =" county "style =" width: 174px; "> </td> </tr>

4. The corresponding JS Code is as follows:

$ (Function () {// select the control from the drop-down list. The drop-down list shows the dynamic query of database information $ ('# province '). combobox ({url: 'apply/provincecombobox_combox.action ', editable: false, // cache: false in non-editable status, // panelHeight: 'auto', // automatically fit valueField: 'code', textField: 'note', onHidePanel: function () {$ ("# city "). combobox ("setValue", ''); $ (" # county "). combobox ("setValue", ''); $ (" # cregicounty "). val (''); var province = $ ('# province '). combobox ('getvalue'); I F (province! = '') {$. Ajax ({type:" POST ", url:" apply/citycombobox_combox.action? Province = "+ province, cache: false, dataType:" json ", success: function (data) {$ (" # city "). combobox ("loadData", data) ;}}}}); $ ('# City '). combobox ({editable: false, // cache: false, // panelHeight: 'auto', // The automatic height is suitable for 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, dataType:" json ", success: function (data) {$ (" # county "). combobox ("loadData", data) ;}}}}); $ ('# county '). combobox ({editable: false, // cache: false, // panelHeight: 'auto', // The automatic height is suitable for valueField: 'code', textField: 'note', onHidePanel: function () {var str = $ ('# county '). combobox ('gettext'); $ ("# cregicounty "). val (str) ;}}); $ ('# country '). combobox ({// country Code initialization valueField: 'English ', textField: 'note', url: 'json/country. json', cache: false, // panelHeight: 'auto', // auto height is applicable to onChange: function (newValue, oldValue) {countrySearch (newValue ); countrys (newValue );}});});

5. Java Action Code

// Query the public String provinceCombobox () throws Exception {List list = comboboxService. findProvince (); this. jsonUtil (list); return null;} // query the public String cityCombobox () throws Exception {List list = comboboxService. findCity (province); this. jsonUtil (list); return null;} // query public String countyCombobox () throws Exception {List list = comboboxService. findCounty (city); this. jsonUtil (list); return null;} // call the json tool method and input the 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. JSON code of the Tool

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. corresponding interface code

// Query the provincial public List findProvince () throws Exception; // query the municipal public List findCity (String code) throws Exception; // query the county public List findCounty (String code) throws Exception;

8. corresponding interface implementation class code

// Drop-down box -- query the province public List findProvince () {log.info ("=== drop-down 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 () ;}// drop-down box -- Query public List findCity (String code2) {log.info ("=== drop-down box -- Query City"); String id = code2.substring ); 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 () ;}// drop-down box -- query the county public List findCounty (String code3) {log.info ("=== drop-down box -- Query county"); String id = code3.substring ); 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 all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Related Article

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.